Advertisement
DiYane

Mirror words

Sep 18th, 2023
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. import re
  2.  
  3. command = input()
  4. list_matches = []
  5. new_list_matches = []
  6. pattern = r"([@#])([A-Za-z]{3,})(\1)(\1)([A-Za-z]{3,})(\1)"
  7. matches = re.findall(pattern,command)
  8.  
  9. for match in matches:
  10.     list_matches.append(match[1])
  11.     list_matches.append(match[4])
  12. if len(list_matches) != 0:
  13.     print(f"{int((len(list_matches))/2)} word pairs found!")
  14. else:
  15.     print(f"No word pairs found!")
  16.    
  17. for i in range (1,len(list_matches),2):
  18.     if (list_matches[i][::-1]) == (list_matches[i-1]) and (list_matches[i-1][::-1]) == (list_matches[i]):
  19.         new_list_matches.append(list_matches[i-1]+" <=> " + list_matches[i])
  20.        
  21. if len(new_list_matches) > 0:
  22.     print("The mirror words are:")
  23.     print(', '.join(new_list_matches))
  24. else:
  25.     print("No mirror words!")
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement