acclivity

pyPalindromeExcludingPunctuation

Apr 12th, 2022 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. def is_palindrome(sentence):
  2.     fwd = 0
  3.     bak = len(sentence) - 1
  4.     while fwd < bak:
  5.         a = sentence[fwd]
  6.         if not a.isalpha() and not a.isdigit():
  7.             fwd += 1
  8.             continue
  9.         b = sentence[bak]
  10.         if not b.isalpha() and not b.isdigit():
  11.             bak -= 1
  12.             continue
  13.         if a.lower() != b.lower():
  14.             print(sentence, "is not palindromic")
  15.             break
  16.         fwd += 1
  17.         bak -= 1
  18.     else:               # the loop did NOT end with break
  19.         print(sentence, "is palindromic")
  20.  
  21. is_palindrome("7@32.37")
  22. # output: 7@32.37 is palindromic
  23.  
  24. is_palindrome("7@A32.3A7")
  25. # output: 7@A32.3A7 is palindromic
Add Comment
Please, Sign In to add comment