/
Peter_S
/
Homework
Обзор
Документация
Войти
/
Peter_S
/
Homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
Hell's_kitchen.py
34 строки
2 KB
Peter S
Small correct commit
26 дек 2025, 06:43
26 дек 2025, 06:43
7ff8e6b
Код
Авторство
О чём код?
def save_the_cookbook(file): with open(file, encoding='utf-8') as file: cook_book = {} sneaky_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '\n'] # Пришлось добавить дополнительную ad-hoc проверку: все варианты с методом readline занимают у меня значительно # больше строк кода. Буду рад, если подскажете более короткий вариант. for line in file: if line[0] not in sneaky_list and '|' not in line: dish = line.strip() cook_book.setdefault(dish, []) if '|' in line: ingr_list = line.strip().split('|') ingr_dict = {'ingredient_name': ingr_list[0].strip(), 'quantity': int(ingr_list[1].strip()), 'measure': ingr_list[2].strip()} cook_book[dish].append(ingr_dict) return cook_book def get_shop_list_by_dishes(dishes, person_count, file='recipes.txt'): shopping_list = {} cook_book = save_the_cookbook(file) for dish in dishes: for ingredient in cook_book[dish]: if ingredient['ingredient_name'] in shopping_list: shopping_list[ingredient['ingredient_name']]['quantity'] += ingredient['quantity'] * person_count continue ingr_dict_1 = {ingredient['ingredient_name']: {'measure': ingredient['measure'], 'quantity': ingredient['quantity'] * person_count}} shopping_list.update(ingr_dict_1) return shopping_list