#Linked List recursion practice
class Link:
def __init__(self, value, next=None):
self.value = value
self.next = next
def __repr__(self):
return str(self.value) + "->(" + str(self.next) + ")"
#problem 1
#Implement a function that takes a string and uses recursion to make a linked list of characters, or 1 byte long strings. Hint use slice indexes on the string !
"""
x = "Hello world"
=> None
charlist(x)
=> H->(e->(l->(l->(o->( ->(w->(o->(r->(l->(d->(None)))))))))))
"""
def charlist(string):
if len(string) == 0:
return None
else:
return Link(string[0], charlist(string[1:]))
#problem 2
#implement a function that takes a linked list of characters and uses recursion to concat those characters back into a string.
"""
=> None
x = "Hello world"
=> None
f = charlist(x)
=> None
lst_to_string(f)
=> 'Hello world'
"""
def lst_to_string(lst):
if lst == None:
return ""
else:
return lst.value + lst_to_string(lst.next)