Algorithm problems
# Option 1 _.reduce = function(collection, iterator, accumulator) { var result = accumulator === undefined ? collection[0] : accumulator; collection = accumulator === undefined ? collection.slice(1) : collection; _.each(collection, function(item){ result = iterator(result, item); }); return result; }; # Option 2 _.reduce = function(collection, iterator, accumulator) { _.each(collection, function(item){ if(accumulator === undefined){ accumulator = item; }else{ accumulator = iterator(accumulator, item); } }) return accumulator; };
Reduces an array or object to a single value by repetitively calling iterator(accumulator, item) for each item. accumulator should be the return value of the previous iterator call. You can pass in a starting value for the accumulator as the third argument to reduce. If no starting value is passed, the first element is used as the accumulator, and is never passed to the iterator. In other words, in the case where a starting value is not passed, the iterator is not invoked until the second element, with the first element as it's second argument. Example: var numbers = [1,2,3]; var sum = _.reduce(numbers, function(total, number){ return total + number; }, 0); // should be 6 var identity = _.reduce([5], function(total, number){ return total + number * number; }); // should be 5, regardless of the iterator function passed in No accumulator is given so the first element is used.
def gcdRecur(a, b): # Your code here if b == 0: return a return gcdRecur(b, a % b)
The greatest common divisor of two positive integers is the largest integer that divides each of them without remainder. For example, gcd(2, 12) = 2 gcd(6, 12) = 6 gcd(9, 12) = 3 gcd(17, 12) = 1 A clever mathematical trick (due to Euclid) makes it easy to find greatest common divisors. Suppose that a and b are two positive integers: If b = 0, then the answer is a Otherwise, gcd(a, b) is the same as gcd(b, a % b)
def gcdIter(a, b): testValue = min(a, b) # Keep looping until testValue divides both a & b evenly while a % testValue != 0 or b % testValue != 0: testValue -= 1 return testValue
The greatest common divisor of two positive integers is the largest integer that divides each of them without remainder. For example, gcd(2, 12) = 2 gcd(6, 12) = 6 gcd(9, 12) = 3 gcd(17, 12) = 1 Write an iterative function, gcdIter(a, b), that implements this idea. One easy way to do this is to begin with a test value equal to the smaller of the two input arguments, and iteratively reduce this test value by 1 until you either reach a case where the test divides both a and b without remainder, or you reach 1.
function chunk(arr, size) { // Break it up. var result = []; for(var i = 0; i < arr.length; i) { result.push(arr.slice(i, i+=size)) } return result; }
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array. chunk(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]].
function findEvenIndex(arr) { //Code goes here! for (var i = 0; i < arr.length; i++){ var left = arr.slice(0, i).reduce(total); var right = arr.slice(i + 1).reduce(total); var lef if(left.reduce(total) } } function total(previousValue, currentValue, currentIndex, array) { return previousValue + currentValue; }
You are given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1. For example: Let's say you are given the array {1,2,3,4,3,2,1}: Your function will return the index 3, because at the 3rd position of the array, the sum of left side of the index ({1,2,3}) and the sum of the right side of the index ({3,2,1}) both equal 6.