Given students' names along with the grade that they are in, create a roster for the school.
In the end, you should be able to:
Note that all our students only have one name. (It's a small town, what do you want?)
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
raise Exception, you should write:
raise Exception("Meaningful message indicating the source of the error")
To run the tests, run the appropriate command below (why they are different):
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
python -m pytest grade_school_test.py
-v: enable verbose output
-x: stop running tests on first failure
--ff: run failures from previous test before running other test cases
For other options, see
python -m pytest -h
Note that, when trying to submit an exercise, make sure the solution is in the
You can find your Exercism workspace by running
exercism debug and looking for the line that starts with
For more detailed information about running tests, code style and linting, please see Running the Tests.
A pairing session with Phil Battos at gSchool http://gschool.it
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import unittest from grade_school import School # Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 class GradeSchoolTest(unittest.TestCase): def test_adding_student_adds_them_to_sorted_roster(self): school = School() school.add_student(name='Aimee', grade=2) expected = ['Aimee'] self.assertEqual(school.roster(), expected) def test_adding_more_students_adds_them_to_sorted_roster(self): school = School() school.add_student(name='Blair', grade=2) school.add_student(name='James', grade=2) school.add_student(name='Paul', grade=2) expected = ['Blair', 'James', 'Paul'] self.assertEqual(school.roster(), expected) def test_students_in_different_grades_in_same_roster(self): school = School() school.add_student(name='Chelsea', grade=3) school.add_student(name='Logan', grade=7) expected = ['Chelsea', 'Logan'] self.assertEqual(school.roster(), expected) def test_roster_returns_empty_list_if_no_students_are_enrolled(self): self.assertEqual(School().roster(), ) def test_roster_is_sorted_by_grade_then_name(self): school = School() for name, grade in [ ('Peter', 2), ('Anna', 1), ('Barb', 1), ('Zoe', 2), ('Alex', 2), ('Jim', 3), ('Charlie', 1), ]: school.add_student(name, grade) expected = ['Anna', 'Barb', 'Charlie', 'Alex', 'Peter', 'Zoe', 'Jim'] self.assertEqual(school.roster(), expected) def test_grade_returns_students_in_that_grade_in_alphabetical_order(self): school = School() school.add_student(name='Franklin', grade=5) school.add_student(name='Bradley', grade=5) school.add_student(name='Jeff', grade=1) expected = ['Bradley', 'Franklin'] self.assertEqual(school.grade(5), expected) def test_grade_returns_empty_list_if_no_students_are_in_that_grade(self): self.assertEqual(School().grade(1), ) if __name__ == '__main__': unittest.main()
from collections import defaultdict class School: def __init__(self): self.db = defaultdict(list) def add_student(self, name, grade): self.db[grade].append(name) self.db[grade] = sorted(self.db[grade]) def roster(self): all_student_names =  for grade_number in sorted(self.db.keys()): all_student_names.extend(self.db[grade_number]) return all_student_names def grade(self, grade_number): return self.db[grade_number]
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.