-3

I'm studying Python at the university and our task was to check if a current string is almost a palindrome.

A string that almost a palindrome is a string that if you delete one char from it, no matter which and only one char,you can read the string in the same way from left to right or right to left; for example: abba, aa and aba.

When I submit my code, there is an automatic system that checks your algorithm and the system tells that there are some problems with my algorithm and I can't find where the problems are.

This is the code of my function:

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;
ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
Ron Tyntarev
  • 1
  • 1
  • 3
  • Have you done any testing yourself? Throw data at it until you find a fail case, then begin debugging it. – Carcigenicate Oct 20 '18 at 11:01
  • 1
    If I delete the first `a` from "aba" it becomes "ba" which is not palindrome. So your example doesn;t look good. The same applies if I delete the last `a`. If I delete first 'a' from "abba", it becomes "bba" which is not palindrome either – Sheldore Oct 20 '18 at 11:02
  • @RonTyntarev I agree with @Bazingaa, the two examples `abba` and `aba` do not conform with your definition, because if you delete any letter `a` from those two examples, they are not converted to palindromes. Are you sure that those are valid examples? – Ralf Oct 20 '18 at 11:17
  • Yeah,i've checked my code and all the examples that I checked were true. I meant that a string is almost a palindrome if you can delete any char from it,not necessarily the first char and the rest of the string,you can read the same nevertheless if it from left to right or right to left. If we delete b from aba,it's a palindrome . I don't have any more ideas for data to check. – Ron Tyntarev Oct 20 '18 at 13:06

2 Answers2

1

Your code seems a bit complex for the excercise at hand. Here is a simpler version that does the same (I think).


To check for palindromes in python, the simplest way is this (from this answer):

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

Obviously this has no short-circuit functionality, so for VERY long strings this can be implemented to be faster, but speed is most likely not a requirement for your assignment.


Now, I don't understand your question entirely, because it can be understood in two different ways; after removing one char from the original string:

  1. either you are checking that at least one from all possible new strings is a palindrome:

    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
    

    This can be shortened using any():

    def is_almost_palindrome_v1b(s):
        return any(
            check_for_palindrome(s[:i] + s[i+1:])
            for i in range(len(s)))
    
  2. you are checking that all possible new strings are palindromes

    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
    

    This can be shortened using all():

    def is_almost_palindrome_v2b(s):
        return all(
            check_for_palindrome(s[:i] + s[i+1:])
            for i in range(len(s)))
    
Ralf
  • 16,086
  • 4
  • 44
  • 68
  • He means: For word = "abccbx", the output should be isAlmostPalindrome(word) = true. It becomes a palindrome after modifying the character x to a. If you modify only one char and it will turn to a palindrome and if you do not have to modify (as above). – Timo Dec 01 '20 at 20:02
0

Thank's for your comment. I was mentioned to the first way. We're not allowed to use any built in libraries at all. I changed my code to str==str[::-1] to check if a string is a palindrome. Here's my new code:

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;
Ron Tyntarev
  • 1
  • 1
  • 3