Skip to content

Instantly share code, notes, and snippets.

@CroModder
Last active September 6, 2017 09:22
Show Gist options
  • Save CroModder/2c9223642ad0706a7757f95d4b6bef5a to your computer and use it in GitHub Desktop.
Save CroModder/2c9223642ad0706a7757f95d4b6bef5a to your computer and use it in GitHub Desktop.
cloudsense
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);
});
});
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;
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);
});
});
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