我是python3新手,我试着测试我的回文代码是否有效,并通过了大多数边缘情况。因此,我在下面创建了以下2个python文件。当我在终端上运行python3 test_palindromes.py时,它显示有2个测试通过了。我想知道这对我的回文功能是否足够。我想我已经用尽了所有的案子。
这是我的palindromes.py
#!python
# Hint: use string.ascii_letters (all letters in ASCII character set)
import string
def is_palindrome(text):
"""A string of characters is a palindrome if it reads the same forwards and
backwards, ignoring punctuation, whitespace, and letter casing"""
# implement is_palindrome_iterative and is_palindrome_recursive below, then
# change this to call your implementation to verify it passes all tests
assert isinstance(text, str)
return is_palindrome_iterative(text)
# return is_palindrome_recursive(text)
def is_palindrome_iterative(text):
# TODO: implement the is_palindrome function iteratively here
# once implemented, change is_palindrome to call is_palindrome_iterative
# to verify that your iterative implementation passes all tests
"""str.isalpha()
Return true if all characters in the string are alphabetic and there is at
least one character, false otherwise. Alphabetic characters are those
characters defined in the Unicode character database as “Letter”, i.e.,
those with general category property being one of “Lm”, “Lt”, , “Ll”, or “Lo”.
Note that this is different from the “Alphabetic” property
defined in the Unicode Standard. (edited)"""
# First, setting up 2 pointer. First and last pointer.
first_pointer = 0
last_pointer = len(text) - 1
# iteration through when the first index is less than the last index
while(first_pointer <= last_pointer):
# set up different while loop condition to do comparison
# test different condition of the palindrome cases
#
# Get letters only
while not text[first_pointer].isalpha():
first_pointer += 1
if first_pointer > len(text) - 1:
return True
while not text[last_pointer].isalpha():
last_pointer -= 1
if last_pointer < 0:
return True
# Not same, return
if(text[first_pointer].lower() != text[last_pointer].lower()):
return False
first_pointer += 1
last_pointer -= 1
return True
def main():
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) > 0:
for arg in args:
is_pal = is_palindrome(arg)
result = 'PASS' if is_pal else 'FAIL'
str_not = 'a' if is_pal else 'not a'
print('{}: {} is {} palindrome'.format(result, repr(arg), str_not))
else:
print('Usage: {} string1 string2 ... stringN'.format(sys.argv[0]))
print(' checks if each argument given is a palindrome')
if __name__ == '__main__':
main()我的test_palindrome.py
import unittest
def test_is_palindrome_with_mixed_casing_and_punctuation(self):
# palindromes with whitespace, punctuation and mixed letter casing
assert is_palindrome('No, On!') is True
assert is_palindrome('Dog God?') is True
assert is_palindrome('Taco? Cat.') is True
assert is_palindrome('Race-Car!!!') is True
assert is_palindrome('Race Fast, Safe Car...') is True
assert is_palindrome('Was it a car or a cat I saw?') is True
assert is_palindrome("Go hang a salami, I'm a lasagna hog.") is True
assert is_palindrome('A man, a plan, a canal - Panama!') is True
def test_is_palindrome_with_non_palindromic_strings(self):
# examples of non-palindromic strings that should be rejected
assert is_palindrome('AB') is False # even length
assert is_palindrome('ABC') is False # odd length
assert is_palindrome('AAB') is False
assert is_palindrome('AABB') is False
assert is_palindrome('AAABB') is False
assert is_palindrome('AAABBB') is False
assert is_palindrome('ABCZBA') is False
assert is_palindrome('ABCCZA') is False
assert is_palindrome('ABCCBZ') is False
assert is_palindrome('ABCDZCBA') is False
assert is_palindrome('ABCDDZBA') is False
assert is_palindrome('ABCDDCZA') is False
assert is_palindrome('ABCDDCBZ') is False
assert is_palindrome('AAAAZAAA') is False
assert is_palindrome('AAAAAAAZ') is False
if __name__ == '__main__':
unittest.main()python3 palindromes_test.py
在0.001s进行了7次测试
发布于 2017-10-30 08:13:19
import sysis_palindrome中,您有这个assert isinstance(text, str),但是不要使用它。您的is_palindrome_iterative可以简化得更多。因为普通回文只是检查单词是否与倒转相同。现在,带标点符号的单词也是如此,但我们只需先删除它们。
当我运用我的想法时,我最终会得到这样的结果。它可能不是迭代的,但它看起来更短/更干净。
我首先删除标点符号,用一个列表理解和一个检查如果一个字母在string.ascii_letters之后,把这个词连接在一起,并检查它是否是相同的反向。
def is_palindrome(text):
"""returns True if the text is a palindrome, with punctuation omitted"""
text = ''.join([i for i in text.lower() if i in string.ascii_letters])
return text == text[::-1]现在,如果我们想要回文至少是两个字母,我们可以添加一个if检查预先检查的长度。
https://codereview.stackexchange.com/questions/179157
复制相似问题