r/learnbioinformatics • u/lc929 • Jul 27 '15
[Week of 2015-06-26] Programming Challenge #1: Longest palindrome in a string
Programming Challenge #1: Longest Palindrome in a String
Problem
Find the maximum-length continguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana".
Significance in Biology
Most genomes contain palindromic motifs. Palindromic DNA sequence may form a hairpin, restriction endonuclease target sites, and methylation sites.
Sample input & output
Input 1:
CATGTAGACAGAGTAGCTA
Output 1:
AGACAGA
Input 2:
AMANAPLANACANALPANAMA
Output 2:
AMANAPLANACANALPANAMA
Input 3:
CGACTTACGTACGTAGCTAGCTAC
Output 3:
TT
Notes
- Please post your solutions in whatever language and time/space complexity you feel comfortable in.
- Remember that we are all here to learn!
- Problem too easy, or too hard, or not relevant enough? Feel free to message the mods with feedback!
5
Upvotes
6
u/blaze99960 Jul 27 '15
Sorry Iammnub, but that wouldn't quite work. What you'd end up with is all the letters that match up if you treat the whole sequence as a palindrome, but you wouldn't be able to see palindromes that start anywhere other than the very end of the string. The logic I would use (I MAY get around to it) is: For every letter in the sequence, check whether it is the same as either of its neighbors or whether it's neighbors are identical. If so, beginning at that point consecutively check each new letter going outwards until the palindrome ceases or you reach the end of the string. Have a variable where you store the longest palindrome found so far (and probably another with its location). After checking every letter of the string, output the longest palindrome.
4
u/Raskolnikov6 Jul 30 '15
Edit: this is camdo2 on a different account, my account isn't posting correctly.
I was thinking about /u/blaze99960 's theoretical implementation but ran into the fact that you're going to have to check for palindromes on both sides and adjacent. For example:
B A A Bif we're searching on either of the A's only by the characters on both sides you would miss a palindrome. so you have to still search the adjacent character alone to see if it matches as the middle of the palindrome doesn't necessarily have to "center" around one character, it could center around two.I was doing some searching online and ran into a really elegant way to avoid having to iterate both of these decisions. Basically, you put in a "junk" character before and after every character. That way you can create an artificial "center" for palindromes that center around two characters instead of one.
To demonstrate:
$ B $ A $ A $ B $If we are searching outwards from the center dollar sign we will pick up the two characters that are, in actuality, adjacent to each other. But this is a shortcut to prevent us from doing two separate operations while iterating over the text...So, now, onto my implementation: http://pastebin.com/gDkKNjq5
As an aside: I really hope we keep running these. This subreddit has a ton of potential.
2
u/camdo2 Jul 28 '15 edited Jul 28 '15
this was tough but I think I got it. basically, it generates a list of lists containing every index of any character. any palindrome will start and end with the same character so then it's pretty trivial to iterate through these indices and generate palindrome candidates and test them. I know I could save some performance by doing some tweaks but this is the best outline I could think of for this problem (I just graduated from high school so I'm not too familiar with analysis of algorithms)
is there an easier way to format code on reddit? I had to go through manually and do it--is there something easier?
2
u/AJs_Sandshrew Jul 29 '15
Just to clarify something, a palindrome in the context of a single text string is what you described: a word that can be read the same forward and backward (e.g. RACECAR, NOON, DAD)
However, in biology a palindromic sequence refers to a sequence that is read the same in the 5' to 3' direction on both the sense and antisense strand. For example:
The restriction enzyme BamHI recognizes the sequence GGATCC. When you write the complementary strand below it, you'll notice that it reads the same on both strands in the 5->3 direction.
The advantage of this is that the enzyme BamHI can recognize the sequence from either direction ("top" or "bottom") because the 3D structure of the sequence above has 180 degree rotational symmetry.
Also, the reason these sequences can form hairpins is because they can loop in and bind to themselves. Here is a simple example:
can loop into:
This wouldn't add too much complexity to the problem proposed here, but I though it was something that should be addressed.
1
u/lc929 Aug 03 '15
good job everyone! here's my writeup for this problem.
1
u/lammnub Jul 27 '15
I'm still learning python so I'll take a stab at it once I finish the codecademy course. But can anyone comment on my logic of how to solve this? And maybe point me into a more efficient direction?
Get an input and create a second variable that is the reverse of the input. Create a list of 0s and 1s where 1s mark that the two letters are the same. Find the longest sequence of 1s and compare that list to the original input and print out where the 1s line up?
3
u/blaze99960 Jul 30 '15 edited Jul 30 '15
Here's my solution. I've barely remember program analysis, but the idea is as in my previous post: iterate through each letter of the string and check whether it's the center of a palindrome, starting with its two neighbors and going outwards until the palindrome ends. Written in Python 3.2
2
u/BioGeek Jul 30 '15
Brute force solution, there are probably smarter ways to do this. takes about ten seconds on my machine to return the answer for the sample sequence at the end.
1
u/camdo2 Jul 29 '15 edited Jul 29 '15
I was thinking about /u/blaze99960 's theoretical implementation but ran into the fact that you're going to have to check for palindromes on both sides and adjacent. For example:
B A A Bif we're searching on either of the A's only by the characters on both sides you would miss a palindrome. so you have to still search the adjacent character alone to see if it matches as the middle of the palindrome doesn't necessarily have to "center" around one character, it could center around two.I was doing some searching online and ran into a really elegant way to avoid having to iterate both of these decisions. Basically, you put in a "junk" character before and after every character. That way you can create an artificial "center" for palindromes that center around two characters instead of one.
To demonstrate:
$ B $ A $ A $ B $If we are searching outwards from the center dollar sign we will pick up the two characters that are, in actuality, adjacent to each other. But this is a shortcut to prevent us from doing two separate operations while iterating over the text...So, now, onto my implementation: http://pastebin.com/gDkKNjq5
As an aside: I really hope we keep running these. This subreddit has a ton of potential.