Skip to content

Instantly share code, notes, and snippets.

@jmoy
Created June 6, 2019 06:06
Show Gist options
  • Save jmoy/2be26a7d616d487e0b36bbcc1d42a50e to your computer and use it in GitHub Desktop.
Save jmoy/2be26a7d616d487e0b36bbcc1d42a50e to your computer and use it in GitHub Desktop.
Session 1 in Python
with open("code/test2.txt") as fin:
sbj_counts = {}
for l in fin:
l = l.strip()
m = re.fullmatch(r"((?:\w|\.)+)\s+in\s+(\w+)",l)
if m:
degree,subject = m.group(1,2)
print(f"{degree} ({subject})")
if subject in sbj_counts:
sbj_counts[subject].add(degree)
else:
sbj_counts[subject] = {degree}
else:
print(l)
for subj in sorted(sbj_counts):
degrees = sorted(sbj_counts[subj])
deglst = ', '.join(degrees)
was_were = "was" if len(degrees)==1 else "were"
print(f"{deglst} {was_were} awarded in {subj}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment