/
coupe80
/
rwfile_homework
Обзор
Документация
Войти
/
coupe80
/
rwfile_homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Homework.py
64 строки
2 KB
coupe80
Задание3
03 фев 2026, 09:33
Верифицирован
03 фев 2026, 09:33
b459daf
Код
Авторство
О чём код?
#Задание1 from pprint import pprint cook_book = {} with open('recipes.txt', encoding='utf-8') as file: dishes = '' for cook in file: cook = cook.strip() if cook.isdigit(): continue elif cook and '|' not in cook: cook_book[cook] = [] dishes = cook elif cook and '|' in cook: name, quantity, measure = cook.split(" | ") cook_book.get(dishes).append(dict(ingredient_name=name, quantity=int(quantity), measure=measure)) pprint(cook_book) №Задание2 def get_shop_list_by_dishes(dishes_list, person_count): shop_list = {} for dish in dishes_list: if dish in cook_book: for ingredient in cook_book[dish]: if ingredient['ingredient_name'] in shop_list: shop_list[ingredient['ingredient_name']]['quantity'] += ingredient['quantity'] * person_count else: shop_list[ingredient['ingredient_name']] = ({'measure': ingredient['measure'], 'quantity': (ingredient['quantity'] * person_count)}) else: print('Такого блюда нет в книге') return shop_list pprint(get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2)) №Задание3 def merge_files(output_file, input_files): files_info = [] for file_name in input_files: with open(file_name, encoding='utf-8') as f: lines = f.readlines() files_info.append({№ 'name': file_name, 'line_count': len(lines), 'content': lines }) sorted_files = sorted(files_info, key=lambda x: x['line_count']) with open(output_file, 'w', encoding='utf-8') as f: for file_info in sorted_files: f.write(f"{file_info['name']}\n{file_info['line_count']}\n") f.writelines(file_info['content']) if file_info['content'] and not file_info['content'][-1].endswith('\n'): f.write('\n') merge_files('result.txt', ['1.txt', '2.txt', '3.txt']) file = open("result.txt", encoding='utf-8') content = file.read() print(content) file.close()