Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Memoizing the subset function Your code for this problem should go in a file called Memoizedsubset.py. In Chapter 6 we wrote the subset function that
Memoizing the subset function Your code for this problem should go in a file called Memoizedsubset.py. 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 target. The code that we developed is here: def subset (target, numberList): Returns True if there exists a subset of numberList that adds up to target and returns False otherwise." if target 0: return True elif numberlist[ return False if numberlist[e]> target: return subset (target, numberList[1:]) else: uselt subset (target numberList[0], numberlist[1:]) loseIt subset (target, numberlist[1:]) return useIt or loseIt Unfortunately, this function can become very slow as the inputs become "large". Cut- and-paste into your own file and try running it on the following > subset (1234567, range (2, 100, 2)) (What is that range(2, 100, 2) doing?) After figuring that out, you might want to go out for coffee with your friends, and perhaps a movie, before checking if your program has completed running Alternatively, you can press CTRL-C to stop your program. 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 tuple(range (2, 100, 2)) >>> memoizedSubset (1234567, numberTuple, )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started