简体   繁体   中英

Capital letters in Jython/JES

I am currently writing a JES program that returns True or False dependent on whether a string containing a palindrome is passed to it. Although the program works, it fails when a capital letter or punctuation symbol is present. How could I get it to work?

print(ThisPalindrome("racecar"))

>> True 

print(ThisPalindrome("Racecar"))

>> False

To solve the issue with capitalization, you could try using the str.lower() method in your checks.

def ThisPalindrome(word):
    lowercase = word.lower()
    reversedOrder = reversed(lowercase)
    if lowercase == ''.join(reversedOrder):
        return True
    else:
        return False

In theory, this function should work with basic punctuation too, as long as it doesn't break the function. Input such as ' could cause it to break.

The toLowerCase() method to return the calling string value converted to lowercase.

The replace() method to return a new string with some or all matches of a pattern replaced by a replacement. We will use one of the RegExp we just created earlier.

The split() method splits a String object into an array of strings by separating the string into sub strings.

The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM