Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Bug

##Bug WhetherWhether we use [::-1] or reversed() to reverse the input string, we have a bug when the input contains characters that are formed using combining accents, as demonstrated by these additional tests:

##Bug Whether we use [::-1] or reversed() to reverse the input string, we have a bug when the input contains characters that are formed using combining accents, as demonstrated by these additional tests:

Bug

Whether we use [::-1] or reversed() to reverse the input string, we have a bug when the input contains characters that are formed using combining accents, as demonstrated by these additional tests:

A minor typo and a pasto
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
def is_palindrome(x):
 '''
 True if the letters of x are the same when
 reversed, ignoring differences of case.
 >>> is_palindrome('')
 True
 >>> is_palindrome('a')
 True
 >>> is_palindrome('a0')
 False
 >>> is_palindrome('ab')
 False
 >>> is_palindrome('ab a')
 False
 >>> is_palindrome('Aba')
 True
 >>> is_palindrome('Àbà')
 True
 '''
 x = x.lower()
 # Alternative (letters only):
 # x = ''.join(filter(str.isalpha, x.lower()))
 # Alternative (letters and digits only):
 # x = ''.join(filter(str.isalnum x.lower()))
 return x[::-1] == x
if __name__ == '__main__':
 import sys
 for string in sys.argv[1:]:
 # check each command-line argument
 message = 'is a palindrome' if is_palindrome(string) else 'is not a palindrome'
 print(f'{string} {message}')
 else:
 # no arguments, so run serfself-test
 import doctest
 doctest.testmod()
True
>>> is_palindrome("o̅o") # o+overline, o
False
>>> is_palindrome("o̅o̅") # o+overline, o+overline
True
def is_palindrome(x):
 '''
 True if the letters of x are the same when
 reversed, ignoring differences of case.
 >>> is_palindrome('')
 True
 >>> is_palindrome('a')
 True
 >>> is_palindrome('a0')
 False
 >>> is_palindrome('ab')
 False
 >>> is_palindrome('ab a')
 False
 >>> is_palindrome('Aba')
 True
 >>> is_palindrome('Àbà')
 True
 '''
 x = x.lower()
 # Alternative (letters only):
 # x = ''.join(filter(str.isalpha, x.lower()))
 # Alternative (letters and digits only):
 # x = ''.join(filter(str.isalnum x.lower()))
 return x[::-1] == x
if __name__ == '__main__':
 import sys
 for string in sys.argv[1:]:
 # check each command-line argument
 message = 'is a palindrome' if is_palindrome(string) else 'is not a palindrome'
 print(f'{string} {message}')
 else:
 # no arguments, so run serf-test
 import doctest
 doctest.testmod()
True
>>> is_palindrome("o̅o") # o+overline, o
False
>>> is_palindrome("o̅o̅") # o+overline, o+overline
True
def is_palindrome(x):
 '''
 True if the letters of x are the same when
 reversed, ignoring differences of case.
 >>> is_palindrome('')
 True
 >>> is_palindrome('a')
 True
 >>> is_palindrome('a0')
 False
 >>> is_palindrome('ab')
 False
 >>> is_palindrome('ab a')
 False
 >>> is_palindrome('Aba')
 True
 >>> is_palindrome('Àbà')
 True
 '''
 x = x.lower()
 # Alternative (letters only):
 # x = ''.join(filter(str.isalpha, x.lower()))
 # Alternative (letters and digits only):
 # x = ''.join(filter(str.isalnum x.lower()))
 return x[::-1] == x
if __name__ == '__main__':
 import sys
 for string in sys.argv[1:]:
 # check each command-line argument
 message = 'is a palindrome' if is_palindrome(string) else 'is not a palindrome'
 print(f'{string} {message}')
 else:
 # no arguments, so run self-test
 import doctest
 doctest.testmod()
>>> is_palindrome("o̅o") # o+overline, o
False
>>> is_palindrome("o̅o̅") # o+overline, o+overline
True
added 95 characters in body; added 31 characters in body
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325

Usage of thisthese optional arguments should be apparent from the extra tests I've added.

We##Bug Whether we use [::-1] or reversed() to reverse the input string, we have a bug withwhen the input contains characters that are formed using combining accents, as demonstrated by these additional tests:

Usage of this should be apparent from the extra tests.

We have a bug with characters that are formed using combining accents, as demonstrated by these additional tests:

Usage of these optional arguments should be apparent from the extra tests I've added.

##Bug Whether we use [::-1] or reversed() to reverse the input string, we have a bug when the input contains characters that are formed using combining accents, as demonstrated by these additional tests:

Removed combining-char tests accidentally copied
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Fix tests to match new code; describe the combining-character bug
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Add test of tolower equivalence and space removal
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Update according to commenters' suggestions
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /