Implement deep_map, which takes a function with f applied to any element within link f and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Implement deep_map, which takes a function with f applied to any element within link f and a link. It returns a new linked list with the same structure as link, but any Link instance contained in link. The deep_map function should recursively apply fn to each of that Link 's elements rather than to that Link itself. Hint: You may find the built-in isinstance function useful. def deep_map (f, link): """Return a Link with the same structure as link but with fn mapped over its elements. If an element is an instance of a linked list, recursively apply f inside that linked list as well. >>>s = Link (1, Link(Link (2, Link(3)), Link(4))) >>>print_link(s) <1 <2 3> 4> >>>print link (deep_map (lambda x: x* x, s)) <1 <4 9> 16> >>>print_link (s) # unchanged <1 <2 3> 4> >>> t = Link (s, Link (Link (Link (5)))) >>> print_link (t) < <1 <2 3> 4> < <5>>> >>> print link (deep_map (lambda x: 2 * x, t)) < <2 <4 6> 8> < <10>>> "*** YOUR CODE HERE ***" Implement deep_map, which takes a function with f applied to any element within link f and a link. It returns a new linked list with the same structure as link, but any Link instance contained in link. The deep_map function should recursively apply fn to each of that Link 's elements rather than to that Link itself. Hint: You may find the built-in isinstance function useful. def deep_map (f, link): """Return a Link with the same structure as link but with fn mapped over its elements. If an element is an instance of a linked list, recursively apply f inside that linked list as well. >>>s = Link (1, Link(Link (2, Link(3)), Link(4))) >>>print_link(s) <1 <2 3> 4> >>>print link (deep_map (lambda x: x* x, s)) <1 <4 9> 16> >>>print_link (s) # unchanged <1 <2 3> 4> >>> t = Link (s, Link (Link (Link (5)))) >>> print_link (t) < <1 <2 3> 4> < <5>>> >>> print link (deep_map (lambda x: 2 * x, t)) < <2 <4 6> 8> < <10>>> "*** YOUR CODE HERE ***"
Expert Answer:
Answer rating: 100% (QA)
To solve the problem we can define a recursive function deepmap that takes a function f and a Link i... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Identify the practice in which a hacker locks out users and encrypts their personal computer files and data, hol ding it until they agree to pay money to the attacker. a. Dumpster Diving b....
-
Let A, B be sets. Define: (a) the Cartesian product (A B) (b) the set of relations R between A and B (c) the identity relation A on the set A [3 marks] Suppose S, T are relations between A and B, and...
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
DFI of real sequences with odd harmonics only Let x(n) be an N -point real sequence with N-point DFT X(k) (N even). In addition, x(n) satisfied the following symmetry property: x(n + N/2) = -x(n) n =...
-
XYZ Corporation produces a commercial product that is in great demand by consumers on a national basis. Unfortunately, near the plant where it is produced there is a large population of dove-tail...
-
In the previous problem, assume that it costs $300 to place an order. The holding cost per DVD player held in inventory per year is $15. The cost each time a customer orders a DVD player that is not...
-
Current assets include: a. Inventory, receivables and prepayments b. Inventory, receivables and accruals c. Inventory, payables and prepayments d. Inventory, payables and accruals
-
Palomar Chemical Company produces three products: ethylene, butane, and ester. Each of these products has high demand in the market, and Palomar Chemical is able to sell as much as it can produce of...
-
(4 points) Evaluate each of the following products. 3 (a) (k + 3) = k=1 3 (b) II k=1 k k+1 || 4 (c) II k=0 6 (d) II k=2 6 (e) II k=2 k(k + 1) (k + 2)(k + 3) k(k + 1) (k + 2)(k + 3) k(k + 2) (k-1)(k+1)
-
The 450-room Hotel Fernando is a four-star full-service hotel in San Petresco. It is highly regarded among both locals and tourists. Its service standards and ameni- ties complement its exquisite...
-
Question 4: A plant asset acquired on January 1, 2015, at a cost of SR240,000 and has an estimated useful life of 5 years. The residual value is estimated to be SR50,000 at the end of the asset's...
-
Black Diamond Company produces snowboards. Each snowboard requires 2 pounds of carbon fiber. Management reports that 6,400 snowboards and 7,400 pounds of carbon fiber are in inventory at the...
-
Explain to me how to figure out how to write the formulafor Zn(NO2)2 (which is a transition metal and a polyatomic ion).Why is it not zinc (II) nitrite? Nitrite is a negative chargeand there are two...
-
discuss the impact of project portfolio management on resource allocation, strategic alignment, and prioritization of projects within a multi-project environment ?
-
Education is an effective tool to learn skills relating to behaviour management. Where might you access this education both internally and externally to an organisation? When would this need be...
-
randy uncle left him an inheritance that will be distributed in the following way 45,000 next year 40,000 in two years and 25,000 in three years if he can earn 7% in his investments how much are the...
-
*Write your thoughts and if you agree or disagree with this discussion post below, Opinions or predictions as well. add onto it and talk about the good points made. My fictional character is Ian...
-
a. Show that the expansion of q(x) in ascending powers of x can be approximated to 10 2x + Bx 2 + Cx 3 where B and C are constants to be found. b. Find the percentage error made in using the series...
-
If the set of stack operations included a MULTI-PUSH operation, which pushes k items onto the stack, would the O(1) bound on the amortized cost of stack operations continue to hold?
-
Prove that Var [aX] = a 2 Var [X] from the definition (C.27) of variance.
-
Give asymptotically tight bounds on the following summations. Assume that r ? 0 and s ? 0 are constants. a. b. c. . in k=1
-
When is the cost of capital equal to the cost of equity? Can the cost of capital be equal to the cost of debt?
-
How do you explain the fact that rates of return required by investors may be identical for two groups of totally different activities (oil and IT services for example) as long as they have the same .
-
Will liquidity premiums tend to rise or fall during a crash? Why?
Study smarter with the SolutionInn App