Restriction enzymes recognise short reverse palindromes — sequences that read the same
on both strands — and cut there. Finding every such site in a plasmid is the first step in planning any
cloning experiment, because it tells you where you can cut and what fragment sizes you will see on a
gel. The reverse palindrome concept is also a good test of whether you really understand strand
complementarity, since a DNA palindrome is not the same thing as a text palindrome.
Statement
A reverse palindrome is a DNA substring that equals its own reverse complement.
Given a DNA sequence, find every reverse palindrome of length between 4 and 12 inclusive.
For each one, print its 1-based starting position and its length, separated by a single space, on its
own line. Sort the output by starting position ascending, and by length ascending where positions tie.
Input — read from standard input
| Variable | Type | Description |
|---|---|---|
dna
line 1
|
str |
The DNA sequence to scan for restriction sites
4 <= len(dna) <= 1000, uppercase A, C, G, T only
|
These variables are already read for you in the starter code on the right.
Output
str one line per site, 1-based position and length separated by a space, sorted by position then length
Sample Cases
TCAATGCATGCGGGTCTATATGCAT
4 6
5 4
6 6
7 4
17 4
18 4
20 6
21 4
GCGC
1 4
Submit also runs your code against 5 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.
Constraints
4 <= length(dna) <= 1000- Only palindromes of length 4 to 12 inclusive are reported
- Positions are 1-based
- Output ordering is position ascending, then length ascending
Further Reading
- A reverse palindrome always has even length, so you only need to test lengths 4, 6, 8, 10, 12.
- Write a
revcomphelper once and reuse it — do not inline the complement logic in the loop. - Guard the window:
i + Lmust not exceed the sequence length.