测试几乎是一个回文



我在大学学习Python,我们的任务是检查当前的字符串是否几乎是一个回文。

几乎是回文的字符串是一个字符串,如果你从中删除一个字符,无论哪个和只有一个字符,你都可以以相同的方式从左到右或从右到左读取字符串;例如:abbaaaaba

当我提交我的代码时,有一个自动系统会检查你的算法,系统告诉我的算法存在一些问题,我找不到问题在哪里。

这是我函数的代码:

def question3(str):#define function with input string
i=0#define counter which will run through the whole string from the begining
j=0#define counter which will run through the whole string without the char we deleted from the begining of the string
k=0#define counter which will run through the string without the deleted char from the end of the string
answer=False #define answer
while i<len(str):#run a loop through the string
k=len(str)-1
while j<len(str):# run a loop through the string without the deleted char
if i==j:# if j is in the place of the deleted chart,j skip to the next char
j=j+1
continue
if k==i:# if k is in the place of the deleted chart,k skip to the next char
k=k-1
continue
if  str[j]==str[k]:#check if the chart in the j's place equal to the char in the k's place
answer=True
else:
answer=False#if the answer is false,we don't need to check the rest of the string because it's already not almost a polyndrom
break#exit the inner loop
j=j+1#j moves to the next chart
k=k-1# k moves to the next chart
if answer==True:# if we found that the string is almost a polyndrom without a specific char,
#we don't need to check the rest of the string without the rest of the chars and we can exit the outer loop
break# exit the loop
i=i+1# move to the next chart
j=0#nullify the counter that runs through the whole string from the beginning without a specific char
print(answer)
return;

对于手头的练习来说,您的代码似乎有点复杂。这是一个更简单的版本,可以做同样的事情(我认为(。


要检查 python 中的回文,最简单的方法是这样(从这个答案(:

def check_for_palindrome(s):
return s == s[::-1]

显然,这没有短路功能,因此对于非常长的字符串,这可以实现得更快,但速度很可能不是您的任务的要求。


现在,我不完全理解你的问题,因为它可以用两种不同的方式理解; 从原始字符串中删除一个字符后:

  1. 要么你正在检查所有可能的新字符串中至少有一个是回文:

    def is_almost_palindrome_v1a(s):
    """
    'True' if there is at least one option that is palindrome.
    """
    for i in range(len(s)):
    new_s = s[:i] + s[i+1:]
    is_palindrome = check_for_palindrome(new_s)
    print(new_s, is_palindrome)
    if is_palindrome:
    return True
    return False
    

    这可以使用any()来缩短:

    def is_almost_palindrome_v1b(s):
    return any(
    check_for_palindrome(s[:i] + s[i+1:])
    for i in range(len(s)))
    
  2. 您正在检查所有可能的新字符串是否都是回文

    def is_almost_palindrome_v2a(s):
    """
    'False' if there is at least one option that is NOT a palindrome.
    """
    for i in range(len(s)):
    new_s = s[:i] + s[i+1:]
    is_palindrome = check_for_palindrome(new_s)
    print(new_s, is_palindrome)
    if not is_palindrome:
    return False
    return True
    

    这可以使用以下all()缩短:

    def is_almost_palindrome_v2b(s):
    return all(
    check_for_palindrome(s[:i] + s[i+1:])
    for i in range(len(s)))
    

感谢您的评论。我被提到了第一种方式。我们根本不允许使用任何内置库。我将代码更改为 str==str[::-1] 以检查字符串是否是回文。这是我的新代码:

def question3(str):#define function with input string
strCopy = ""  # define an empty string to copy a string without a char we want to 
delete
i = 0  # define counter which will run through the original string
j = 0  # define first help counter
answer = False  # define answer
while i < len(str):  # run a loop through the original string
while j < len(
str):  # a loop where the original string will be copied to a new string without the deleted char
if i == j:  # check if the current char is the char we want to delete,if yes,skip to the next char
j = j + 1
continue
strCopy = strCopy + str[j]  # add the char in the j'th place from the original string to the new string
j = j + 1#moving to the next char
if strCopy==strCopy[::-1]:#check if the string without the deleted char is a palindrome,if yes,so the original string is almost a palindrome
answer=True
break
i = i + 1#moving to the next char
strCopy = ""#nullify the string without the deleted char
j = 0#nullify j
print(answer)  # print the result(True/False)
return;

最新更新