avp26/u5/kindergarten_garden.py
Your Name 494c226d92 U5
2026-10-15 19:46:33 +02:00

45 lines
986 B
Python

class NoSuchStudentException(Exception):
"""Raised when plants are requested for a student not assigned a plot."""
DEFAULT_STUDENTS = (
"Alena",
"Boris",
"Cyril",
"Daniel",
"Eva",
"František",
"Gitka",
"Helena",
"Ivana",
"Jozef",
"Karol",
"Ladislav",
)
PLANT_NAMES = {
"G": "Tráva",
"C": "Ďatelina",
"R": "Reďkovky",
"V": "Fialky",
}
class Garden:
def __init__(self, diagram, students=None):
self.rows = diagram.splitlines()
self.students = sorted(DEFAULT_STUDENTS if students is None else students)
def plants(self, student):
try:
index = self.students.index(student)
except ValueError:
raise NoSuchStudentException(
f"Žiak '{student}' nemá pridelený záhon."
) from None
start = index * 2
return [
PLANT_NAMES[code] for row in self.rows for code in row[start : start + 2]
]