Implements clone which duplicates a list. Pay attention, because if there are sublists, they must be duplicated as well. Understand the implementation of the following function, which recursively displays the ids of each node in a list def show_ids(M, level=0): k = M.first_node while k is not None: print(" "*2*level, id(k)) if (str(k.value.__class__.__name__) == str(M.__class__.__name__)): show_ids(k.value, level+1) k = k.next W=L2(Node(10, Node(L2(Node(14, Node(15, Node(L2(Node(16, Node(17))))))), Node(20, Node(30)))) ) show_ids(W) Develop your solution as follows: - First copy the nodes of the current list (self) - Create a new list with the copied nodes - Loop through the nodes of the new list checking the value field - If this field is also a list (use isinstance as in the show_ids function) then it calls clone on that list and substitutes the value. Complete the code: def L4(*args,**kwargs): class L4_class(L): def clone(self): def clone_node(node): return <... YOUR CODE HERE ...> r = <... YOUR CODE HERE...> return r return L4_class(*args,**kwargs) check your code, both lists have to have the same content, but different ids W=L4(Node(10, Node(L4(Node(14, Node(15, Node(L4(Node(16, Node(17))))))), Node(20, Node(30)))) ) W W.__class__.__name__ K = W.clone() k show_ids(W) show_ids(K)
Implements clone which duplicates a list. Pay attention, because if there are sublists, they must be duplicated as well. Understand the implementation of the following function, which recursively displays the ids of each node in a list
def show_ids(M, level=0):
k = M.first_node
while k is not None:
print(" "*2*level, id(k))
if (str(k.value.__class__.__name__) == str(M.__class__.__name__)):
show_ids(k.value, level+1)
k = k.next
W=L2(Node(10, Node(L2(Node(14, Node(15, Node(L2(Node(16, Node(17))))))), Node(20, Node(30)))) )
show_ids(W)
Develop your solution as follows:
- First copy the nodes of the current list (self)
- Create a new list with the copied nodes
- Loop through the nodes of the new list checking the value field
- If this field is also a list (use isinstance as in the show_ids function) then it calls clone on that list and substitutes the value.
Complete the code:
def L4(*args,**kwargs):
class L4_class(L):
def clone(self):
def clone_node(node):
return <... YOUR CODE HERE ...>
r = <... YOUR CODE HERE...>
return r
return L4_class(*args,**kwargs)
check your code, both lists have to have the same content, but different ids
W=L4(Node(10, Node(L4(Node(14, Node(15, Node(L4(Node(16, Node(17))))))), Node(20, Node(30)))) )
W
W.__class__.__name__
K = W.clone()
k
show_ids(W)
show_ids(K)
Step by step
Solved in 5 steps with 4 images