Programming Questions

• Popular Tags
My code (and the logic behind it) is very very simple: LOGIC: a) find the biggest one b) add all that remains if b >= a then there is always at least one pair (or all ) of numbers (in the remained) that if you sum them will be the biggest one. if < biggest there is no pair that will make the biggest if you sum them. CODE ``` def ArrayAddition(arr): biggest = sorted(arr).pop() sum_remains = sum(sorted(arr)[:-1]) if sum_remains < biggest: return "false" return "true" ``` The logic is there... Test it with any combination of numbers and you will see its true. The code as well follows the logic. So where is the fault? Why Coderbyte editor says it's all wrong?
kostasdaskalos posted this question on 7/15/14 |
• +
• 1
• -
• Sorry wrong example: [10, 6, 3, 2]
• +
• 0
• -
• I think this example may help as well. Say the array is [10,1,3,12]. Even though 10+1+3 = 14 which is greater than 12, there are no pairs in the array that add up to the largest number, 12.
• +
• 0
• -
• You have a logic bug. Since you seem to have issues with English I will try to show you by formalizing what you implement versus what you should implement. What you do is: a = find_max() b = sum_array - a if (b > a) return true
• +
• 0
• -
• For the example your code would give true when it is actually false
• +
• 0
• -
• That's not always going to be true. Let's say for example the array was [10, 1, 2, 3, 12]. Your program would return false but there is a way to obtain 12, 10 + 2. Read the question again. It said any combination of the numbers not just the remaining!
• +
• 0
• -
• BTW you clearly did not try my code on the array you said...
• +
• -2
• -
• I am sorry but you are wrong. Biggest number in your example is 12 and the remaining numbers are 10, 1, 2, 3 . Add all the remaining numbers? 10+1+2+3=16. And 16 > 12 so there is (at least) one pair that will make the biggest 12. Probably because English is not my native language I didnt make clear my logic. I said: there will be *at least* one pair that if you add them it will be the biggest. I didnt say that if you add *all* the remaining will be the biggest... It can be either of these cases. A pair that adds up to the biggest *or* all the remaining adds up to the biggest.