avp26/u5/kindergarten_garden.py
2026-10-13 14:34:22 +02:00

41 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class NoSuchStudentException(Exception):
"""Raised when plants are requested for a student not assigned a plot."""
class Garden:
PLANT_MAP ={
'G': 'Tráva', 'C': 'Ďatelina', 'R': 'Reďkovky', 'V': 'Fialky'
}
DEFAULT_STUDENTS = [
"Alena", "Boris", "Cyril", "Daniel", "Eva", "František", "Gitka", "Helena",
"Ivana", "Jozef", "Karol", "Ladislav"
]
def __init__(self, diagram, students=None):
self.diagram = diagram.splitlines()
if students is None:
self.students = sorted(self.DEFAULT_STUDENTS)
else:
self.students = sorted(students)
def plants(self, student_name): ## присутствует ли конкретный ученик которог запросила функция
if student_name not in self.students:
raise NoSuchStudentException(f"Žiak {student_name} sa nenachádza v zozname.")
# Находим индекс ученика в отсортированном списке
idx = self.students.index(student_name)
# Вычисляем номера колонок для этого ученика
col1 = idx * 2
col2 = col1 + 1
# Собираем буквы с верхнего и нижнего ряда для этих двух колонок (всего 4 лунки)
plant_codes = [
self.diagram[0][col1], # верх 1
self.diagram[0][col2], # верх 2
self.diagram[1][col1], # ниp 1
self.diagram[1][col2] # ниp 2
]
# Переводим буквенные коды (G, C, R, V)
return [self.PLANT_MAP[code] for code in plant_codes]