school.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from collections import defaultdict


class School(object):
    def __init__(self, name):
        self.db = defaultdict(set)

    def add(self, student, grade):
        """
        Add student to grade
        """
        self.db[grade].add(student)

    def grade(self, grade):
        """
        Return students in grade
        """
        return self.db.get(grade, set())

    def sort(self):
        """
        Return list of grades along with students each grades
        """
        return [(k, tuple(sorted(v))) for (k, v) in self.db.items()]

Comments

Converted to using defaultdict.

helenst commented 11 March 2016 at 13:34 UTC

You're not logged in right now. Please login via GitHub to comment