avp26/u5/kindergarten_garden.py
2026-10-14 19:53:22 +00:00

51 lines
1.4 KiB
Python
Raw Permalink 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):
pass
class Garden:
def __init__(self, diagram, students=None):
# розбивка на 2 рядки
rows = diagram.split("\n")
self.row1 = rows[0]
self.row2 = rows[1]
# деф. список або сорт. кастомного
if students is None:
self.students = [
"Alena", "Boris", "Cyril", "Daniel", "Eva", "František",
"Gitka", "Helena", "Ivana", "Jozef", "Karol", "Ladislav"
]
else:
self.students = sorted(students)
# мапінг назв
self.plant_names = {
"G": "Tráva",
"C": "Ďatelina",
"R": "Reďkovky",
"V": "Fialky"
}
def plants(self, student):
# чек студ.
if student not in self.students:
raise NoSuchStudentException()
# розрахунок інд.
index = self.students.index(student)
pos = index * 2
# вибір 4 симв.
letters = [
self.row1[pos],
self.row1[pos + 1],
self.row2[pos],
self.row2[pos + 1]
]
# конверт в назви
result = []
for letter in letters:
result.append(self.plant_names[letter])
return result