I want to make a program that cross checks strings.

Here is an example :

Enter line: AAGGAA
This sequence can make a hairpin

Enter line: UGAG
This sequence cannot make a hairpin

Enter line: GUGCCACGGCACCGUG
This sequence can make a hairpin

Enter line: GUACCACGGCACCGUG
This sequence cannot make a hairpin

My challenge states

Your task is to write a program which reads in a sequence of RNA and determines whether or not it could, if folded in the middle, form a hairpin structure. All sequences that we use to test your program will be of even length.

Basically it splits strings which are even and cross checks those strings if they can make a hairpin sequence.

so if the string AAGGAA was entered,
it would be split into AAG and GAA and the first letter of string one has to match the last letter of string two, and so on.

Can someone please help me?
I've tried coding it but have had no luck :(

Dani AI

Generated

As and noted, the simplest interpretation used in the thread is a midpoint mirror check: split an even-length sequence in half, reverse the second half, and compare. That identity test is compact and precisely what passed the OP's examples. For many programming exercises that is the intended solution.

A biologically realistic "hairpin" is different: the two halves must form complementary base pairs (A<->U and C<->G in RNA, with optional G–U wobble). If the goal is a true RNA pairing check rather than a character-for-character mirror, the comparison must test complements rather than equality. The short function below gives both options and normalizes common input issues (case, T->U, even-length check, invalid characters).

def check_hairpin(seq, mode='identity', allow_wobble=False):
    s = seq.strip().upper().replace('T', 'U')
    n = len(s)
    if n % 2 != 0:
        raise ValueError("sequence length must be even")
    left = s[:n//2]
    right_rev = s[n//2:][::-1]
    if not set(s).issubset(set("AUCG")):
        raise ValueError("only A,C,G,U (or T) allowed")
    if mode == 'identity':
        return left == right_rev
    complements = {'A': {'U'}, 'U': {'A'}, 'C': {'G'}, 'G': {'C'}}
    if allow_wobble:
        complements['G'].add('U'); complements['U'].add('G')
    for a, b in zip(left, right_rev):
        if b not in complements.get(a, set()):
            return False
    return True

Notes: use mode='identity' to reproduce the palindrome-style answers from earlier posts, or mode='complement' (with allow_wobble=True if desired) for a chemistry-accurate test. Also validate input (even length, allowed letters) before testing to avoid surprising failures.

Recommended Answers

All 4 Replies

split the RNA sequence at the midline and reverse the second part then compare the 2parts.
something like a function that takes the sequence and returns the following:

return sequence[:len(sequence)/2]==sequence[len(sequence)/2:][::-1]

It's called palindrome and easiest is to just comparenstrimg with reversed string. But your example GUA....GUG is not one!

something like this maybe:

def hairpin(seq):
    print seq==seq[::-1]

hairpin(raw_input("Enter RNA Sequence: "))

Thanks guys! It passed :D
The function I used was
return sequence[:len(sequence)/2]==sequence[len(sequence)/2:][::-1]

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.