Advertisement
go6odn28

2_mirror_words

Mar 23rd, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. import re
  2.  
  3. string = input()
  4. valid_pairs = []
  5. mirror_pairs = []
  6. word_pairs = r"(@|#)(([A-Za-z]{3,})\1\1([A-Za-z]{3,}))\1"
  7. valid_match = re.search(word_pairs, string)
  8. if valid_match:
  9.     match = re.findall(word_pairs, string)
  10.     for m in match:
  11.         valid_pairs.append(m[2])
  12.         valid_pairs.append(m[3])
  13.     print(f"{len(valid_pairs)//2} word pairs found!")
  14.     for index in range(0, len(valid_pairs)-1, 2):
  15.         if valid_pairs[index] == valid_pairs[index+1][::-1]:
  16.             mirror_pairs.append(f"{valid_pairs[index]} <=> {valid_pairs[index+1]}")
  17.  
  18. else:
  19.     print("No word pairs found!")
  20. if mirror_pairs:
  21.     print("The mirror words are:")
  22.     print(", ".join(mirror_pairs))
  23. else:
  24.     print("No mirror words!")
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement