So I was doing coderbyte's challenge and I have problems with one: ArrayAdditionI, here's the statement of the problem:
''' Using the Python language, have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return 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. '''
Since I couldn't do it, I researched for a solution and I found this:
def countFor(arr, m, s0):
if len(arr) == 0:
return False
a0 = arr[0]
ar = arr[1:]
sw = s0 + a0
if sw == m:
return True
if countFor(ar, m, sw):
return True
if countFor(ar, m, s0):
return True
return False
def ArrayAdditionI(arr):
m = max(arr)
arr.remove(m)
return str(countFor(arr, m, 0)).lower()
Now, I'm trying to understand what exactly the code does on every loop, I printed out the output for every loop of this list [4, 6, 23, 10, 1, 3]:
Input: [4, 6, 10, 1, 3] 23 0
a0: 4
ar: [6, 10, 1, 3]
sw: 4
Input: [6, 10, 1, 3] 23 4
a0: 6
ar: [10, 1, 3]
sw: 10
Input: [10, 1, 3] 23 10
a0: 10
ar: [1, 3]
sw: 20
Input: [1, 3] 23 20
a0: 1
ar: [3]
sw: 21
Input: [3] 23 21
a0: 3
ar: []
sw: 24
Input: [] 23 24
Input: [] 23 21
Input: [3] 23 20
a0: 3
ar: []
sw: 23
True
and I follow and understand what's going on, until the last three loops, I don't what part of the code makes it go from "Input: [] 23 24" to "Input : [] 23 21" to "Input: [3] 23 20".