2 line solution
This commit is contained in:
parent
ba6c38c00e
commit
c11cbf310c
@ -1,57 +1,65 @@
|
|||||||
import string
|
import string
|
||||||
import math
|
import math
|
||||||
|
|
||||||
def isPalindrome(s):
|
# def isPalindrome(s):
|
||||||
"""
|
# """
|
||||||
:type s: str
|
# :type s: str
|
||||||
:rtype: bool
|
# :rtype: bool
|
||||||
"""
|
# """
|
||||||
# Create a translation table to remove uppercase letters and special characters
|
# # Create a translation table to remove uppercase letters and special characters
|
||||||
# translation_table = str.maketrans("","", string.punctuation + string.whitespace)
|
# # translation_table = str.maketrans("","", string.punctuation + string.whitespace)
|
||||||
translation_table = ''.join(c.lower() if c.isupper() or c.isalnum() else '' for c in s if c.isalnum() or c in string.whitespace)
|
# translation_table = ''.join(c.lower() if c.isupper() or c.isalnum() else '' for c in s if c.isalnum() or c in string.whitespace)
|
||||||
# print(translation_table)
|
# # print(translation_table)
|
||||||
if len(translation_table) == 2:
|
# if len(translation_table) == 2:
|
||||||
if translation_table[0] == translation_table[1]:
|
# if translation_table[0] == translation_table[1]:
|
||||||
return True
|
# return True
|
||||||
else:
|
# else:
|
||||||
return False
|
# return False
|
||||||
|
|
||||||
# Use the translation table to remove uppercase letters and special characters
|
# # Use the translation table to remove uppercase letters and special characters
|
||||||
# upper_string = s.translate(translation_table)
|
# # upper_string = s.translate(translation_table)
|
||||||
# lower_string = upper_string.lower()
|
# # lower_string = upper_string.lower()
|
||||||
lower_string = translation_table
|
# lower_string = translation_table
|
||||||
|
|
||||||
half = len(lower_string)/2
|
# half = len(lower_string)/2
|
||||||
# Round the value down since middle value doesn't matter
|
# # Round the value down since middle value doesn't matter
|
||||||
if len(lower_string)%2 != 0:
|
# if len(lower_string)%2 != 0:
|
||||||
halfdown = int(math.floor(half))
|
# halfdown = int(math.floor(half))
|
||||||
else:
|
# else:
|
||||||
halfdown = int(half)
|
# halfdown = int(half)
|
||||||
# print(halfup)
|
# # print(halfup)
|
||||||
# print(halfdown)
|
# # print(halfdown)
|
||||||
first = []
|
# first = []
|
||||||
second = []
|
# second = []
|
||||||
print(lower_string)
|
# print(lower_string)
|
||||||
|
|
||||||
for i in range(halfdown):
|
# for i in range(halfdown):
|
||||||
# print(i)
|
# # print(i)
|
||||||
# print(lower_string[i])
|
# # print(lower_string[i])
|
||||||
first.append(lower_string[i])
|
# first.append(lower_string[i])
|
||||||
|
|
||||||
if len(lower_string)%2 != 0:
|
# if len(lower_string)%2 != 0:
|
||||||
for j in range(halfdown+1, len(lower_string)):
|
# for j in range(halfdown+1, len(lower_string)):
|
||||||
second.insert(0, lower_string[j])
|
# second.insert(0, lower_string[j])
|
||||||
|
|
||||||
else:
|
# else:
|
||||||
for j in range(halfdown, len(lower_string)):
|
# for j in range(halfdown, len(lower_string)):
|
||||||
second.insert(0, lower_string[j])
|
# second.insert(0, lower_string[j])
|
||||||
|
|
||||||
print(first)
|
# print(first)
|
||||||
print(second)
|
# print(second)
|
||||||
if first == second:
|
# if first == second:
|
||||||
return True
|
# return True
|
||||||
else:
|
# else:
|
||||||
return False
|
# return False
|
||||||
|
|
||||||
|
#Fastest solution
|
||||||
|
# Operator ~ is the bitwise NOT operator (~x == -x-1 => ~0 == -1 => ~1 == -2 and etc),
|
||||||
|
# which expects just one argument. It performs logical negation on a given number by
|
||||||
|
# flipping all of its bits:
|
||||||
|
def isPalindrome(s: str) -> bool:
|
||||||
|
s = [c.lower() for c in s if c.isalnum()]
|
||||||
|
return all (s[i] == s[~i] for i in range(len(s)//2))
|
||||||
|
|
||||||
print(isPalindrome("A man, a plan, a canal: Panama"))
|
print(isPalindrome("A man, a plan, a canal: Panama"))
|
||||||
print(isPalindrome("aa"))
|
print(isPalindrome("aa"))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user