Advertisement
GeorgiLukanov87

AD ASTRA regex 2 vars 100/100

Aug 6th, 2022
149
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 1 0
  1. # var 1 functions findall
  2. # 02. Ad Astra
  3.  
  4.  
  5. def extract_datas(some_matches):
  6.     my_data_list = []
  7.     calories_sum = []
  8.     for information in some_matches:
  9.         string_to_print = f"Item: {information[1]}, Best before: {information[2]}, Nutrition: {information[3]}"
  10.         my_data_list.append(string_to_print)
  11.         calories_sum.append(int(information[3]))
  12.     return my_data_list, sum(calories_sum) // 2000
  13.  
  14.  
  15. def final_print(some_data, some_days):
  16.     print(f'You have food to last you for: {some_days} days!')
  17.     if some_data:
  18.         for datas in some_data:
  19.             print(datas)
  20.  
  21.  
  22. import re
  23.  
  24. data = input()
  25. pattern = r'(\#|\|)([A-Za-z\s*]+)\1(\d{2}\/\d{2}\/\d{2})\1(\d{1,5})\1'
  26. matched = re.findall(pattern, data)
  27. extracted_datas, days = extract_datas(matched)
  28. final_print(extracted_datas, days)
  29.  
  30.  
  31. ====================================================================================================================
  32.  
  33. # Var2 named groups groupdict , finditer
  34. # 02. Ad Astra
  35.  
  36. import re
  37.  
  38. data = input()
  39. pattern = r'(?P<sep1>(\#|\|))(?P<Food>[A-Za-z\s]+)(?P=sep1)' \
  40.           r'(?P<Date>\d{2}/\d{2}/\d{2})(?P=sep1)(?P<Calories>\d{1,5})(?P=sep1)'
  41.  
  42. my_products_details = []
  43. total_calories = []
  44.  
  45. valid_input = re.finditer(pattern, data)
  46. for el in valid_input:
  47.     current_el = el.groupdict()
  48.     total_calories.append(int(current_el['Calories']))
  49.     print_string = f"Item: {current_el['Food']}, Best before: {current_el['Date']}, Nutrition: {current_el['Calories']}"
  50.     my_products_details.append(print_string)
  51.  
  52. total_calories = sum(total_calories)
  53. food_per_day = total_calories // 2000
  54.  
  55. if food_per_day >= 1:
  56.     print(f'You have food to last you for: {food_per_day} days!')
  57.     for el in my_products_details:
  58.         print(el)
  59. else:
  60.     print(f'You have food to last you for: 0 days!')
  61.    
  62.    
  63. ------------------------------------------------------------------------------------------------------------------------
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement