Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # var 1 functions findall
- # 02. Ad Astra
- def extract_datas(some_matches):
- my_data_list = []
- calories_sum = []
- for information in some_matches:
- string_to_print = f"Item: {information[1]}, Best before: {information[2]}, Nutrition: {information[3]}"
- my_data_list.append(string_to_print)
- calories_sum.append(int(information[3]))
- return my_data_list, sum(calories_sum) // 2000
- def final_print(some_data, some_days):
- print(f'You have food to last you for: {some_days} days!')
- if some_data:
- for datas in some_data:
- print(datas)
- import re
- data = input()
- pattern = r'(\#|\|)([A-Za-z\s*]+)\1(\d{2}\/\d{2}\/\d{2})\1(\d{1,5})\1'
- matched = re.findall(pattern, data)
- extracted_datas, days = extract_datas(matched)
- final_print(extracted_datas, days)
- ====================================================================================================================
- # Var2 named groups groupdict , finditer
- # 02. Ad Astra
- import re
- data = input()
- pattern = r'(?P<sep1>(\#|\|))(?P<Food>[A-Za-z\s]+)(?P=sep1)' \
- r'(?P<Date>\d{2}/\d{2}/\d{2})(?P=sep1)(?P<Calories>\d{1,5})(?P=sep1)'
- my_products_details = []
- total_calories = []
- valid_input = re.finditer(pattern, data)
- for el in valid_input:
- current_el = el.groupdict()
- total_calories.append(int(current_el['Calories']))
- print_string = f"Item: {current_el['Food']}, Best before: {current_el['Date']}, Nutrition: {current_el['Calories']}"
- my_products_details.append(print_string)
- total_calories = sum(total_calories)
- food_per_day = total_calories // 2000
- if food_per_day >= 1:
- print(f'You have food to last you for: {food_per_day} days!')
- for el in my_products_details:
- print(el)
- else:
- print(f'You have food to last you for: 0 days!')
- ------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement