/
kristinkaandreeva2002
/
PYAPI_Homework
Обзор
Документация
Войти
/
kristinkaandreeva2002
/
PYAPI_Homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task1AndTask2.py
56 строк
2 KB
kristinkaandreeva2002
update Task1AndTask2.py
24 янв 2026, 20:52
Верифицирован
24 янв 2026, 20:52
5f20701
Код
Авторство
О чём код?
# Задание 1 def parse_recipe_file(file_path: str) -> dict: cook_book = {} with open(file_path, 'r', encoding='utf-8') as file: lines = [line.strip() for line in file.readlines() if line.strip()] i = 0 while i < len(lines): dish_name = lines[i] i += 1 ingredient_count = int(lines[i]) i += 1 ingredients = [] for _ in range(ingredient_count): ingredient_info = lines[i].split('|') ingredient_name = ingredient_info[0].strip() quantity = int(ingredient_info[1].strip()) measure = ingredient_info[2].strip() ingredients.append({ 'ingredient_name': ingredient_name, 'quantity': quantity, 'measure': measure }) i += 1 cook_book[dish_name] = ingredients return cook_book # Результат cook_book = parse_recipe_file('recipes.txt') cook_book # Задание 2 def get_shop_list_by_dishes(dishes: list, person_count: int, cook_book: dict) -> dict: shop_list = {} for dish in dishes: if dish in cook_book: for ingredient in cook_book[dish]: ingredient_name = ingredient['ingredient_name'] measure = ingredient['measure'] quantity = ingredient['quantity'] * person_count if ingredient_name in shop_list: shop_list[ingredient_name]['quantity'] += quantity else: shop_list[ingredient_name] = {'measure': measure, 'quantity': quantity} return shop_list # Результат shop_list = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2, cook_book) shop_list