to operate the nbprune workflow
the topic is for you to write a graph parser
given a file that looks like this
s1 10 s2
s2 20 s1
s2 30 s3
s3 20 s4
your code should
- open and read the file
- and return a
dict
that looks like this
{'s1': [('s2', 10)], 's2': [('s1', 20), ('s3', 30)], 's3': [('s4', 20)]}
# prune-begin
from collections import defaultdict
def graphdict(filename):
result = defaultdict(list)
with open(filename) as f:
for line in f:
line = line.strip()
if not line:
continue
src, dist, dst = line.split()
result[src].append( (dst, int(dist)) )
return result
with open("graphdict.txt", "w") as w:
print("""
s1 10 s2
s2 20 s1
s2 30 s3
s3 20 s4
""", file=w)
dict(graphdict("graphdict.txt"))
{'s1': [('s2', 10)], 's2': [('s1', 20), ('s3', 30)], 's3': [('s4', 20)]}
# prune-end
# your code here
def graphdict(filename):
pass