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:
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
-
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 =...
-
a. Explain why equity in income of investees appears as a subtraction when net income in converted to cash flow from operations. b. Compute the amount of cash received from investees as dividends...
-
In the experiment in Exercise 8, what does it mean in terms of fish behavior if there is an interaction between presence in sample 1 and presence in sample 2? Between presence in sample 1 and...
-
For each of the following situations, calculate the \(z\)-statistic \((z)\), make a decision about the null hypothesis (reject, do not reject), and indicate the level of significance \((p>.05, p
-
Differentiate between a free trade area and a common market. Explain the marketing implications of the differences.
-
Sing Ltd is investing $600 million in a new manufacturing facility which will increase their capacity by 75%. The resultant present value of future net cash flows is estimated to be $850 million....
-
Think of a large factory that moves jobs from one workstation to another using fork trucks. When a workstation has filled up a bin that is ready for a fork truck, it sends out a signal to a central...
-
Nottebart Corporation has outstanding 10,000 shares of 100 par value, 6% preference shares and 60,000 shares of 10 par value ordinary shares. The preference shares were issued in January 2015, and no...
-
On May 10, 2015, Cosmo Co. enters into a contract to deliver a product to Greig Inc. on June 15, 2015. Greig agrees to pay the full contract price of 2,000 on July 15, 2015. The cost of the goods is...
-
Lansbury Company purchases equipment on January 1, Year 1, at a cost of 518,000. The asset is expected to have a service life of 12 years and a residual value of 50,000. Instructions (a) Compute the...
-
Frozen Delight, Inc. charges an initial franchise fee of \($75\),000 for the right to operate as a franchisee of Frozen Delight. Of this amount, \($25\),000 is collected immediately. The remainder is...
-
Pengo Company owns land that it purchased at a cost of 400 million in 2013. The company chooses to use revaluation accounting to account for the land. The lands value fluctuates as follows (all...
-
During a meeting of the management committee of Edsel Corporation, a number of proposals are made to alleviate its weak cash position and improve income. Evaluate and comment on both the immediate...
-
g(x) = x 5 5x 6 a. Show that g(x) = 0 has a root, , between x = 1 and x = 2. b. Show that the equation g(x) = 0 can be written as x = (px + q) 1/r , where p, q and r are integers to be found. The...
-
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
-
What is the relation between degrees Fahrenheit and degrees Rankine? And the relation between degrees Celsius and Kelvin?
-
State Newton's second law as you would apply it to a control mass.
-
Define a 1-pound force in terms of the acceleration it will give to a 1-pound mass. Give a similar definition for a newton in the SI system.
Study smarter with the SolutionInn App