avp26/u5/kindergarten_garden.py
2026-10-13 13:57:16 +02:00

38 lines
1.2 KiB
Python

def bidirectional_dict(d: dict) -> dict:
for k, v in list(d.items()):
d[v] = k
return d
class NoSuchStudentException(Exception):
"""Raised when plants are requested for a student not assigned a plot."""
class Garden:
PLANTS = bidirectional_dict({
"Tráva": "G",
"Ďatelina": "C",
"Reďkovky": "R",
"Fialky": "V"
})
students = {}
rows: list[str]
def __init__(self, seq: str, students: list[str] = ["Alena", "Boris", "Cyril", "Daniel", "Eva", "František", "Gitka", "Helena", "Ivana", "Jozef", "Karol", "Ladislav"]):
self.rows = seq.split("\n")
if students is not None:
self.students = {name: "" for name in sorted(students)}
self.parse()
def parse(self):
for row in self.rows:
for x in range(0, len(row), 2):
self.students[list(self.students.keys())[x//2]] += row[x:x+2]
def plants(self, name: str) -> list:
student_plants = self.students.get(name)
if student_plants is None:
raise NoSuchStudentException(f"Student {name} is not assigned a plot.")
return [self.PLANTS.get(c) for c in student_plants]