Python Forum
Palindromes - Python Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Palindromes - Python Help
#1
Question 
Hello everyone Big Grin ,

I have a problem with my code.

I must write a program that reads a string and from it obtain all the palindromes that can be obtained.

It must not write palindromes less than two letters and only must appear once.

The problem that I have in my code and that I cannot solve it, is that it prints the palindromes of a single character and they should not appear.

##############
###EXAMPLE:###
##############


Input: madam

Output: madam
maam
mdm
mam
mm
ada
aa


If anyone is able to define a function that allows this, I would appreciate it.
I leave the code that I have attached:
def find_all_subsets(seq, n):
    if n == 0:
        return [[]]
    else:
        result = []
        subsets = find_all_subsets(seq, n-1)

        for subset in subsets:
            result += [subset]
            result += [[seq[n-1]] + subset]
        return result

def check_palindrome(subsetsList):
    finalList = []
    for set in subsetsList:
        if set[::-1] == set:
            finalList.append(set)
        else:
           continue
    return finalList

if __name__ == "__main__":
    word = input("Write string: ")
    palindromicSubsets = check_palindrome(find_all_subsets(word, len(word)))
    print(palindromicSubsets)
Thank you very much! Heart
Reply
#2
def isPalindrome(str):
    attempt = str.lower()[::-1]

    if attempt == str.lower():
        print(f"{str} Is a palindrome")
    else:
        print(f"{str} Is not a palindrome")
    return  attempt

# main function
s = "Kayak"
ans = isPalindrome(s)
Output:
Kayak Is a palindrome
Reply
#3
Hello, thank for your replay, but my program try to find this:

input: kayak

Output: kayak
kaak
kak
kyk
aa
kk

All palindroms that you can make with an input string, doesn't matter if the word that you give as an input is or not a palindrome.

Anyway really thanks for your help ^^
Reply
#4
Question 
Hello everyone Big Grin ,

I have a task to do and I have the code, but with some problems.

The task is:
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:
1. do not print palindromes of the length 1 (one-letter palindromes).
2. each palindrome is printed exactly once.

EXAMPLES
Input: recursion
Output:
rur
rer
rcr
rr

Input: print
Output:

Input: madam
Output:
madam
maam
mdm
mam
mm
ada
aa


The program I have written is:
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)
But I do not understand why it does not return the final result to check if it is correct or not.

Thank you very much for your help Heart
Reply
#5
Question 
Hello everyone Big Grin ,

I have a recursion statement with python Angel .

STATEMENT

PALINDROMES

A palindrome is a word which reads the same backwards as forwards (madam, noon, radar…). 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.

Examples:
Input: madam
Output:
madam
maam
mdm
mam
mm
ada
aa

Input: recursion
Output:
rur
rer
rcr
rr

Input: print
Output:

I have written the following code that works perfectly, but in the second defined function I have not used recursion because I did not know how to approach this part of the statement. If someone can help me I would be so thankful Heart Heart .

def find_subsets(seq, n):
    if n != 0:
        result = []
        subsets = find_subsets(seq, n-1)
        for subset in subsets:
            result += [subset]
            result += [[seq[n-1]] + subset]
        return result
    else:
        return [[]]
def check_palindrome(subsetsList):
    finalList = []
    for x in subsetsList:
        if x[::-1] == x and len(x)>1:
            string = " ".join(x)
            print(string)
        else:
           continue
word = input("Write string: ")
check_palindrome(find_subsets(word, len(word)))
buran write Apr-04-2021, 05:28 PM:
Please, stop creating new threads unnecessarily.
Reply
#6
Whoa.
Take your original program and fix the one problem. Now, you do have an issue in using a keyword (set) as a variable name and that is an issue with the solution. So, I changed the variable to st. Then, check the length of the set() on your result. set() eliminated duplicates.
def find_all_subsets(seq, n):
    if n == 0:
        return [[]]
    else:
        result = []
        subsets = find_all_subsets(seq, n-1)
 
        for subset in subsets:
            result += [subset]
            result += [[seq[n-1]] + subset]
        return result
 
def check_palindrome(subsetsList):
    finalList = []
    for st in subsetsList:
        if st[::-1] == st:
            if len(set(st)) > 1:
                finalList.append(st)
        else:
           continue
    return finalList
 
if __name__ == "__main__":
    word = input("Write string: ")
    palindromicSubsets = check_palindrome(find_all_subsets(word, len(word)))
    print(palindromicSubsets)
Works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  finding almost palindromes leahma 1 3,669 Oct-20-2018, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020