This commit is contained in:
Marege 2026-10-13 17:21:41 +02:00
parent 7c30fcdbc0
commit ca38589028

53
u5/kindergarten_garden.py Normal file
View File

@ -0,0 +1,53 @@
class NoSuchStudentException(Exception):
"""Výnimka, ak žiak nemá v záhone pridelené políčka."""
pass
class Garden:
students = [
"Alena", "Boris", "Cyril", "Daniel", "Eva", "František",
"Gitka", "Helena", "Ivana", "Jozef", "Karol", "Ladislav"
]
plant_names = {
'V': 'Fialky',
'R': 'Reďkovky',
'C': 'Ďatelina',
'G': 'Tráva'
}
def __init__(self, diagram, students=None):
rows = diagram.splitlines() #ділимо на 2 рядки
top = rows[0]
bot = rows[1]
if students is None:
students = self.students #якшо нема списку то берем наш та сортуємо по абецеді
sorted_students = sorted(students)
#рахуємо для кожного учня його 4 полиички
cups = []
len_top = len(top)
len_bot = len(bot)
for i in range(0, len_top, 2):
top_part = top[i:i + 1] #беремо звеху 2(не індекс а к-сть елементів) і знизу 2
bot_part = bot[i:i + 1]
cup = top_part + bot_part
cups.append(cup)
#двємо горшочки з ківточками учням
self.plots = {}
l_cups = len(cups)
for i in range(l_cups):
if i >= len(sorted_students):
break #якшо горшків більше як учнів
student = sorted_students[i] # поточний студент 1
self.plots[student] = cups[i]
def plants(self, student):
if student not in self.plots: #чи такий учень існує
raise NoSuchStudentException(f"Žiak {student} nemá políček.")
#дефінуєм 4 букви учня
allfourleters = self.plots[student] # букви назарчика
result = [] #кожна буква це рослина беремо її з словника і додаємо в результат
for i in allfourleters:
plant = self.plant_names[i]
result.append(plant)
return result