Hacker News new | past | comments | ask | show | jobs | submit login

No need for `invert_dict`; you're just counting so `collections.Counter` would work fine:

    from collections import Counter

    with open('data.txt', 'r') as datafile:
        students = dict(line.rsplit(maxsplit=1) for line in datafile)

    failing = [name for name, grade in students.items() if grade >= 'E']
    grade_counts = Counter(grade[0] for grade in students.values())

    print("Zsófia's grade:", students["Zsófia"])

    print("List of students with a failing grade:")
    print("  " + ", ".join(failing))

    print("Distribution of grades by letter:")
    for grade, count in sorted(grade_counts.items()):
        print("  {}: {} student{}".format(grade, count, 's' * (count != 1)))



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: