Advertisement
GeorgiLukanov87

03. Descriptions 100/100 with regex

Jul 16th, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # 03. Descriptions 100/100 with regex
  2. # https://judge.softuni.org/Contests/Practice/Index/1140?fbclid=IwAR0szSoRJQozKH6o_5Bwi1U9Y-5R6OcPT_l6jSJX73WzJq_BmmhONJbVkEc#2
  3.  
  4.  
  5. import re
  6.  
  7. data = input()
  8.  
  9. pattern_names = r'(name is\s)([A-Z][a-z]+)+\s?([A-Z][a-z]+)'
  10. pattern_years = r'((\d+)\s(years))'
  11. pattern_b_day_data = r'(\d{2}-\d{2}-\d{4}).?'
  12. valid_data = []
  13. db_empty = False
  14. printed_once = False
  15. while not data == 'make migrations':
  16.     matched_names = re.findall(pattern_names, data)
  17.     if matched_names:
  18.         first_name = matched_names[0][1]
  19.         last_name = matched_names[0][2]
  20.         valid_data.append(first_name + " " + last_name)
  21.     else:
  22.         data = input()
  23.         db_empty = True
  24.         continue
  25.  
  26.     matched_years = re.findall(pattern_years, data)
  27.     if matched_years:
  28.         years_old = int(matched_years[0][1])
  29.         if 9 < years_old <= 99:
  30.             valid_data.append(years_old)
  31.         else:
  32.             matched_years = []
  33.             db_empty = True
  34.  
  35.     matched_b_day_data = re.findall(pattern_b_day_data, data)
  36.     if matched_b_day_data:
  37.         b_day_data = ''.join(matched_b_day_data)
  38.     else:
  39.         data = input()
  40.         db_empty = True
  41.         continue
  42.  
  43.     valid_data.append(b_day_data)
  44.     if matched_names and matched_years and matched_b_day_data:
  45.         print(f'Name of the person: {valid_data[0]}.')
  46.         print(f'Age of the person: {valid_data[1]}.')
  47.         print(f'Birthdate of the person: {valid_data[2]}.')
  48.         valid_data.clear()
  49.         printed_once = True
  50.         db_empty = True
  51.  
  52.     data = input()
  53.  
  54. if db_empty and not printed_once:
  55.     print('DB is empty')
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement