/
Vladimirrus
/
OOP-Opening
Обзор
Документация
Войти
/
Vladimirrus
/
OOP-Opening
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
Task2
Task 2.py
59 строк
2 KB
Vladimirrus
Task 2
19 окт 2025, 20:09
19 окт 2025, 20:09
8d30b64
Код
Авторство
О чём код?
from pprint import pprint def read_recipes(file_path): cook_book = {} with open(file_path, 'r', 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): ingredient_line = file.readline().strip() if ingredient_line: ingredient_name, quantity, measure = ingredient_line.split(' | ') ingredients.append({ 'ingredient_name': ingredient_name, 'quantity': int(quantity), 'measure': measure }) cook_book[dish_name] = ingredients file.readline() return cook_book def get_shop_list_by_dishes(cook_book, dishes, person_count): shop_list = {} for dish in dishes: if dish in cook_book: for ingredient in cook_book[dish]: ingredient_name = ingredient['ingredient_name'] quantity = ingredient['quantity'] * person_count measure = ingredient['measure'] if ingredient_name in shop_list: shop_list[ingredient_name]['quantity'] += quantity else: shop_list[ingredient_name] = { 'measure': measure, 'quantity': quantity } return shop_list cook_book = read_recipes('recipes.txt') dishes_to_cook = ['Запеченный картофель', 'Омлет'] person_count = 2 shopping_list = get_shop_list_by_dishes(cook_book, dishes_to_cook, person_count) pprint(shopping_list)