Created
March 15, 2020 18:42
-
-
Save gauravghongde/2a7490d7bb6f1dd68c2e1d46c83f3e44 to your computer and use it in GitHub Desktop.
Check if palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import perf_counter | |
string_to_check = "Go hang a salami --- --- I'm a lasagna hog." | |
#myCode - | |
def is_palindrome1(p_str): | |
new_str = "" | |
for char in p_str: | |
if char.isalpha(): | |
new_str += char.lower() | |
start_p = 0 | |
end_p = len(new_str)-1 | |
while start_p < end_p: | |
if new_str[start_p] != new_str[end_p]: | |
return False | |
start_p += 1 | |
end_p -= 1 | |
return True | |
#cleanCode - | |
def is_palindrome2(p_str): | |
fwd = ''.join(re.findall(r'[a-z]+', p_str.lower())) | |
bwd = fwd[::-1] | |
return fwd == bwd | |
#------------- | |
t1_start1 = perf_counter() | |
print(is_palindrome1(string_to_check)) | |
t1_stop1 = perf_counter() | |
print("Elapsed time during the whole program in seconds [func 1]:", t1_stop1-t1_start1) | |
t1_start2 = perf_counter() | |
import re | |
print(is_palindrome2(string_to_check)) | |
t1_stop2 = perf_counter() | |
print("Elapsed time during the whole program in seconds [func 2]:", t1_stop2-t1_start2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment