Created
January 7, 2012 09:49
-
-
Save mourner/1574304 to your computer and use it in GitHub Desktop.
Codility demo tests solutions
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
// 100-score solution for http://codility.com/demo/take-sample-test/ | |
function equi(array) { | |
var i, | |
len = array.length, | |
sum = 0, | |
leftSum = 0, | |
rightSum; | |
for (i = 0; i < len; i++) { | |
sum += array[i]; | |
} | |
for (i = 0; i < len; i++) { | |
rightSum = sum - leftSum - array[i]; | |
if (leftSum === rightSum) { | |
return i; | |
} | |
leftSum += array[i]; | |
} | |
return -1; | |
} | |
// 100-score solution for http://codility.com/demo/take-sample-test/ps/ | |
function ps(array) { | |
var i, | |
len = array.length, | |
memo = {}, | |
result = 0, | |
value; | |
for (i = 0; i < len; i++) { | |
value = array[i]; | |
if (!memo[value]) { | |
result = i; | |
memo[value] = true; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's outdated