测试几乎回文

时间:2018-10-20 11:00:37

标签: python string python-3.x

我正在大学里学习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;

2 个答案:

答案 0 :(得分:0)

您的代码对于手头的练习似乎有点复杂。这是一个更简单的版本(我认为)。


要检查python中的回文,最简单的方法是这样(来自this answer):

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)))
    

答案 1 :(得分:0)

谢谢您的评论。有人提到我是第一种方式。我们根本不允许使用任何内置库。我将代码更改为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;