It is funny that our development team held a small-scale coding challenge. The challenge question is like:

Take an array of the form:

[[1, 4, 6, …],

[3, 6, …],

[1, 1, 8, 5, …]]and sum each row, then multiply these sums. For example above the expected result would be (1+4+6) * (3+6) * (1+1+8+5)

[1, 1, 8, 5, …]]and sum each row, then multiply these sums. For example above the expected result would be (1+4+6) * (3+6) * (1+1+8+5)

It is free to choose any programming language to solve that. Here is my answer: (Using PHP)

<?php

$array = array(

array(1,2,3,4),

array(3,2,8),

array(4,6)

);function add($a,$b){

return $a+$b;

}

function multiply($a,$b){

return $a*$b;

}

$array = array(

array(1,2,3,4),

array(3,2,8),

array(4,6)

);function add($a,$b){

return $a+$b;

}

function multiply($a,$b){

return $a*$b;

}

function run($arr){

$return_arr=array();

$return_arr = array_map(function($row){

return array_reduce($row,”add”);

},$arr);

return array_reduce($return_arr,”multiply”,1);

}

echo run($array);

?>

Basically, I used array_map to apply callback to each of the elements in the original array in order to get the sum-up result for each row. During this process, I applied array_reduce() which can reduce the array to a single value using a callback function “add”.

After that I should get an array which stores the sum-up result for each row. For example, if the original array is [[1,2,3],[3,5],[7,3]], then at the end of this stage, we can get the outcome like [6,8,10].

Then the rest task is to multiply iteratively for this array. So, what I did was using array_reduce again, but this time I changed the callback to multiply() and also added an additional parameters as the initial value.

Note that if the initial value is lacking, then the output would be 0. Because at the beginning of the array_reduce execution, it would do something like 0*…. So no matter what the value we have, the outcome would be zero… So put in the third param here as the initial value. Then that’s it. The output would be 480 if using the example above

Advertisements

April 26, 2012 at 9:53 pm

Here i have used three array values. Summed up individually and then multiplied with each other.Hope this is what u want.

I think its the simplest way 🙂

April 26, 2012 at 9:59 pm

April 26, 2012 at 10:38 pm

kcube,

Thank you so much for your reply! 🙂

Yes, your way is correct. Actually, my original method is to use foreach to loop through array and use array_sum() in php to get the sum value for each sub-array. Then I found that in PHP, we have something like .map() and .reduce() in javascript, which are array.map() and array.reduce(). I think these two methods are super cool. Since I am not a super expert, so I really did not know these two functions. 🙂

April 26, 2012 at 10:50 pm

this is an simple example for array_map()

this will an output of cube of the number:

Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )

this function is used to map an array to an function where u can define the function for calculation

April 26, 2012 at 10:59 pm

hmm…Interesting!!

By the way, here is one interview question in our company. Please see this link:http://jsfiddle.net/zhujy_8833/UpT8p/, you can see my answer for this question.

Let me know what you will do for this question.(Actually, I got some tricky answers from our development team, I will tell you when you shared your answer:-))