#
# Your previous Plain Text content is preserved below:
#
# Paul
# Scott Kate Kevin
# Jack James Chris
#
#
#
#
class Person:
def __init__(self, name):
self.name = name
self.children = []
def print_family(self):
print(self.name)
for child in self.children:
child.print_family()
def family_to_string(self):
# self.family_to_string_helper("")
stringlist = []
stringlist.append(self.name)
for child in self.children:
stringlist.append(child.family_to_string())
return " ".join(stringlist)
def encode(self):
famlist = []
famlist.append(self.name + "(")
for child in self.children:
famlist.append(child.encode())
famlist.append(")")
return "".join(famlist)
def decode(string):
result, _ = decodehelper(string)
return result
def decodehelper(string):
name, rest = string.split("(", 1)
currentPerson = Person(name)
while rest[0] != ")":
person, rest = decodehelper(rest)
currentPerson.children.append(person)
return currentPerson, rest[1:]
paul = Person("Paul")
scott = Person("Scott")
kate = Person("Kate")
kevin = Person("Kevin")
paul.children = [scott, kate, kevin]
jack = Person("Jack")
james = Person("James")
chris = Person("Chris")
scott.children = [jack, james]
kate.children = [chris]
#print(paul.family_to_string())
# new_paul, _ = Person.decode("Jack()")
# print(new_paul.family_to_string())
copy_of_paul = decode(paul.encode())
print(copy_of_paul.family_to_string())
# Paul(Scott(Jack()James())Kate(Chris())Kevin())
#
# Scott(Jack()James())Kate(Chris())Kevin())
# rest = Jack()James())Kate(Chris())Kevin())
# rest = James())Kate(Chris())Kevin())
# rest = )Kate(Chris())Kevin())
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.