Question: In Chapter 6 we wrote the subset function that determines whether or not there exists a subset of the numberList that adds up to the
In Chapter we wrote the subset function that determines whether or not there exists a subset of the numberList that adds up to the target. The code that we developed is here:
def subsettarget numberList:
Returns True if there exists a subset of numberList that adds
up to target and returns False otherwise.
if target : return True
elif numberList : return False
if numberList target: return subsettarget numberList:
else:
useIt subsettarget numberList numberList:
loseIt subsettarget numberList:
return useIt or loseIt
Unfortunately, this function can become very slow as the inputs become "large". Cutandpaste into your own file and try running it on the following
subset range
What is that range doing?
The subset function is slow because it does a large number of redundant calculations. Your task now is to write a much faster memoized version called memoizedSubset. As we saw with the change function that we memoized in the text, we will want to give this new function a tuple input instead of a list. Our new function will look like this: memoizedSubset target, numberTuple, memo
After you've written it try the same inputs with this new, faster function:
numberTuple tuplerange
memoizedSubset numberTuple,
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
