DNA to RNA Transcription code in Python 3.6 failing 'ACGTXXXCTTAA' test

Question:

I am trying to solve DNA to RNA Transcription problem but my code is unable to pass this test case: ACGTXXXCTTAA

The transcription should be as follows:

G --> C

C --> G

T --> A

A --> U

Here’s my code:

dna = input()
new = ""

for i in dna:
    if i not in 'ATGC':
        print("Invalid Input")
        break
    if i == 'A':
        new += 'U'
    elif i == 'C':
        new += 'G'
    elif i == 'T':
        new += 'A'
    else:
        new += 'C'

print(new)

This code passes all tests except aforementioned one. The correct output should be:

Invalid Input

The output my code gives:

Invalid Input
UGCA

I have tried many iterations but I cannot get rid of UGCA. Even though I use break to get out of the loop in case of any Invalid Input the code is able to transcribe ACGT before XXX and print the result when it should just output Invalid Input.

Can anyone see what I am missing?

Asked By: Bikramjeet Singh

||

Answers:

You always print new so instead using print("Invalid Input") you can simply change the value of new to ‘Invalid Input’ and print. That should fix your problem!

dna = input()
new = ""

for i in dna:
    if i not in 'ATGC':
        new = "Invalid Input"
        break
    if i == 'A':
        new += 'U'
    elif i == 'C':
        new += 'G'
    elif i == 'T':
        new += 'A'
    else:
        new += 'C'

print(new)
Answered By: Grant Williams

You could try something like this for brevity and clarity

DNA_TO_RNA_MAP = {'G': 'C', 'C': 'G', 'T': 'A', 'A': 'U'}

dna = input()

if any(base not in DNA_TO_RNA_MAP.keys() for base in dna):
    print('Invalid Input')
else: 
    print(''.join(DNA_TO_RNA_MAP[base] for base in dna))
Answered By: bphi

This is a neat problem because it introduces so many concepts at once! Your direct problem is that even when you throw the Invalid Input error, you still print new which is the partially transcribed sequence. To fix that, you could have your error handling code throw an exception, or simply blank out the result. I would probably turn this into a function and throw an exception.

def dna_to_rna(dna):
    acc = []
    for ch in dna:
        if ch not in 'ATGC':
            raise ValueError("Invalid Input")
        if ch == 'A':
            acc.append('U')
        # ... and etc for each alternation
    return ''.join(acc)  # return the correct complete sequence
                         # only after all characters are processed

try:
    result = dna_to_rna(whatever_sequence)
except ValueError as e:
    print(e)

However this is also a great case to use a dictionary for lookups, rather than a big long if/elif/else block

mapping = {'A': 'U',
           'C': 'G',
           'T', 'A',
           'G', 'C'}
try:
    result = ''.join([mapping.get(ch) for ch in dna])
except TypeError:
    # if any member of `dna` is not ATCG, it will try to `''.join` over
    # a None value, which throws a TypeError
    print("Invalid Input")
else:
    print(result)
Answered By: Adam Smith
b=input()
a="GCTA"
c="CGAU"
try:print(''.join([c[a.index(i)]for i in b]))
except:print("Invalid Input")
Answered By: ABHIRUP ROY
new = ''
dna = input()

for i in dna:
    if i not in "GCTA":
        print('Invalid Input')
        break
else:
    for j in dna:
        if j == 'G':
            new += 'C'
        elif j == 'C':
            new +='G'
        elif j == 'T':
            new +='A'
        elif j == 'A':
            new +='U'
        else:
            print("Invalid Input")
print(new)
Answered By: Abhilash M

path = input(‘nEnter the file path…nn’)

with open(path, ‘r’) as file:
dna = file.read()

mrna = dna.upper().translate(str.maketrans(‘CGAT’, ‘GCUA’))

Answered By: SuperJugger88
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.