/
Netty
/
Homework
Обзор
Документация
Войти
/
Netty
/
Homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cook_book.py
93 строки
3 KB
Netty
update cook_book.py
11 янв 2026, 20:31
11 янв 2026, 20:31
5bdb2d6
Код
Авторство
О чём код?
import os def read_cook_book(): cook_book = {} with open('recipes.txt', 'r', encoding='utf-8') as file: lines = file.read().splitlines() i = 0 while i < len(lines): if not lines[i].strip(): i += 1 continue dish_name = lines[i].strip() i += 1 ingredient_count = int(lines[i].strip()) i += 1 ingredients = [] for _ in range(ingredient_count): if i < len(lines): parts = lines[i].strip().split('|') name = parts[0].strip() quantity = int(parts[1].strip()) measure = parts[2].strip() ingredients.append({ 'ingredient_name': name, 'quantity': quantity, 'measure': measure }) i += 1 cook_book[dish_name] = ingredients return cook_book def get_shop_list_by_dishes(dishes, person_count, cook_book=None): if cook_book is None: cook_book = read_cook_book() shop_list = {} for dish in dishes: if dish in cook_book: for ingredient in cook_book[dish]: name = ingredient['ingredient_name'] new_quantity = ingredient['quantity'] * person_count measure = ingredient['measure'] if name not in shop_list: shop_list[name] = { 'measure': measure, 'quantity': new_quantity } else: shop_list[name]['quantity'] += new_quantity else: print(f"Блюдо '{dish}' не найдено в книге рецептов.") return shop_list def merge_files(): files_info = [] files_dir = 'files_for_task3' if not os.path.exists(files_dir): print(f"Папка '{files_dir}' не найдена!") return for filename in os.listdir(files_dir): file_path = os.path.join(files_dir, filename) if os.path.isfile(file_path): with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() files_info.append({ 'name': filename, 'line_count': len(lines), 'content': lines }) files_info.sort(key=lambda x: x['line_count']) with open('result.txt', 'w', encoding='utf-8') as result_file: for file_info in files_info: result_file.write(f"{file_info['name']}\n") result_file.write(f"{file_info['line_count']}\n") result_file.writelines(file_info['content']) result_file.write("\n") ```