/
Zenon
/
homework_py
Обзор
Документация
Войти
/
Zenon
/
homework_py
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
51 строка
2 KB
Seraphim
first_commit
21 окт 2025, 02:06
21 окт 2025, 02:06
6a8347c
Код
Авторство
О чём код?
def parser(filename): cook_book = {} with open(filename, 'r', encoding='utf-8') as file: data = file.read().strip() dishes = data.split('\n\n') for dish in dishes: lines = dish.split('\n') dish_name = lines[0] ingredients_count = int(lines[1]) ingredients = [] for i in range(2, 2 + ingredients_count): name, quantity, measure = lines[i].split('|') ingredients.append({ 'ingredient_name': name, 'quantity': int(quantity), 'measure': measure }) cook_book[dish_name] = ingredients return cook_book def get_shop_list_by_dishes(dishes, person_count): cook_book = parser('recipes.txt') shop_list = {} if isinstance(dishes, str): dishes = [dishes] for dish_name in dishes: if dish_name not in cook_book: print(f'блюда {dish_name} нет в списке рецептов') continue for ingredient in cook_book[dish_name]: 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 print(get_shop_list_by_dishes(['Омлет', 'Фахитос'], 1))