Skip to content

Instantly share code, notes, and snippets.

@bmdoherty
Created September 2, 2016 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmdoherty/c87524f9a91c4a501974fbe14b0ba63e to your computer and use it in GitHub Desktop.
Save bmdoherty/c87524f9a91c4a501974fbe14b0ba63e to your computer and use it in GitHub Desktop.
brendan_puzzler
<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