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()
# LAZY BRUTE class School(object): class Student(object): def __init__(self, name, grade): self.name = name self.grade = grade def __init__(self): self.roster_list =  self.grades =  def add_student(self, name, grade): new_student = School.Student(name, grade) if grade not in self.grades: self.grades.append(grade) self.roster_list.append(new_student) def roster(self): roster =  grades = sorted(self.grades) for grade in grades: roster.extend(self.grade(grade)) return roster def grade(self, grade_number): grade_roster = [student.name for student in self.roster_list if student.grade == grade_number] return sorted(grade_roster)
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.