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