/
HarryHarbachev
/
DZ3
Обзор
Документация
Войти
/
HarryHarbachev
/
DZ3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
task1-task2.py
42 строки
1 KB
HarryH
first_commit
25 окт 2025, 18:18
25 окт 2025, 18:18
752a607
Код
Авторство
О чём код?
def read_recipes(path: str) -> dict: cook_book = {} with open(path, encoding="utf-8") as file: while True: dish_name = file.readline().strip() if not dish_name: break ingredient_count = int(file.readline().strip()) ingredients = [] for _ in range(ingredient_count): line = file.readline().strip() parts = [part.strip() for part in line.split("|")] ingredients.append( { "ingredient_name": parts[0], "quantity": int(parts[1]), "measure": parts[2], } ) cook_book[dish_name] = ingredients file.readline() return cook_book def get_shop_list_by_dishes(dishes: list[str], person_count: int) -> dict: global cook_book shop_list = {} for dish in dishes: ingredients = cook_book[dish] for ingredient in ingredients: name = ingredient["ingredient_name"] measure = ingredient["measure"] quantity = ingredient["quantity"] * person_count if name in shop_list: shop_list[name]["quantity"] += quantity else: shop_list[name] = {"measure": measure, "quantity": quantity} return shop_list cook_book: dict = read_recipes("recipes.txt") print(get_shop_list_by_dishes(["Запеченный картофель", "Омлет"], 2))