Write a function interleaved_sum, which takes in a number n and two one-argument functions: odd_term and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a function interleaved_sum, which takes in a number n and two one-argument functions: odd_term and even_term. It applies odd_term to every odd number and even_term to every even number from 1 to n including n and returns the sum. For example, executing interleaved_sum (5, lambda x: x, lambda x: x x x) returns 1 + 2^2 + 3 + 4^2 + 5 = 29. Implement this function without using any loops or directly testing if a number is odd or even -- no modulos ( % ) allowed! Hint: You will need to use a helper function to iterate through each term: • It may help to start summing from the first term up to n (if you start from 1, you have to apply odd_term first. After, you'll be able to tell whether every subsequent term is odd or even) • How can you keep track of which function to use? Is it possible to swap the two functions in the helper function's parameters during each recursive call? def interleaved_sum(n, odd_term, even_term): """Compute the sum odd_term(1) + even_term(2) + odd_term(3) + to n. >>> identity = lambda x: x >>> square = lambda x: x * X >>> triple = lambda x: x * 3 >>> interleaved_sum (5, identity, square) # 1 + 2^2 + 3 + 4^2 + 5 29 >>> interleaved_sum (5, square, identity) # 1^2 + 2 + 3^2 + 4 + 5^2 41 >>> interleaved_sum (4, triple, square) # 1 * 3 + 2^2 + 3 * 3 + 4^2 32 >>> interleaved_sum (4, square, triple) # 1^2 + 2 * 3 + 3^2 + 4 * 3 28 ..., up >>> from construct_check import check >>> check (HW_SOURCE_FILE, 'interleaved_sum', ['While', 'For', 'Mod']) # ban loops and % True |||||| "*** YOUR CODE HERE ***" Write a function interleaved_sum, which takes in a number n and two one-argument functions: odd_term and even_term. It applies odd_term to every odd number and even_term to every even number from 1 to n including n and returns the sum. For example, executing interleaved_sum (5, lambda x: x, lambda x: x x x) returns 1 + 2^2 + 3 + 4^2 + 5 = 29. Implement this function without using any loops or directly testing if a number is odd or even -- no modulos ( % ) allowed! Hint: You will need to use a helper function to iterate through each term: • It may help to start summing from the first term up to n (if you start from 1, you have to apply odd_term first. After, you'll be able to tell whether every subsequent term is odd or even) • How can you keep track of which function to use? Is it possible to swap the two functions in the helper function's parameters during each recursive call? def interleaved_sum(n, odd_term, even_term): """Compute the sum odd_term(1) + even_term(2) + odd_term(3) + to n. >>> identity = lambda x: x >>> square = lambda x: x * X >>> triple = lambda x: x * 3 >>> interleaved_sum (5, identity, square) # 1 + 2^2 + 3 + 4^2 + 5 29 >>> interleaved_sum (5, square, identity) # 1^2 + 2 + 3^2 + 4 + 5^2 41 >>> interleaved_sum (4, triple, square) # 1 * 3 + 2^2 + 3 * 3 + 4^2 32 >>> interleaved_sum (4, square, triple) # 1^2 + 2 * 3 + 3^2 + 4 * 3 28 ..., up >>> from construct_check import check >>> check (HW_SOURCE_FILE, 'interleaved_sum', ['While', 'For', 'Mod']) # ban loops and % True |||||| "*** YOUR CODE HERE ***"
Expert Answer:
Answer rating: 100% (QA)
To implement the interleavedsum function without using loops or modules you can create a h... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
On 24 April 2022 a 2M worth of jewellery was stolen from Adam and Sarah Knight in their London home. On 25 April 2022, Adam and Sarah placed an advertisement on a national London newspaper offering...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
3. (15 pts.) The random variables X and Y have joint density function fxx(x, y) x-?y?, x > 1, y > 1. Compute the cdf and pdf of the random variable U = XY.
-
Use nodal analysis to find Io and I1 in the circuit shown below. Figure -) 12 V 12 mA
-
In 2013, Emma purchased an automobile, which she uses for both business and personal purposes. Although Emma does not keep records as to operating expenses (e.g., gas, oil, and repairs), she can...
-
The figure below shows the results of a survey in which 1000 adults were asked how much they spend in preparation for personal travel each year. Make a frequency distribution for the data. Then use...
-
Weston Corporation has an internal audit department operating out of the corporate headquarters. Various types of audit assignments are performed by the department for the eight divisions of the...
-
The following is a sequence of eight numbers: 01 2 3 4 5 6 7 350 100 250 400 300 50 200 150 Use QuickSort to sort the numbers in the sequence in ascending order. Show the result of each step by...
-
The general ledger of Zips Storage at January 1, 2024, includes the following account balances: Accounts Debits Credits Cash $26,300 17,100 Accounts Receivable Prepaid Insurance Land 15,400 165,000...
-
1. Indicate whether f = O(g), f = (g), or f = e(g). f = n0, g=n20 f=2", g = (3/2)" f = 10(n), g=nlog, (10) f = 202 (m) 9= n 2. Determine the big-O complexity of func1 in terms of M and N. A and B...
-
3. You have the following payoff table for a decision analysis problem. Payoffs are in units of thousands of dollars of profit. STATE OF NATURE ALTERNATIVES S S Sj St d 35 50 45 40 d 45 40 30 60 d 20...
-
A 10 kilogram body initially moving with a velocity of 10 meters per second makes a head-on collision with a 15 kilogram body initially at rest. The two objects stick together. What is the velocity...
-
An annual coupon bond has a maturity of eleven years, a face value of $1000, and a coupon rate of 2%. The current market price of the bond is $810.25. Use a grid search to find the yield to maturity...
-
You are wondering how best to allocate these costs into cost pools. You settle on forming a total of five pools. Volume related costs, allocated to products using labor hours. Cost related to...
-
selling price $922,000.00 $1,210,000.00 $1,240,000.00 $915,000.00 $1,075,000.00 $1,280,000.00 $1,100,000.00 $1,400,000.00 $1,600,000.00 $835,000.00 $955,000.00 $1,280,000.00 $1,200,000.00...
-
She helps me cleaning the classroom. What is the object pronoun here? a. She b. me c. my d. None of the mentioned pronoun above belongs to an object pronoun.
-
What is EBIT/eps analysis? What information does it provide managers?
-
Find both first partial derivatives. z = 2yx
-
In Exercises, evaluate the definite integral. Use a graphing utility to verify your result. X [*cos = dx
-
In Exercises evaluate the definite integral. Use a graphing utility to verify your result. |x - 4x + 3| dx
-
A prototype can be ___________. (a) Complete system with partial requirements (b) Working model (c) Full-fledged system (d) All of these
-
_____________ is also used as requirement finding and feasibility testing tool. (a) Prototype (b) Reports (c) System (d) Procedure
-
Which of the following systems development approach is adopted if information requirements are not well defined? (a) SDLC (b) Prototype (c) Structured approach (d) Agile
Study smarter with the SolutionInn App