10 examples of 'convert to palindrome' in Python

Every line of 'convert to palindrome' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
31@staticmethod
32def is_palindrome(num):
33 return str(num) == ''.join(reversed(str(num)))
21def isPalindrome(string):
22 leftIdx = 0
23 rightIdx = len(string) - 1
24 while leftIdx < rightIdx:
25 if string[leftIdx] != string[rightIdx]:
26 return False
27 leftIdx += 1
28 rightIdx -= 1
29 return True
14def isPalindrome(self, s):
15 """
16 :type s: str
17 :rtype: bool
18 """
19 cleaned = re.sub('[^0-9a-zA-Z]', '', s)
20 if cleaned.lower() == cleaned[::-1].lower():
21 return True
22 return False
34def isPalindrome(self, s: 'str') -> 'bool':
35 s = ''.join(filter(str.isalnum, s)).lower()
36 return s == s[::-1]
36def isPalindrome(self, x):
37 """
38 :type x: int
39 :rtype: bool
40 """
41 if x < 0:
42 return False
43 x = abs(x)
44 l = len(str(x))
45 i = 1
46 while i < l / 2 + 1:
47
48 head = (x / 10 ** (l-i)) % 10
49 tail = (x % 10 ** i) if i == 1 else (x % 10 ** i) / (10 ** (i-1))
50 if head != tail:
51 return False
52 i = i + 1
53
54 return True
6def isPalindrome(self, x):
7 """
8 :type x: int
9 :rtype: bool
10 """
11 if x < 0:
12 return False
13 x_keep = x
14 result = 0
15 while x / 10 != 0:
16 result = (result + x % 10) * 10
17 x = x / 10
18
19 result += x % 10
20 if result - x_keep == 0:
21 return True
22 else:
23 return False
35def isPalindrome(self, A):
36 low, high = 0, len(A) - 1
37 while low < high:
38
39 while low < high and not A[low].isalnum():
40 low += 1
41
42 while low < high and not A[high].isalnum():
43 high -= 1
44
45 if A[low].lower() != A[high].lower():
46 return 0
47
48 low, high = low + 1, high - 1
49
50 return 1
5def canPermutePalindrome(self, s):
6 """
7 :type s: str
8 :rtype: bool
9
10 beats 49.02%
11 """
12 return sum(v % 2 for v in collections.Counter(s).values()) < 2
3def isPalindrome(self, x):
4 if x < 0:
5 return False
6 if (x < 10):
7 return True
8 prefix = x
9 suffix = x % 10
10 p = 10
11 # for 1213445443121
12 # get x/10**n < 10**4 1213 and x%10**4 3121
13 while suffix * suffix < x:
14
15 prefix = x
16 while prefix >= p:
17 prefix = prefix / 10
18
19 l = suffix / (p/10)
20 h = prefix % 10
21 if (l != h):
22 return False
23 p = p * 10
24 suffix = x % p
25
26 return True
2def isPalindrome(self, x):
3 """
4 :type x: int
5 :rtype: bool
6 """
7 if x < 0:
8 return False
9 elif x < 10 or x == 0:
10 return True
11 else:
12 new_num = x % 10
13 x = x // 10
14 if new_num == 0:
15 return False
16 while True:
17 if new_num >= x:
18 return new_num == x or new_num // 10 == x # 表达式本省是否成立可以返回True/False, 不用显示返回
19 else:
20 new_num = new_num * 10 + x % 10
21 x = x // 10

Related snippets