/
netogithubik
/
neto-files
Обзор
Документация
Войти
/
netogithubik
/
neto-files
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
2.py
68 строк
2 KB
rskuro
add task 2
28 янв 2026, 00:03
28 янв 2026, 00:03
4315d20
Код
Авторство
О чём код?
def ingredient_line_handler(line): line_arr = line.split(" | ") return { "ingredient_name": line_arr[0], "quantity": line_arr[1], "measure": line_arr[2], } def recipe_book_maker(recipe_strings_array): recipe_object = {} current_recipe_name = None current_ingredients = [] for line in recipe_strings_array: stripped_line = line.strip() if not stripped_line: if current_recipe_name is not None: recipe_object[current_recipe_name] = current_ingredients current_recipe_name = None current_ingredients = [] continue if stripped_line.isdigit(): continue if "|" in stripped_line: current_ingredients.append(ingredient_line_handler(stripped_line)) else: current_recipe_name = stripped_line if current_recipe_name is not None: recipe_object[current_recipe_name] = current_ingredients return recipe_object def get_shop_list_by_dishes(dishes, person_count): shop_obj = {} with open("recipes.txt", encoding="UTF-8") as file: data = file.readlines() recipe_book = recipe_book_maker(data) for dish in dishes: if dish not in recipe_book: print(f"Предупреждение: рецепт '{dish}' не найден в книге рецептов. Ингридиенты для него не были посчитаны.") continue for ingredient in recipe_book[dish]: ingredient_name = ingredient["ingredient_name"] ingredient_quantity = float(ingredient["quantity"]) ingredient_measure = ingredient["measure"] if ingredient_name not in shop_obj: shop_obj[ingredient_name] = { 'quantity': ingredient_quantity * person_count, 'measure': ingredient_measure } else: shop_obj[ingredient_name]["quantity"] += ingredient_quantity * person_count return shop_obj print(get_shop_list_by_dishes(['Запеченный картофель', 'Омлет'], 2))