MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_palindrome_traversal

Function is_palindrome_traversal

strings/palindrome.py:40–56  ·  view source on GitHub ↗

Return True if s is a palindrome otherwise return False. >>> all(is_palindrome_traversal(key) == value for key, value in test_data.items()) True

(s: str)

Source from the content-addressed store, hash-verified

38
39
40def is_palindrome_traversal(s: str) -> bool:
41 """
42 Return True if s is a palindrome otherwise return False.
43
44 >>> all(is_palindrome_traversal(key) == value for key, value in test_data.items())
45 True
46 """
47 end = len(s) // 2
48 n = len(s)
49
50 # We need to traverse till half of the length of string
51 # as we can get access of the i'th last element from
52 # i'th index.
53 # eg: [0,1,2,3,4,5] => 4th index can be accessed
54 # with the help of 1st index (i==n-i-1)
55 # where n is length of string
56 return all(s[i] == s[n - i - 1] for i in range(end))
57
58
59def is_palindrome_recursive(s: str) -> bool:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected