Skip to content

Instantly share code, notes, and snippets.

@botic
Created November 7, 2010 15:24
Show Gist options
  • Save botic/666175 to your computer and use it in GitHub Desktop.
Save botic/666175 to your computer and use it in GitHub Desktop.
FMI-UE1-EX9
include('ringo/term');
var select3rd = function(n, L) {
var pos = 0, counter = 2, length = L.length;
while (counter >= -1 && pos < length) {
if (L[pos] > n) {
counter--;
}
pos++;
}
return counter == 0; // counter is 3rd biggest
}
var call = function(n, L) {
if (select3rd(n, L) === true) {
write(GREEN, BOLD, INVERSE, " YES ");
} else {
write(RED, BOLD, INVERSE, " NO ");
}
write(RESET);
writeln(" " + n + " in " + L);
}
call(3, [1,2]); // NO
call(3, [1,2,4,5,6]); // NO
call(3, [1,2,3,5,6]); // YES
call(3, [1,2,3,5,6,7]); // NO
call(50, [10, 20, 30, 31, 32, 33, 40, 50, 51, 52]); // YES
call(50, [10, 20, 30, 31, 32, 33, 40, 50, 51, 52, 60]); // NO
/***** DEMO *****
pn-macbook:Übungen philipp$ ~/ringo/ringojs-0.6/bin/ringo select-3rd.js
NO 3 in 1,2
NO 3 in 1,2,4,5,6
YES 3 in 1,2,3,5,6
NO 3 in 1,2,3,5,6,7
YES 50 in 10,20,30,31,32,33,40,50,51,52
NO 50 in 10,20,30,31,32,33,40,50,51,52,60
******* DEMO *****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment