/
valuevvik
/
Homework
Обзор
Документация
Войти
/
valuevvik
/
Homework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.py
81 строка
2 KB
valuevvik
main
18 окт 2025, 16:54
18 окт 2025, 16:54
6e23f76
Код
Авторство
О чём код?
# Задача 1 cook_book = {} with open('recipes.txt', 'r', encoding='utf-8') as file: lines = [line.strip() for line in file if line.strip()] i = 0 while i < len(lines): dish_name = lines[i] i += 1 ingredient_count = int(lines[i]) i += 1 ingredients = [] for _ in range(ingredient_count): name, quantity, measure = map(str.strip, lines[i].split('|')) ingredients.append({ 'ingredient_name': name, 'quantity': int(quantity), 'measure': measure }) i += 1 cook_book[dish_name] = ingredients print(cook_book) # Задача 2 def get_shop_list_by_dishes(dishes, person_count): shop_list = {} for dish_name in dishes: if dish_name in cook_book: for ingredient in cook_book[dish_name]: name = ingredient['ingredient_name'] quantity = ingredient['quantity'] * person_count measure = ingredient['measure'] if name in shop_list: shop_list[name]['quantity'] += quantity else: shop_list[name] = { 'measure': measure, 'quantity': quantity } return shop_list result = get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2) print(result) # Задача 3 def merge_files(file_list, output_filename='result.txt'): files_data = [] for filename in file_list: with open(filename, 'r', encoding='utf-8') as file: lines = file.readlines() files_data.append({ 'name': filename, 'line_count': len(lines), 'content': ''.join(lines) }) files_data.sort(key=lambda x: x['line_count']) with open(output_filename, 'w', encoding='utf-8') as output_file: for file_info in files_data: output_file.write(f"{file_info['name']}\n") output_file.write(f"{file_info['line_count']}\n") output_file.write(file_info['content']) if file_info != files_data[-1]: output_file.write('\n') files_to_merge = ['1.txt', '2.txt', '3.txt'] merge_files(files_to_merge)