GeorgiLukanov87

03. Star Enigma Regular Expressions - More Exercises 100/100

Jul 19th, 2022 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. # 03. Star Enigma Regular Expressions - More Exercises 100/100
  2. # https://judge.softuni.org/Contests/Practice/Index/1744#2
  3.  
  4.  
  5. import re
  6.  
  7. n_messages = int(input())
  8.  
  9.  
  10. def count_letters(some_str: str):
  11.     searched_letters = ['s', 't', 'a', 'r']
  12.     counter_letters = 0
  13.     for char in some_str.lower():
  14.         if char in searched_letters:
  15.             counter_letters += 1
  16.     return counter_letters, some_str
  17.  
  18.  
  19. def decryption(some_str: str, value: int):
  20.     modified_message = ""
  21.     for char in some_str:
  22.         new_char = int(ord(char)) - int(value)
  23.         modified_message += chr(new_char)
  24.     return modified_message
  25.  
  26.  
  27. pattern = r'.*@([A-z]+)[^\@\-\!\:\>]*:(\d+)[^\@\-\!\:\>]*\!(A|D)\![^\@\-\!\:\>]*\->(\d+).*'
  28. attacked_planets = []
  29. destroyed_planets = []
  30.  
  31. for _ in range(n_messages):
  32.     message = input()
  33.     keys, cleaned_message = count_letters(message)
  34.     decrypted_message = decryption(cleaned_message, keys)
  35.     valid_info = re.findall(pattern, decrypted_message)
  36.  
  37.     if valid_info:
  38.         name = valid_info[0][0]
  39.         attack_type = valid_info[0][2]
  40.  
  41.         if attack_type == 'A':
  42.             attacked_planets.append(name)
  43.            
  44.         elif attack_type == 'D':
  45.             destroyed_planets.append(name)
  46.  
  47. print(f'Attacked planets: {len(attacked_planets)}')
  48. for att_planet in sorted(attacked_planets):
  49.     print(f'-> {att_planet}')
  50.  
  51. print(f'Destroyed planets: {len(destroyed_planets)}')
  52. for destr_planet in sorted(destroyed_planets):
  53.     print(f'-> {destr_planet}')
  54.  
  55.  
Add Comment
Please, Sign In to add comment