/
anton1om
/
file12
Обзор
Документация
Войти
/
anton1om
/
file12
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
task2.py
40 строк
1 KB
Anton
initial commit
07 июн 2026, 21:17
07 июн 2026, 21:17
b0396f3
Код
Авторство
О чём код?
def convert_ingredients(ingredients_str): ingredients = {} tmp_str = ingredients_str.split(' | ') ingredients['ingredient_name'] = tmp_str[0] ingredients['quantity'] = int(tmp_str[1]) ingredients['measure'] = tmp_str[2] return ingredients def read_cook_book(file_path): cook_book = {} with open(file_path, 'r', encoding='utf-8') as f: dish_string = f.readline() while dish_string: dish_name = dish_string.strip() cook_book[dish_name] = [] ingredient_count = int(f.readline()) for _ in range(ingredient_count): ingredients_str = f.readline().strip() cook_book[dish_name].append(convert_ingredients(ingredients_str)) f.readline() dish_string = f.readline() return cook_book def get_shop_list_by_dishes(dishes, person_count): cook_book = read_cook_book('recipes.txt') shop_list = {} for dish in dishes: for ingridient in cook_book[dish]: i_name = ingridient['ingredient_name'] shop_list.setdefault(i_name,{}) shop_list[i_name]['measure'] = ingridient['measure'] shop_list[i_name].setdefault('quantity',0) shop_list[i_name]['quantity'] += ingridient['quantity'] * person_count return shop_list print(get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2))