Created
September 2, 2016 12:00
-
-
Save bmdoherty/5223df62f9be22c70ac2413bffad64e1 to your computer and use it in GitHub Desktop.
Friday Puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function smallestWholeNumberGreaterThanZero(prev, item){ | |
if (prev eq 0){ | |
return item; | |
} | |
if (item eq 0){ | |
return prev; | |
} | |
return (prev < item ? prev : item); | |
} | |
function f(bytes){ | |
var hash = []; | |
var unit; | |
var magnitude; | |
var sizes = [ | |
{'unit':'B', size:1}, | |
{'unit':'KB', size:1024}, | |
{'unit':'MB', size:1024*1024}, | |
{'unit':'GB', size:1024*1024*1024}, | |
{'unit':'TB', size:1024*1024*1024*1024}, | |
{'unit':'PB', size:1024*1024*1024*1024*1024} | |
]; | |
for (unit in sizes) { | |
hash.append( int(bytes/unit.size) ); | |
} | |
unit = hash.reduce(smallestWholeNumberGreaterThanZero, 0); | |
magnitude = sizes[arrayFind(hash, unit)].unit; | |
return unit & ' ' & magnitude | |
} | |
writedump(f(1)); | |
writedump(f(1024)); | |
writedump(f(1048576)); | |
writedump(f(1234567)); | |
writedump(f(9999999999)); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment