Skip to content

Instantly share code, notes, and snippets.

@MikeTatsky
Created November 10, 2016 09:19
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 MikeTatsky/75a53a2ea9d911b83cbeb206ef536aad to your computer and use it in GitHub Desktop.
Save MikeTatsky/75a53a2ea9d911b83cbeb206ef536aad to your computer and use it in GitHub Desktop.
var arr = [];
function isOdd(n){
return Math.abs(n % 2) == 1;
}
function isSortedAsc(arr){
var value = arr[0],
i = 1,
iL = arr.length;
for (;i<iL;i++){
if(arr[i] > value){
return false;
}
else{
value = arr[i];
}
}
return true;
}
var getInput = function(value){
if(value === '#'){
switch(arr.length){
case 0:
return;
case 1:
print(arr[0]);
arr = [];
break;
default:
if(isOdd(arr.length) && isSortedAsc(arr)){
print(arr.splice((arr.length + 1) / 2 - 1, 1)[0]);
}
else{
print(arr.splice(arr.length / 2 + 1 - 1, 1)[0]);
}
}
}
else{
arr.push(parseInt(value));
}
};
var line;
while(line = readline()){
getInput(line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment