Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 03. Star Enigma Regular Expressions - More Exercises 100/100
- # https://judge.softuni.org/Contests/Practice/Index/1744#2
- import re
- n_messages = int(input())
- def count_letters(some_str: str):
- searched_letters = ['s', 't', 'a', 'r']
- counter_letters = 0
- for char in some_str.lower():
- if char in searched_letters:
- counter_letters += 1
- return counter_letters, some_str
- def decryption(some_str: str, value: int):
- modified_message = ""
- for char in some_str:
- new_char = int(ord(char)) - int(value)
- modified_message += chr(new_char)
- return modified_message
- pattern = r'.*@([A-z]+)[^\@\-\!\:\>]*:(\d+)[^\@\-\!\:\>]*\!(A|D)\![^\@\-\!\:\>]*\->(\d+).*'
- attacked_planets = []
- destroyed_planets = []
- for _ in range(n_messages):
- message = input()
- keys, cleaned_message = count_letters(message)
- decrypted_message = decryption(cleaned_message, keys)
- valid_info = re.findall(pattern, decrypted_message)
- if valid_info:
- name = valid_info[0][0]
- attack_type = valid_info[0][2]
- if attack_type == 'A':
- attacked_planets.append(name)
- elif attack_type == 'D':
- destroyed_planets.append(name)
- print(f'Attacked planets: {len(attacked_planets)}')
- for att_planet in sorted(attacked_planets):
- print(f'-> {att_planet}')
- print(f'Destroyed planets: {len(destroyed_planets)}')
- for destr_planet in sorted(destroyed_planets):
- print(f'-> {destr_planet}')
Add Comment
Please, Sign In to add comment