Question: Python3 Problem __repr__: Implement a function that returns the representation of a particular linked list as a string. We should be able to use the
Python3
Problem __repr__: Implement a function that returns the representation of a particular linked list as a string. We should be able to use the representation as an expression in Python to create the same linked list.
Problem __str__:
Implement a function that returns a "nice" string that represents the linked list. In particular, we have the values in the linked list separated by arrows (->). For example, calling str(Link(1, Link(2)) should result in the string '1 -> 2'. Note the spaces between the arrows and the elements of the linked list. If the element in the linked list happens to be another linked list, we will use parentheses to make the elements of the linked list clear.
For instance, str(Link(Link(1, Link(2)), Link(3)) evaluates to '(1 -> 2) -> 3'.
class Link(object): empty = ()
def __init__(self, first, rest=empty): assert rest is Link.empty or isinstance(rest, Link)
self.first = first self.rest = rest
def __repr__(self): """ >>> Link(None) Link(None)
>>> Link(1, Link(2, Link(3))) Link(1, Link(2, Link(3)))
>>> Link(Link(1, Link(2)), Link(3, Link(4))) Link(Link(1, Link(2)), Link(3, Link(4)))
>>> Link(Link(Link(Link('Wow')))) Link(Link(Link(Link('Wow')))) """ """YOUR CODE GOES HERE"""
def __str__(self): """ >>> str(Link('Hello')) 'Hello'
>>> str(Link(1, Link(2))) '1 -> 2'
>>> print(Link(1 / 2, Link(1 // 2))) 0.5 -> 0
>>> str(Link(Link(1, Link(2, Link(3))), Link(4, Link(5)))) '(1 -> 2 -> 3) -> 4 -> 5'
>>> print(Link(Link(Link(Link('Wow'))))) (((Wow)))
>>> print(Link(Link('a'), Link(Link('b'), Link(Link('c'))))) (a) -> (b) -> (c) """ """YOUR CODE GOES HERE""" Copy pastable code: https://paste.ee/p/L4Fke
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
