Skip to content

Instantly share code, notes, and snippets.

@augusto-altman
Last active April 21, 2016 00:02
Show Gist options
  • Save augusto-altman/07c5d935d002ba15edc3d1eeca0e0c72 to your computer and use it in GitHub Desktop.
Save augusto-altman/07c5d935d002ba15edc3d1eeca0e0c72 to your computer and use it in GitHub Desktop.
Codility - Alpha 2010
//Problem link: https://codility.com/programmers/challenges/alpha2010/
//Max score: 100% - Total time: 1 hr 02 mins
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
//First solution - Score: 84%
//Link: https://codility.com/demo/results/trainingS8K2BU-VRE/
//Time: 17 mins
function solution(A) {
for(var i = 0; i< A.length; i++) {
var sub = A.slice(0, i);
if(isMagic(sub, A)) {
return i-1;
}
}
return A.length - 1;
}
function isMagic(sub, A) {
for(var j = 0; j < A.length; j++) {
if(sub.indexOf(A[j]) < 0) {
return false;
}
}
return true;
}
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
//Second solution - Score: 92%
//Link: https://codility.com/demo/results/trainingZVPKG9-QMD/
//Time: 27 mins
function solution(A) {
var uniqueA = getUnique(A);
var max;
for(var i = 0; i < uniqueA.length; i++) {
if(typeof(max) === 'undefined' || A.indexOf(uniqueA[i]) > max) {
max = A.indexOf(uniqueA[i]);
}
}
return max;
}
function getUnique(a) {
return a.filter(function(item, pos) {
return a.indexOf(item) == pos;
});
}
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------
//Last solution - Score: 100%
//Link: https://codility.com/demo/results/trainingPKDMA3-AMY/
//Time 18 mins
function solution(A) {
var indexOfObject = getIndexOfObject(A);
var max;
for(var value in indexOfObject) {
if(typeof(max) === 'undefined' || indexOfObject[value] > max) {
max = indexOfObject[value];
}
}
return max;
}
function getIndexOfObject(a) {
var res = {};
for(var i = 0; i < a.length; i++) {
if(typeof(res[a[i]]) === 'undefined') {
res[a[i]] = i;
}
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment