Advertisement
George_Ivanov05

0.2

Aug 10th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. import re
  2.  
  3. text = input()
  4. regex_pattern = r"(?P<beginning>\#|\@)(?P<word>[A-Za-z]{3,}(?P=beginning)(?P=beginning)[A-Za-z]{3,})"
  5.  
  6. matches = re.finditer(regex_pattern, text)
  7. a = ""
  8. mirror_words = []
  9. all_words = []
  10. for i in matches:
  11.     a = i.group("word")
  12.     if "##" in a:
  13.         a = a.replace("##", " ")
  14.     elif "@@" in a:
  15.         a = a.replace("@@", " ")
  16.     all_words.append(a)
  17.     b = a.split()
  18.     first = b[0]
  19.     second = b[1]
  20.     if first == second[::-1]:
  21.         mirror_words.append(f"{first} <=> {second}")
  22.  
  23. if len(all_words) == 0:
  24.     print("No word pairs found!")
  25.     if len(mirror_words) == 0:
  26.         print(f"No mirror words!")
  27. else:
  28.     print(f"{len(all_words)} word pairs found!")
  29.     if len(mirror_words) > 0:
  30.         print(f"The mirror words are:")
  31.         print(", ".join(mirror_words))
  32.     else:
  33.         print(f"No mirror words!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement