Question: 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

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) >>>print link (deep_map (lambda x: x* x, s)) >>>print_link (s) # unchanged >>> t = Link (s, Link (Link (Link (5)))) >>> print_link (t) < < >> >>> print link (deep_map (lambda x: 2 * x, t)) < < >> "*** YOUR CODE HERE ***"
Step by Step Solution
There are 3 Steps involved in it
To solve the problem we can define a recursive function deepmap that takes a function f and a Link i... View full answer
Get step-by-step solutions from verified subject matter experts
