Last active
August 29, 2015 14:08
-
-
Save geirman/17788d5e5d513f294e4d to your computer and use it in GitHub Desktop.
This is my submission to Adam Cameron's weekend code quiz (http://blog.adamcameron.me/2014/11/something-for-weekend-wee-code-quiz-in.html)
This file contains hidden or 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 getSubseries(series, threshold){ | |
/* | |
* Setup a subseries container, to be replaced whenever a longer series is | |
* found. As well, let's calculate the length of the original series to avoid | |
* doing it repeatedly with every iteration of the following for loop; | |
*/ | |
var subseries = []; | |
var seriesLen = arrayLen(series); | |
/* | |
* Setup two for loops, the first to keep track of the start index (a) and the second | |
* will be used to keep track of the end index (z). We'll increment through each start | |
* index then reduce the array from the end, replacing the subseries whenever BOTH the | |
* sum is <= threshold AND the array length is greater than the length of the value | |
* currently stored there. | |
*/ | |
for(var a = 1; a <= seriesLen; a++){ | |
var z = seriesLen; | |
var buffer = { | |
'array' = [], | |
'sum' = 0 | |
}; | |
for( z; z > a; z--){ | |
buffer.array = arraySlice(series, a, z-a+1); | |
buffer.sum = arraySum( buffer.array ); | |
if( buffer.sum <= threshold && arrayLen(buffer.array) > arrayLen(subseries) ){ | |
subseries = buffer.array; | |
} | |
} | |
} | |
return(subseries); | |
} | |
/* | |
* | |
* UNIT TESTS BELOW | |
* | |
*/ | |
tests = [ | |
{ | |
'test' = 'Finds series in the middle of an array', | |
'threshold' = 500, | |
'series' = [100, 300, 100, 50, 50, 50, 50, 50, 500, 200, 100], | |
'expected' = [100, 50, 50, 50, 50, 50] | |
},{ | |
'test' = 'Tests whether a match is found at the BEGINNING of the array', | |
'threshold' = 30, | |
'series' = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100], | |
'expected' = [10, 20] | |
},{ | |
'test' = 'Tests whether a match is found at the END of the array', | |
'threshold' = 30, | |
'series' = [50, 40, 30, 20, 10], | |
'expected' = [20, 10] | |
},{ | |
'test' = 'Tests empty array', | |
'threshold' = 30, | |
'series' = [], | |
'expected' = [] | |
},{ | |
'test' = 'Tests that entire array is selected when threshold is greater than its sum', | |
'threshold' = 1550, | |
'series' = [100, 300, 100, 50, 50, 50, 50, 50, 500, 200, 100], | |
'expected' = [100, 300, 100, 50, 50, 50, 50, 50, 500, 200, 100] | |
} | |
]; | |
function unitTest(testData){ | |
var result.subseries = getSubseries( testData.series, testData.threshold ); | |
var result.expected = testData.expected; | |
if( arrayToList( result.subseries) == arrayToList( result.expected ) ){ | |
report( 'PASS', testData.test ); | |
}else{ | |
report( 'FAIL', testData.test, result ); | |
} | |
} | |
function report( status, test, result ){ | |
switch( status ) { | |
case 'PASS': | |
writeOutput('<div style="color: green;">PASS: ' & test); | |
break; | |
case 'FAIL': | |
writeOutput('<div style="color: red;">FAIL: ' & test); | |
writeDump( result ); | |
break; | |
default: | |
report('FAIL', 'default case', 'unexpected error'); | |
} | |
} | |
for(test in tests){ | |
unitTest(test); | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment