This commit is contained in:
Vadym Afanasiev 2026-10-14 19:53:22 +00:00
parent b87fc3fb49
commit cd18736cfa

View File

@ -4,10 +4,12 @@ class NoSuchStudentException(Exception):
class Garden: class Garden:
def __init__(self, diagram, students=None): def __init__(self, diagram, students=None):
# розбивка на 2 рядки
rows = diagram.split("\n") rows = diagram.split("\n")
self.row1 = rows[0] self.row1 = rows[0]
self.row2 = rows[1] self.row2 = rows[1]
# деф. список або сорт. кастомного
if students is None: if students is None:
self.students = [ self.students = [
"Alena", "Boris", "Cyril", "Daniel", "Eva", "František", "Alena", "Boris", "Cyril", "Daniel", "Eva", "František",
@ -16,6 +18,7 @@ class Garden:
else: else:
self.students = sorted(students) self.students = sorted(students)
# мапінг назв
self.plant_names = { self.plant_names = {
"G": "Tráva", "G": "Tráva",
"C": "Ďatelina", "C": "Ďatelina",
@ -24,12 +27,15 @@ class Garden:
} }
def plants(self, student): def plants(self, student):
# чек студ.
if student not in self.students: if student not in self.students:
raise NoSuchStudentException() raise NoSuchStudentException()
# розрахунок інд.
index = self.students.index(student) index = self.students.index(student)
pos = index * 2 pos = index * 2
# вибір 4 симв.
letters = [ letters = [
self.row1[pos], self.row1[pos],
self.row1[pos + 1], self.row1[pos + 1],
@ -37,6 +43,7 @@ class Garden:
self.row2[pos + 1] self.row2[pos + 1]
] ]
# конверт в назви
result = [] result = []
for letter in letters: for letter in letters:
result.append(self.plant_names[letter]) result.append(self.plant_names[letter])