同步操作将从 编程语言算法集/Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# Algorithms to determine if a string is palindrometest_data = {"MALAYALAM": True,"String": False,"rotor": True,"level": True,"A": True,"BB": True,"ABC": False,"amanaplanacanalpanama": True, # "a man a plan a canal panama"}# Ensure our test data is validassert all((key == key[::-1]) is value for key, value in test_data.items())def is_palindrome(s: str) -> bool:"""Return True if s is a palindrome otherwise return False.>>> all(is_palindrome(key) is value for key, value in test_data.items())True"""start_i = 0end_i = len(s) - 1while start_i < end_i:if s[start_i] == s[end_i]:start_i += 1end_i -= 1else:return Falsereturn Truedef is_palindrome_recursive(s: str) -> bool:"""Return True if s is a palindrome otherwise return False.>>> all(is_palindrome_recursive(key) is value for key, value in test_data.items())True"""if len(s) <= 1:return Trueif s[0] == s[len(s) - 1]:return is_palindrome_recursive(s[1:-1])else:return Falsedef is_palindrome_slice(s: str) -> bool:"""Return True if s is a palindrome otherwise return False.>>> all(is_palindrome_slice(key) is value for key, value in test_data.items())True"""return s == s[::-1]if __name__ == "__main__":for key, value in test_data.items():assert is_palindrome(key) is is_palindrome_recursive(key)assert is_palindrome(key) is is_palindrome_slice(key)print(f"{key:21}{value}")print("a man a plan a canal panama")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。