Question: *** MUST be in Python language a) Write a generator function called gen_powers ( x, n ) that generates the first n powers of x

*** MUST be in Python language

a) Write a generator function called gen_powers(x, n) that generates the first n powers of x, beginning with 1 (x0, by definition). So, gen_powers(x, 10) should generate this sequence: 1, x, x2, x3,..., x9. To get credit, your generator should NOT use the math.pow() function, or any other function, and should NOT compute xi from scratch every time it needs to output the next value. Keep the previous output value xi between calls.

b) Write a Python expression using sum(), the gen_powers(2, ...) generator call (with parameter x==2), and a generator expression that computes this sum:

y = *** MUST be in Python language a) Write a generator function called where n is a local positive int variable.

Remember that the generator outputs the first n numbers from the powers sequence. To get credit you must use gen_powers(2, ...) (param x==2), as indicated, and not just write sum(gen_powers(1/2, ...)). After the code, write what the value of the sum is with 30 terms.

c) Use the gen_powers(2, ...) generator call (with parameter x==2), the functools.reduce(), map() functions, and lambda expressions to compute the sum from part b). To get credit do NOT use the sum() function. Assume n is a positive local int variable.

Hint: first map the sequence of 2i to sequence of 1/2i

20 21 22 2n

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!