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.