-1

我有这个代码:

def reverse (word):
    newword = ''
    letterflag = -1
    for numletter in word:
        newword += word[letterflag]
        letterflag-=1
        s=newword
    s.upper()
    return newword
  
def isPalindrome(word, ignorecase=False):

    """
    >>> type(isPalindrome("bob"))
    <type 'bool'>
    >>> isPalindrome("abc")
    False
    >>> isPalindrome("bob")
    True
    >>> isPalindrome("a man a plan a canal, panama")
        True
    >>> isPalindrome("A man a plan a canal, Panama")
    False
    >>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
    True
    """
    word = str (word)
    newword = reverse(word)
    if word == newword:
        return True
    else:
        return False
  
   

当我键入“Bob”时,由于大写 B,我希望它返回 true。

4

3 回答 3

0

如果您希望有区分大小写的选项或不区分大小写的选项,请在 isPalindrome() 函数中添加 IF 语句:

if ignorecase == True:
    word = word.lower()

完成后应该是这样的:

import string
def reverse (word):
    newword = ''
    letterflag = -1
    for numletter in word:
        newword += word[letterflag]
        letterflag-=1
        s=newword
    s.upper()
    return newword

def isPalindrome(word, ignorecase=False):

    """
    >>> type(isPalindrome("bob"))
    <type 'bool'>
    >>> isPalindrome("abc")
    False
    >>> isPalindrome("bob")
    True
    >>> isPalindrome("a man a plan a canal, panama")
        True
    >>> isPalindrome("A man a plan a canal, Panama")
    False
    >>> isPalindrome("A man a plan a canal, Panama", ignorecase=True)
    True
    """
    if ignorecase == True:
        word = word.lower()
    word = word.replace(',', '')
    word = word.replace(' ', '')
    newword = reverse(word)
    if word == newword:
        return True
    else:
        return False

该代码给了我以下反馈:

isPalindrome('Bob', ignorecase=True)
Out[34]: True

isPalindrome('Bob')
Out[35]: False
于 2013-11-13T20:35:04.157 回答
0

了解此答案中所做内容的最佳方法是在 Python 控制台中尝试其中的各个部分。

要修复您的 reverse(),请执行以下操作:

def reverse (word):
    newword = ''
    letterflag = -1
    for numletter in word:
        newword += word[letterflag]
        letterflag-=1
    return newword

请注意,我还取出了 .upper() 部分,因为它们无效,并且 reverse 不是放置它的正确位置,因为您无法将反转的大写单词与原始单词进行比较。也s.upper()不像你想象的那样工作。它返回一个大写的副本s而不修改s. 你只需要return newword.upper()让它工作。

此外,不需要 letterflag,您可以简单地执行以下操作:

def reverse (word):
    newword = ''
    for letter in word:
        newword = letter + newword #adds each new letter to beginning
    return newword

但是,执行反向功能的最简单方法是:

def reverse (word):
    return word[::-1]

您的 isPalendrome 需要这样才能基本工作:

def isPalindrome(word, ignorecase=False):
    word = word.replace(',', '').replace(' ', '') #removes ,'s and spaces
    if ignorecase:
        return word.lower() == reverse(word).lower()
    else:
        return word == reverse(word)

这是一个更高级的解决方案,它将忽略不是字母的任何内容,并可选择忽略大小写。这个版本确实需要反转。

def isPalindrome(word, ignorecase=False):
    abcs = 'abcdefghijklmnopqrstuvwxyz'
    word = [c for c in word.lower()*ignorecase or word if c in abcs]
    return word == word[::-1] #no need for reverse
于 2013-11-13T19:28:55.743 回答
0

只要让你的输入总是小写,这样你就可以完全避免这个问题。

word = str(word)
word = word.lower()
word = word.replace(',', '') # removes any commas from the string
newword = word[::-1] # reverse string
if word == newword:
    return True
else:
    return False
于 2013-11-13T18:50:00.700 回答