测试几乎 我在大学学习Python,我们的任务是检查当前的字符串是否几乎是一个文明。 一根几乎一个alindroome的字符串是一个字符串,如果您从中删除一个char,则不...

问题描述 投票:0回答:1

aa

aba

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

这是我功能的代码:
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中的alindrinmes,最简单的方法是(来自this Answer

):

def check_for_palindrome(s):
    return s == s[::-1]
显然,这没有短路功能,因此对于很长的字符串,这可以实现更快,但是速度很可能不是您的分配的要求。
python string python-3.x
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)))

您正在检查所有可能的新字符串都是alindromes
  1. 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)))
    

        
  2. thank的评论。我被提到了第一种方式。我们根本不允许使用任何内置库。我将代码更改为str == str [::-1],以检查字符串是否为palindrome。这是我的新代码:
  3. 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;

    
    

    def check_palindrom(s): right = len(s)-1 count = 0 fl = len(s) // 2 for i in range(fl): if s[i] != s[right]: count += 1 if count > 1: return "String is not palindrom" right -= 1 return "String is almost palindrom" if count == 1 else "String is palindrom" s="abcdcbeea" print(check_palindrom(s))

        

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.