Last active
September 6, 2017 09:22
-
-
Save CroModder/2c9223642ad0706a7757f95d4b6bef5a to your computer and use it in GitHub Desktop.
cloudsense
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
const findSumIndex = require('./task_1'); | |
describe('findSumIndex', () => { | |
it('should return -1 | Input [1,2,3]', () => { | |
expect(findSumIndex([1,2,3])).toBe(-1); | |
}); | |
it('should return 0 | Input [1]', () => { | |
expect(findSumIndex([1])).toBe(0); | |
}); | |
it('should return 2 | Input [1,2,3,2,1]', () => { | |
expect(findSumIndex([1,2,3,2,1])).toBe(2); | |
}); | |
it('should return 4 | Input [-6,4,3,5,7,3,1,2]', () => { | |
expect(findSumIndex([-6,4,3,5,7,3,1,2])).toBe(4); | |
}); | |
}); |
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
function findSumIndex(input) { | |
if (!input.length) return -1; | |
let rightSum = input.reduce((acc, val) => acc + val, 0); | |
let leftSum = 0; | |
for (let i = 0; i < input.length; i++) { | |
rightSum = rightSum - input[i]; | |
if (rightSum === leftSum) return i; | |
leftSum = leftSum + input[i]; | |
} | |
return -1; | |
} | |
module.exports = findSumIndex; |
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
const mostLeastFrq = require('./task_3'); | |
describe('mostLeastFrq', () => { | |
it('should return 21 | Input [1,2,2]', () => { | |
expect(mostLeastFrq([1,2,2])).toBe(21); | |
}); | |
it('should return 31 | Input [1,2,2,333]', () => { | |
expect(mostLeastFrq([1,2,2,333])).toBe(31); | |
}); | |
it('should return 24 | Input [11,222,3,3,4]', () => { | |
expect(mostLeastFrq([11,222,3,3,4])).toBe(24); | |
}); | |
it('should return 34 | Input [3,33224]', () => { | |
expect(mostLeastFrq([3,33224])).toBe(34); | |
}); | |
}); |
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
function mostLeastFrq(input) { | |
if (!input.length) return -1; | |
let mf = 1, lf = 1, freq = 1; | |
let arr = input.toString().replace(/\D/g, '').split('').map(Number).sort(); | |
let mfElem = arr[0], lfElem = arr[0]; | |
console.log(arr); | |
for(let i=0; i < arr.length; i++){ | |
if(arr[i]===arr[i+1]){freq++;} | |
else {freq=1;} | |
if(freq>mf){mfElem = arr[i]; mf = freq;} | |
if(freq<=lf && arr[i] != arr[i-1]){lfElem = arr[i]; lf = freq;} | |
} | |
return Number(mfElem + "" + lfElem); | |
} | |
module.exports = mostLeastFrq; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment