Python:回文等式

问题描述

我需要帮助填写空白以实现此功能,该功能将检查单词是否是回文,工作:

def is_palindrome(input_string):
    # We'll create two strings,to compare them
    new_string = ""
    reverse_string = ""
    # Traverse through each letter of the input string
    for ___:
        # Add any non-blank letters to the 
        # end of one string,and to the front
        # of the other string. 
        if ___:
            new_string = ___
            reverse_string = ___
    # Compare the strings
    if ___:
        return True
    return False

解决方法

如果您想要一种简单的方法来确定某个单词是否是回文字母:

def isPalindrome(word):
    # Loop over half the length of the string in question
    for l in range(round(len(word)/2)):
        # If the current letter is not equal to the letter
        # on the corresponding opposite of the string,return False       
        if word[l] != word[-(l+1)]: return False
    # If they all match,return True
    return True

从技术上讲,您可以导入math模块并执行math.floor而不是round来跳过对奇数字符串的不必要的比较

,

我建议您该网页包含四(04)个算法来解决回文问题。源代码位于javaScript中,但是您可以轻松地在Python中执行相同的操作(或转换为Python)。

链接:https://medium.com/better-programming/algorithms-101-palindromes-8a06ea97af86

您要尝试的是上一个建议的网页中的解决方案1 ​​

此外,回文算法是众所周知的,并且有很多记录片,您可以从这里开始:

有关一般信息,请检查wikipedia page on Palindrome

祝你好运。