Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a list of palindromes with only 'x','y' and a given length n in Python?

is there any patterns i can use to sort out how to create a string that is palindrome which made up with 'X' 'Y'

like image 797
user3426141 Avatar asked Nov 01 '25 22:11

user3426141


1 Answers

Let's assume n is even. Generate every string of length n/2 that consists of x and y, and append its mirror image to get a palindrome.

Exercise 1: prove that this generates all palindromes of length n.

Exercise 2: figure out what to do when n is odd.

like image 175
NPE Avatar answered Nov 03 '25 13:11

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!