0

I made a recursive function which takes a binary code and is supposed to convert to the letters "A", "T", "C" and "G".

Then in this, it's supposed to assign that binary code to the corresponding letters below, but it just doesn't move past the first letter. Can any of you guys help me sort this out?

def numeros(i,f):
    e="100101"
    if i >=len(e):
        print f
    else:
         if e[i:i+1]=="1":
            f=f+"G"
            print f
            numeros(i+1,f)
         elif e[i:i+3]=="011":
            f=f+"T"
            print f
            numeros( i+3,f)
         elif e[i:i+3]=="010":
            f=f+"A"
            print f
            numeros( i+3,f)
         elif e[i:i+3]=="00":
            f=f+"C"
            print f
            numeros( i+3,f)

numeros(0,"")
2
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable Example. Commented Jan 9, 2016 at 22:29
  • You have a typo. When you check for "C", you have to do elif e[i:i+2] == '00': and in the subsequent call numeros(i+2, f). It's also not necessary to do recursively. It can be done with the same logic iteratively. You're effectively decoding a prefix code. Commented Jan 9, 2016 at 22:59

2 Answers 2

1

Without getting into the merits of the approach, and simply looking at the problem (lack of recursion) I suggest that now's a good time to start looking at debugging techniques and tools. It might be worthwhile looking at the python debugger, or simply adding some print statements so that you can get an idea of what's going on in your code.

At the very least when you have an if/elif chain it's always a good idea to have an else at the bottom, even if you never expect the code to get there, to print out a message or raise an exception indicating that something unexpected has happened. If you do this you'll see that there's an error in your elifs that's preventing the code from getting to any of the recursive calls and you need to double check your conditional tests.

Sign up to request clarification or add additional context in comments.

Comments

0

Recursion is not a good fit for this problem and will crash on any input string over 1000 tokens long (== default depth of Python's call stack).

I suggest instead:

CODES = {
    "1"  : "G",
    "00" : "C",
    "010": "A",
    "011": "T"
}

def decode(string):
    token = ""
    message = []
    for digit in string:
        token += digit
        if token in CODES:
            message.append(CODES[token])
            token = ""
    return "".join(message)

which gives

>>> print(decode("100101"))
GCG

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.