I’m a bit confused on how to approach this problem. I know what I want to do but can’t wrap my head on how to logically solve this problem.
say I have a list:
numlist = [10,4]
and I have the following values in another list:
datalist = [10,5,4,2,1]
how do I break down the numbers in numlist using only numbers from datalist?
An example of an answer would be:
10, 4 10, 2,2 10, 2,1,1 10, 1,1,1,1 5,5, 4 5,5, 2,2
…and so on.
I understand how to do this vaguely. make a for loop, for each entry in the list and compare if it can be divided by the datalist, and if so print the result. I think I need recursions which is where I’m having trouble understanding.
here’s my code so far (I have some print statements for troubleshooting):
def variableRecursion(self, solutionList):
#solution list contrains ['red', 2, 'green', 1] which means 2 reds(value 4) and 1 green(value 2)
#adding fake lookup list for now, in real code, I can use real data because I am reversing the order
list = [('red', 4), ('green', 2), ('blue', 1) ]
for x1, x2 in zip(solutionList[::2], solutionList[1::2]):
for x in list:
for y1, y2 in zip(x[::2], x[1::2]):
#print x1, x2
keyName = x1
keyShares = x2
keyValue = lookup.get_value(x1)
if ((keyValue%y2) == 0) and (keyValue != y2):
tempList = []
#print 'You can break ', keyName, keyValue, ' with ', y1, y2, ' exactly ', keyValue/x2, ' times.'
#newKeyShares = keyShares - 1
for a1, a2 in zip(solutionList[::2], solutionList[1::2]):
#print a1, a2
print 'You can break ', keyName, keyValue, ' with ', y1, y2, ' exactly ', keyValue/y2, ' times.'
newKeyShares = keyShares - 1
print 'there is a match', a1, a2, ' but we will change the shares to ', newKeyShares
print a1
if (a1 == keyName):
print 'a'
tempList.append([(keyName), (newKeyShares)])
elif (a1 == y1):
print 'b'
tempList.append([(y1), (a2+keyValue/y2)])
else:
print 'c'
try:
tempList.append([(y1), (a2+keyValue/y2)])
except e:
tempList.append([(a1), (a2)])
print tempList
appendList.appendList(tempList)
tempList = []
#exit()
#print solutionList
This problem is very similar to Problem 31 of Project Euler: “How many different ways can £2 be made using any number of coins?”. Only in your example, you are asking to enumerate all the ways you can add up numbers to get 10 and 4.
The best way to approach the problem is to first try breaking up only a single number. Let’s look at the possible breakups for five, using numbers [5,4,2,1]:
The following python code will give you a list of these combinations:
This code assumes that different orderings of otherwise identical answers should be treated as distinct. For example, both [4,1] and [1,4] will appear as solutions when you split 5. If you prefer, you can constrain it to only have answers that are numerically ordered (so [1,4] appears but not [4,1])
Now you can use this to find the possible splits for 10, and the possible splits for 4, and combine them:
edit: As noted in the comments, calling possibleSplits with a value of 100 is very slow – upwards of ten minutes and counting. The reason this occurs is because possibleSplits(100) will recursively call possibleSplits(99), possibleSplits(98), and possibleSplits(96), each of which call three possibleSplits of their own, and so on. We can approximate the processing time of possibleSplits(N) with datalist[1,2,4] and large N as
processingTime(N) = C + processingTime(N-1) + processingTime(N-2) + processingTime(N-4)
For some constant time C.
So the relative time for possibleSplits is
Supposing possibleSplits(5) takes 7 ns, possibleSplits(100) takes about 3 * 10^9 years. This is probably an unsuitably long time for most practical programs. However, we can reduce this time by taking advantage of memoization. If we save the result of previously calculated calls, we can get linear time complexity, reducing possibleSplits(100) to 100 ns.
The comments also noted that the expected value of orderedPossibleSplits(100) has about 700 elements. possibleSplits(100) will therefore have a much much larger number of elements, so it’s impractical to use it even with memoization. Instead, we’ll discard it and rewrite orderedPossibleSplits to use memoization, and to not depend on possibleSplits.
On my machine, orderedPossibleSplits(100, [1,2,4]) takes about ten seconds – much improved from our original three billion year run time.