/
Ivan-Shevtsov
/
Homework-OOP-2
Обзор
Документация
Войти
/
Ivan-Shevtsov
/
Homework-OOP-2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Task2.py
63 строки
2 KB
Ivan-Shevtsov
create: 1.txt, 2.txt, 3.txt, README.md, recipes.txt, result.txt, Task1.py, Task2.py, Task3.py
30 апр 2026, 06:50
Верифицирован
30 апр 2026, 06:50
49a4e22
Код
Авторство
О чём код?
def read_cook_book(file_path): cook_book = {} with open(file_path) as f: lines = [line.strip() for line in f] i = 0 while i < len(lines): if not lines[i]: i += 1 continue dish_name = lines[i].strip() i += 1 if i >= len(lines): break ingredient_count = int(lines[i].strip()) i += 1 ingredients = [] for _ in range(ingredient_count): if i >= len(lines): break part1, part2, part3 = lines[i].strip().split(' | ') ingredients.append({ 'ingredient_name': part1.strip(), 'quantity': int(part2.strip()), 'measure': part3.strip() }) i += 1 if ingredients: cook_book[dish_name] = ingredients return cook_book def get_shop_list_by_dishes(dishes, person_count, cook_book): 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: if shop_list[name]['measure'] != measure: raise ValueError( f"Ингредиент '{name}' имеет разные единицы измерения: " f"{shop_list[name]['measure']} и {measure}" ) shop_list[name]['quantity'] += quantity else: shop_list[name] = {'measure': measure, 'quantity': quantity} return shop_list if __name__ == '__main__': cook_book = read_cook_book('recipes.txt') result = get_shop_list_by_dishes(['Фахитос', 'Омлет'], 2, cook_book) print(result)