File size distribution: Difference between revisions
Content deleted Content added
Add Julia language |
Added Kotlin |
||
Line 190: | Line 190: | ||
-> total: 7.3 MB bytes and 1376 files</pre> |
-> total: 7.3 MB bytes and 1376 files</pre> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.2.10 |
|||
import java.io.File |
|||
import kotlin.math.log10 |
|||
import kotlin.math.floor |
|||
fun fileSizeDistribution(path: String) { |
|||
val sizes = IntArray(12) |
|||
val p = File(path) |
|||
val files = p.walk() |
|||
var accessible = 0 |
|||
var notAccessible = 0 |
|||
var totalSize = 0L |
|||
for (file in files) { |
|||
try { |
|||
if (file.isFile()) { |
|||
val len = file.length() |
|||
accessible++ |
|||
if (len == 0L) { |
|||
sizes[0]++ |
|||
continue |
|||
} |
|||
totalSize += len |
|||
val logLen = log10(len.toDouble()) |
|||
val index = floor(logLen).toInt() |
|||
sizes[index + 1]++ |
|||
} |
|||
} |
|||
catch (se: SecurityException) { |
|||
notAccessible++ |
|||
} |
|||
} |
|||
println("File size distribution for '$path' :-\n") |
|||
for (i in 0 until sizes.size) { |
|||
print(if (i == 0) " " else "+ ") |
|||
print("Files less than 10 ^ ${"%-2d".format(i)} bytes : ") |
|||
println("%5d".format(sizes[i])) |
|||
} |
|||
println(" -----") |
|||
println("= Number of accessible files : ${"%5d".format(accessible)}") |
|||
println("\n Total size in bytes : $totalSize") |
|||
println("\n Number of inaccessible files : ${"%5d".format(notAccessible)}") |
|||
} |
|||
fun main(args: Array<String>) { |
|||
fileSizeDistribution("./") // current directory |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
File size distribution for './' :- |
|||
Files less than 10 ^ 0 bytes : 2 |
|||
+ Files less than 10 ^ 1 bytes : 0 |
|||
+ Files less than 10 ^ 2 bytes : 46 |
|||
+ Files less than 10 ^ 3 bytes : 380 |
|||
+ Files less than 10 ^ 4 bytes : 558 |
|||
+ Files less than 10 ^ 5 bytes : 19 |
|||
+ Files less than 10 ^ 6 bytes : 6 |
|||
+ Files less than 10 ^ 7 bytes : 5 |
|||
+ Files less than 10 ^ 8 bytes : 0 |
|||
+ Files less than 10 ^ 9 bytes : 0 |
|||
+ Files less than 10 ^ 10 bytes : 0 |
|||
+ Files less than 10 ^ 11 bytes : 0 |
|||
----- |
|||
= Number of accessible files : 1016 |
|||
Total size in bytes : 14459732 |
|||
Number of inaccessible files : 0 |
|||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |