Python Forum

Full Version: Help with Python - Palindromes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I have a problem with getting the result of my program Confused .

This is a task on palindromes.

STATEMENT
Write a program which reads a string and prints all palindromes, which can be obtained by removing of some characters of the string. The resulting palindromes do not need to be English words!
Restrictions:
  • do not print palindromes of the length 1 (one-letter palindromes)
  • each palindrome is printed exactly once
I have written this code but I do not understand why it does not print the final result Cry .

If anyone can find the problem and fix it, I appreciate it Heart .

CODE
def check_palin(word):
    if word==word[::-1]:
            return True
    else:
        return False
 
def all_palindromes(string):
   result=list()
   st1=""
 
   for i in range(0,len(string)):
        st1 += string[i]
        result.append(string[i])
        for j in range(i+1,len(string)):
           st1+=string[j]
           if check_palin(st1)==True:
                result.append(st1)
        st1=""
    
   return list(set(result))

w = input("Write any string to find all palindromes: ")
all_palindromes(w)
You don't have a call to print anywhere.

Also, the if and else in check_palin are redundant.
(Apr-04-2021, 02:27 PM)ndc85430 Wrote: [ -> ]You don't have a call to print anywhere.

Also, the if and else in check_palin are redundant.


I need to call the function to can see the result if it works good or not :S I tried with
all_palindromes(w)
but still not bring the result on the screen and really I dont know why.

This if and else are just to check if the 1st string that I write as input is or not a palindrome, I don't know if is it the best option to do that but I am really new on python.
Well, you aren't printing anything are you? Why would you expect anything else to happen? All you do is call the function and throw its return value away.
Please, keep the discussion here https://python-forum.io/Thread-Palindrom...#pid140162
I close this thread now.
STOP creating new threads!