Python: grade_school
This commit is contained in:
parent
b094d15ff7
commit
64012ef885
5 changed files with 170 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,3 +2,4 @@
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
**/__pycache__/
|
**/__pycache__/
|
||||||
|
**/.mypy_cache/
|
||||||
|
|
|
||||||
1
python/grade-school/.exercism/metadata.json
Normal file
1
python/grade-school/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"python","exercise":"grade-school","id":"0a07ce0d313a4fc7895b13247af40fe6","url":"https://exercism.io/my/solutions/0a07ce0d313a4fc7895b13247af40fe6","handle":"DmitryKokorin","is_requester":true,"auto_approve":false}
|
||||||
82
python/grade-school/README.md
Normal file
82
python/grade-school/README.md
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
# Grade School
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
- Add a student's name to the roster for a grade
|
||||||
|
- "Add Jim to grade 2."
|
||||||
|
- "OK."
|
||||||
|
- Get a list of all students enrolled in a grade
|
||||||
|
- "Which students are in grade 2?"
|
||||||
|
- "We've only got Jim just now."
|
||||||
|
- Get a sorted list of all students in all grades. Grades should sort
|
||||||
|
as 1, 2, 3, etc., and students within a grade should be sorted
|
||||||
|
alphabetically by name.
|
||||||
|
- "Who all is enrolled in school right now?"
|
||||||
|
- "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe.
|
||||||
|
Grade 3…"
|
||||||
|
|
||||||
|
Note that all our students only have one name. (It's a small town, what
|
||||||
|
do you want?)
|
||||||
|
|
||||||
|
## For bonus points
|
||||||
|
|
||||||
|
Did you get the tests passing and the code clean? If you want to, these
|
||||||
|
are some additional things you could try:
|
||||||
|
|
||||||
|
- If you're working in a language with mutable data structures and your
|
||||||
|
implementation allows outside code to mutate the school's internal DB
|
||||||
|
directly, see if you can prevent this. Feel free to introduce additional
|
||||||
|
tests.
|
||||||
|
|
||||||
|
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?
|
||||||
|
|
||||||
|
|
||||||
|
## Exception messages
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```python
|
||||||
|
raise Exception("Meaningful message indicating the source of the error")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run `pytest grade_school_test.py`
|
||||||
|
|
||||||
|
Alternatively, you can tell Python to run the pytest module:
|
||||||
|
`python -m pytest grade_school_test.py`
|
||||||
|
|
||||||
|
### Common `pytest` options
|
||||||
|
|
||||||
|
- `-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`
|
||||||
|
|
||||||
|
## Submitting Exercises
|
||||||
|
|
||||||
|
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/grade-school` directory.
|
||||||
|
|
||||||
|
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
|
||||||
|
|
||||||
|
For more detailed information about running tests, code style and linting,
|
||||||
|
please see [Running the Tests](http://exercism.io/tracks/python/tests).
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
A pairing session with Phil Battos at gSchool [http://gschool.it](http://gschool.it)
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||||
22
python/grade-school/grade_school.py
Normal file
22
python/grade-school/grade_school.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import bisect
|
||||||
|
from collections import defaultdict
|
||||||
|
from typing import List, DefaultDict
|
||||||
|
|
||||||
|
|
||||||
|
class School:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.student_db: DefaultDict[int, List[str]] = defaultdict(list)
|
||||||
|
|
||||||
|
def add_student(self, name: str, grade: int) -> None:
|
||||||
|
bisect.insort(self.student_db[grade], name)
|
||||||
|
|
||||||
|
def grade(self, grade_number: int) -> List[str]:
|
||||||
|
|
||||||
|
if grade_number not in self.student_db.keys():
|
||||||
|
return []
|
||||||
|
|
||||||
|
return self.student_db[grade_number]
|
||||||
|
|
||||||
|
def roster(self) -> List[str]:
|
||||||
|
return sum([self.grade(grade_number) for grade_number
|
||||||
|
in sorted(self.student_db)], [])
|
||||||
64
python/grade-school/grade_school_test.py
Normal file
64
python/grade-school/grade_school_test.py
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from grade_school import School
|
||||||
|
|
||||||
|
# Tests adapted from `problem-specifications//canonical-data.json`
|
||||||
|
|
||||||
|
|
||||||
|
class GradeSchoolTest(unittest.TestCase):
|
||||||
|
def test_adding_a_student_adds_them_to_the_sorted_roster(self):
|
||||||
|
school = School()
|
||||||
|
school.add_student(name="Aimee", grade=2)
|
||||||
|
expected = ["Aimee"]
|
||||||
|
self.assertEqual(school.roster(), expected)
|
||||||
|
|
||||||
|
def test_adding_more_student_adds_them_to_the_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_adding_students_to_different_grades_adds_them_to_the_same_sorted_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_an_empty_list_if_there_are_no_students_enrolled(self):
|
||||||
|
school = School()
|
||||||
|
expected = []
|
||||||
|
self.assertEqual(school.roster(), expected)
|
||||||
|
|
||||||
|
def test_student_names_with_grades_are_displayed_in_the_same_sorted_roster(self):
|
||||||
|
school = School()
|
||||||
|
school.add_student(name="Peter", grade=2)
|
||||||
|
school.add_student(name="Anna", grade=1)
|
||||||
|
school.add_student(name="Barb", grade=1)
|
||||||
|
school.add_student(name="Zoe", grade=2)
|
||||||
|
school.add_student(name="Alex", grade=2)
|
||||||
|
school.add_student(name="Jim", grade=3)
|
||||||
|
school.add_student(name="Charlie", grade=1)
|
||||||
|
expected = ["Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"]
|
||||||
|
self.assertEqual(school.roster(), expected)
|
||||||
|
|
||||||
|
def test_grade_returns_the_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_an_empty_list_if_there_are_no_students_in_that_grade(self):
|
||||||
|
school = School()
|
||||||
|
expected = []
|
||||||
|
self.assertEqual(school.grade(1), expected)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue