/
Greka14
/
exercise2
Обзор
Документация
Войти
/
Greka14
/
exercise2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
main.py
72 строки
2 KB
Greka14
first_commit
28 май 2026, 14:36
28 май 2026, 14:36
4aaeb39
Код
Авторство
О чём код?
def parse_recipe_file(file_path: str) -> dict: cook_book = {} with open(file_path, 'r', encoding='utf-8') as f: lines = [line.strip() for line in f if line.strip() != ''] i = 0 while i < len(lines): dish_name = lines[i] i += 1 if i >= len(lines): break try: ingredient_count = int(lines[i]) except ValueError: break i += 1 ingredients = [] for _ in range(ingredient_count): if i >= len(lines): break parts = [part.strip() for part in lines[i].split('|')] if len(parts) == 3: ingredient_name, quantity, measure = parts try: quantity = int(quantity) except ValueError: try: quantity = float(quantity) except ValueError: pass ingredients.append({ 'ingredient_name': ingredient_name, 'quantity': quantity, 'measure': measure }) i += 1 cook_book[dish_name] = ingredients return cook_book def get_shop_list_by_dishes(cook_book: dict, dishes: list, person_count: int) -> dict: shop_list = {} for dish in dishes: if dish not in cook_book: continue for ingredient in cook_book[dish]: name = ingredient['ingredient_name'] measure = ingredient['measure'] quantity = ingredient['quantity'] * person_count if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} return shop_list if __name__ == '__main__': cook_book = parse_recipe_file('recipes.txt') result = get_shop_list_by_dishes(cook_book, ['Запеченный картофель', 'Омлет'], 2) print(result)