/
gntshka
/
OpenReadWriteInFile
Обзор
Документация
Войти
/
gntshka
/
OpenReadWriteInFile
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task 1 and 2/CookBook.py
57 строк
2 KB
Ignat M
First commit
10 апр 2025, 15:51
10 апр 2025, 15:51
fa71457
Код
Авторство
О чём код?
import pprint with open('Recipe.txt', encoding='utf-8') as f: recipe_txt = f.readlines() def check_dish_name(dish_name): list_check = [str(x) for x in range(10)] list_check.append('|') for x in list_check: if x in dish_name: return False return True def check_ingridient_name(ingridient_name): return True if '|' in ingridient_name else False cook_book = {} for i, el in enumerate(recipe_txt): if el == '\n' or el == '': continue el = el.strip() if check_dish_name(el): cook_book[el] = [] dish = el continue if check_ingridient_name(el): ingridient = el.split(' | ') ingridient_dict = { 'ingridient_name': ingridient[0], 'quantity': float(ingridient[1]), 'measure': ingridient[2] } cook_book[dish].append(ingridient_dict) def get_shop_list_by_dishes(dishes, person_count): shop_list_by_dishes = {} for dish in dishes: for ingridient in cook_book[dish]: x = ingridient['ingridient_name'] if x in shop_list_by_dishes: all_quantity = (shop_list_by_dishes[x]['quantity'] + ingridient['quantity'] * person_count) shop_list_by_dishes[x]['quantity'] = all_quantity else: shop_list_by_dishes[x] = { 'quantity': ingridient['quantity'] * person_count, 'measure': ingridient['measure'] } return shop_list_by_dishes pprint.pprint(get_shop_list_by_dishes(['Фахитос', 'Запеченный картофель'], 3))