Skip to content

Instantly share code, notes, and snippets.

@pta2002
Created December 7, 2017 22:04
Show Gist options
  • Save pta2002/a251dfa0b83209c775dedd7a53ca96aa to your computer and use it in GitHub Desktop.
Save pta2002/a251dfa0b83209c775dedd7a53ca96aa to your computer and use it in GitHub Desktop.
class Node(object):
def __init__(self, name, weight, children):
self.name = name
self.weight = weight
self.children = children
def parse(content):
queue = {}
for l in content.split('\n'):
if l.strip() != '':
words = l.split(' ')
name = words[0]
weight = int(words[1][1:-1])
children = []
print('.', l)
if len(words[2:]) > 0:
while len(words[2:]) > 1:
w = words.pop()
if w.endswith(','):
w = w[:-1]
children.append(w)
queue[name] = (Node(name, weight, []), children)
return queue
with open('d6in.txt') as f:
queue = parse(f.read())
while len(queue) > 1:
for (k, v) in queue.items():
if len(v[1]) > 0:
while len(v[1]) > 0:
n = v[1].pop()
a = queue[n]
v[0].children.append(a)
del queue[n]
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment