Answered step by step
Verified Expert Solution
Link Copied!

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

image text in transcribed
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+5.14. Let f (x) be n2x or 2n -n2x or 0 according as 0 5x

Answered: 1 week ago

Question

Only Answer last five questions THERMO-CHEM, INC. Case

Answered: 1 week ago

Question

5. Identify three characteristics of the dialectical approach.

Answered: 1 week ago

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago