On 4/23/2012 1:24 PM, Steven D'Aprano wrote:
Nestor wrote:
The other day a colleague of mine submitted this challenge taken from some website to us coworkers:
Have the function ArrayAddition(arr) take the array of numbers stored in arr and print true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise print false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should print true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.
By the way, your solution is wrong. Consider this sample data:
-2, -5, 0, -1, -3
In this case, the Haskell solution should correctly print true, while yours will print false, because you skip the empty subset. The empty sum equals the maximum value of the set, 0.
The ease at which people can get this wrong is an argument in favour of a standard solution.
I call it an argument for writing tests first, starting with the most simple and trivial case(s). -- Terry Jan Reedy