Although I love these kind of problems I am aware that they are problematic in an interview situation.
Reginald Braithwaite eloquently described some of the more problematic points in Carnac the magnificient
Although I love these kind of problems I am aware that they are problematic in an interview situation.
Reginald Braithwaite eloquently described some of the more problematic points in Carnac the magnificient
(function problem1(){ | |
var list = [1, 2, 3, 4, 5]; // sum is 15 | |
function sumFor(aList) { | |
var sum = 0; | |
for (var index = 0; index < aList.length; index++) { | |
sum += aList[index]; | |
} | |
return sum; | |
} | |
function sumWhile(aList) { | |
var sum = 0, index = 0; | |
while (index < aList.length) { | |
sum += aList[index++]; | |
} | |
return sum; | |
} | |
function sumRecursion(aList) { | |
if (aList.length == 0){ | |
return 0; | |
} else { | |
return aList[0] + sumRecursion(aList.slice(1)); | |
} | |
} | |
function sumReduce(aList) { | |
return aList.reduce(function(acc, el){ return acc + el}, 0); | |
} | |
console.log(sumFor(list)); | |
console.log(sumWhile(list)); | |
console.log(sumRecursion(list)); | |
console.log(sumReduce(list)); | |
})(); | |
(function problem2(){ | |
function zip(as, bs){ | |
var result = [], index = 0; | |
while (index < as.length && index < bs.length) { | |
result.push(as[index]); | |
result.push(bs[index]); | |
index++; | |
} | |
return result; | |
}; | |
console.log(zip(['a','b','c'], [1, 2, 3])); | |
})(); | |
(function problem3(){ | |
function fibonacci(n){ | |
var result = []; | |
var a = 0, b = 1; | |
while (n > 0) { | |
result.push(a); | |
var tmp = a; | |
a = b; | |
b = tmp + a; | |
n--; | |
} | |
return result; | |
}; | |
console.log(fibonacci(100)); | |
})(); | |
(function problem4(){ | |
function significateDigit(n) { | |
return parseInt(("" + n).split("")[0]); | |
} | |
function decreasing(a, b) { | |
return significateDigit(b) - significateDigit(a); | |
} | |
function toString(element) { | |
return "" + element; | |
} | |
function concat(accumalator, element){ | |
return accumalator + element; | |
} | |
function largestNumber(aList){ | |
return parseInt(aList.sort(decreasing).map(toString).reduce(concat)); | |
}; | |
console.log(largestNumber([50, 2, 1, 9])); | |
})(); | |
(function problem5(){ | |
var options = ["-", "", "+"]; | |
function multiOptions(n) { | |
if (n == 0) { | |
return [[]]; | |
} else { | |
var result = []; | |
multiOptions(n - 1).forEach(function(candidate){ | |
console.log() | |
options.forEach(function(option){ | |
var copy = candidate.slice(0); | |
copy.push(option); | |
result.push(copy); | |
}); | |
}); | |
return result; | |
} | |
}; | |
function zip(as, bs){ | |
var result = [], index = 0; | |
while (index < as.length && index < bs.length) { | |
result.push(as[index]); | |
result.push(bs[index]); | |
index++; | |
} | |
return result; | |
}; | |
function expression(candidate) { | |
var numbers = candidate.map(function(_, index){ return index + 1}); | |
var expr = zip(numbers, candidate).reduce(function(acc, element){ | |
return acc + element; | |
}) + (numbers[numbers.length - 1] + 1); | |
return expr; | |
} | |
console.log(multiOptions(8) | |
.map(expression) | |
.map(function(expr){ | |
return [expr, eval(expr)]; | |
}) | |
.filter(function(tuple){ | |
return tuple[1] == 100 | |
}) | |
.map(function(tuple){ | |
return tuple[0]; | |
})); | |
})(); |