-1

Hey everyone I have been struggling on the longest palindrome algorithm challenge in python 2.7. I am getting close but have a small error I can't figure out. I have the palindrome working, but cannot get longest palindrome to print correct, either gives me a character buffer error or prints 0.

def palindrome(string):
    string = "".join(str.split(" ")).lower() 
    i = 0
    while i < len(string):
        if string[i] != string[(len(string) - 1) - i]: 
            return False

        i += 1 
    return True
print palindrome("never odd or even")

def longest_palindrome(string): 
    best_palindrome = 0 
    i1 = 0
    while i1 < len(string):
        length = 1 
        while (i1 + length) <= len(string):
            substring = string.split(i1,length)
            if palindrome(substring) and (best_palindrome == 0 or len(substring) > len(best_palindrome)):
                best_palindrome = substring
            length += 1
        i1 += 1
    return best_palindrome
print longest_palindrome("abcbd") 
Jives
  • 59
  • 7
  • Please explain what "the longest palindrome algorithm challenge" is so we can understand what you are doing. – Rory Daulton Dec 31 '16 at 21:27
  • 2
    your palindrome function is doing something nonsense. – Saeed Amiri Dec 31 '16 at 21:33
  • I think @OP is trying to implement [Manacher's Algorithm](https://en.wikipedia.org/wiki/Longest_palindromic_substring) – Sash Sinha Dec 31 '16 at 21:34
  • @shash678, Manacher's Algorithm is way more complex, OP is doing something totally strange. – Saeed Amiri Dec 31 '16 at 21:35
  • BTW, I suggest you to read my answer [here](http://stackoverflow.com/questions/10767916/longest-palindromic-substring-and-suffix-trie), for a related question to have better simple understanding about possible solutions. – Saeed Amiri Dec 31 '16 at 21:43
  • 1
    Unspace a string with `s = s.replace(' ', ''). Since `len(string)` is a constant, assign it to a name such as `nchars`. Tracebacks should be copied and pasted if you do not understand and cannot act on it. The first argument of `string.split` cannot be an int. The result of `string.split` is a list, not a string, and passing a list to `palindrome` is also a bug. Since there is no longest palindrome in general, we cannot tell what the function is supposed to do and how to fix it. – Terry Jan Reedy Dec 31 '16 at 21:45
  • @TerryJanReedy, it's better to turn your explanation to an answer, even though with lots of edits OPs approach is far from his goal. – Saeed Amiri Dec 31 '16 at 21:59
  • hey sorry I am trying to get it to print "bcb" when the input is "abcbd" – Jives Jan 01 '17 at 20:00
  • @SaeedAmiri there is no actual purpose, it was just a random challenge I was trying to complete. – Jives Jan 01 '17 at 20:09
  • @Jives, I meany your challenge. – Saeed Amiri Jan 01 '17 at 20:53

1 Answers1

1

From what I understand, your first method was to check if a string is a palindrome or not and your second method is to find the longest palindrome.

The palindrome code that you posted always returned true no matter what the input was because

string = "".join(str.split(" ")).lower()

returns an empty string. I changed this part of your code to

string = string.replace(" ", "").lower()

which I believe gives you the desired effect of removing all spaces and making the string into lowercase.

Next, your second method should be looping through all possible substrings of the inputted string and check if a) its a palindrome and b) if it is longer than the previous largest palindrome.

An example for the string "doloa" would be:

doloa; is palindrome=false;

dolo; is palindrome=false;

dol; is palindrome=false;

do; is palindrome=false;

d; is palindrome=true; is bigger than previous large palindrome=true; 

oloa; is palindrome=false;

olo; is palindrome=true; is bigger than previous large palindrome=true;

you would continue this loop for the whole string, and in the end, your variable 'best_palindrome' should contain the largest palindrome.

I fixed your code and I believe this should work (please tell me if this is your desired output).

def palindrome(string):
    comb = string.replace(" ", "").lower() 
    # print(comb)
    # print(len(comb))
    i = 0
    while i < len(comb):
        # print(comb[i] + ":" + comb[(len(comb) - 1) - i] + " i: " + str(i) + ", opposite: " + str((len(comb) - 1) - i))
        if comb[i] != comb[(len(comb) - 1) - i]: 
        return False
        i += 1 
    return True

print palindrome("never odd or even")

def longest_palindrome(string): 
    best_palindrome = ""
    i1 = 0
    while i1 < len(string):
        length = 0
        while (i1 + length) <= len(string):
            substring = string.replace(" ", "").lower()
            substring = substring[i1:len(substring)-length]
            #print("Substring: " + str(substring))
            if palindrome(substring) and (best_palindrome == "" or len(substring) > len(best_palindrome)):
                best_palindrome = substring
            length += 1
        i1 += 1
    return best_palindrome
print longest_palindrome("bgologdds") 

Note: I change the name of some of the variables and I also added some print strings for debugging. You can delete those or uncomment them for future debugging.

ikhaliq15
  • 143
  • 1
  • 12