Problem 2.2: Linked Lists as Strings (100pts)
Kirito and Eugeo like different ways of displaying the linked list structure in Python. While Kirito likes box and pointer diagrams, Eugeo prefers a more futuristic way. Write a function make_to_string
that returns a function that converts the linked list to a string in their preferred style.
def make_to_string(front, mid, back, empty_repr):
""" Returns a function that turns linked lists to strings.
>>> kirito_to_string = make_to_string("[", "|-]-->", "", "[]")
>>> eugeo_to_string = make_to_string("(", " . ", ")", "()")
>>> lst = Link(1, Link(2, Link(3, Link(4))))
>>> kirito_to_string(lst)
'[1|-]-->[2|-]-->[3|-]-->[4|-]-->[]'
>>> kirito_to_string(Link.empty)
'[]'
>>> eugeo_to_string(lst)
'(1 . (2 . (3 . (4 . ()))))'
>>> eugeo_to_string(Link.empty)
'()'
"""
"*** YOUR CODE HERE ***"
Hint1: You can convert numbers to strings using the
str
function, and you can combine strings together using+
.Hint2: You can use either loop or recursion and the answer is not complicated.