is_ palindrome.py 614 B

123456789101112131415161718192021222324252627282930313233
  1. import pytest
  2. def isPalindrome(s: str):
  3. s = ''.join(ch for ch in s if ch.isalnum()).lower()
  4. if s == '':
  5. return True
  6. lens = len(s)
  7. p, q = 0, lens - 1
  8. while p < lens or q > 0:
  9. if p >= q:
  10. return True
  11. elif s[p] == s[q]:
  12. p += 1
  13. q -= 1
  14. else:
  15. return False
  16. @pytest.mark.parametrize(
  17. "s, expect",
  18. [
  19. ("", True),
  20. (".", True),
  21. ("aa", True),
  22. ("A man, a plan, a canal: Panama", True),
  23. ("a123a", False)
  24. ]
  25. )
  26. def test_cases(s, expect):
  27. assert isPalindrome(s) == expect