Skip to content

Commit

Permalink
6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
c11z committed Feb 7, 2019
1 parent 57af6c1 commit 03c3e71
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions epi_judge_python/is_string_palindromic_punctuation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
from test_framework import generic_test


# O(n) time complexity O(1) space
def is_palindrome(s: str):
i = 0
j = len(s) - 1
trip = True if s == "" else False
while i < j:
if not s[i].isalnum():
i += 1
elif not s[j].isalnum():
j -= 1
elif s[i].lower() == s[j].lower():
trip = True
i += 1
j -= 1
else:
return False
return trip


# solution from book
def is_palindrome(s):
# TODO - you fill in here.
# i moves forward, and j moves backward.
i, j = 0, len(s) - 1
while i < j:
# i and j both skip non-alphanumeric characters.
while not s[i].isalnum() and i < j:
i += 1
while not s[j].isalnum() and i < j:
j -= 1
if s[i].lower() != s[j].lower():
return False
i, j = i + 1, j - 1
return True


if __name__ == '__main__':
if __name__ == "__main__":
exit(
generic_test.generic_test_main("is_string_palindromic_punctuation.py",
"is_string_palindromic_punctuation.tsv",
is_palindrome))
generic_test.generic_test_main(
"is_string_palindromic_punctuation.py",
"is_string_palindromic_punctuation.tsv",
is_palindrome,
)
)

0 comments on commit 03c3e71

Please sign in to comment.