Created
September 2, 2016 12:23
-
-
Save bmdoherty/c87524f9a91c4a501974fbe14b0ba63e to your computer and use it in GitHub Desktop.
brendan_puzzler
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 f(bytes){ | |
var hash = []; | |
var unit = 0; | |
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; | |
} | |
function smallestWholeNumberGreaterThanZero(prev, item){ | |
if (prev eq 0){ | |
return item; | |
} | |
if (item eq 0){ | |
return prev; | |
} | |
return (prev < item ? prev : item); | |
} | |
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