DNA.py no suspect - the return of keyser soze


#1

Hello.

My DNA Analysis Code is correct, w/o errors, but it didn’t find the usual suspect (!)
I expect to find 1 criminal amongs 3 suspects, but I find no felons and I have to release them all
Please, could someone help me ?

This is the exercise link:
https://www.codecademy.com/courses/learn-python/projects/dna

…and here is the code:

sample = ['GTA','GGG','CAC']
def read_dna(dna_file):
  dna_data = ""
  with open(dna_file, "r") as f:
    for line in f:
      dna_data+=line
  return dna_data

def dna_codons(dna):
  codons = []
  for i in range(0,len(dna),3):
    if i > (i + 3) < len(dna):
      codons.append(dna[i:i+3])
  return codons
  
def match_dna(dna):
  matches = 0
  for codon in dna:
    if codon in sample:
      matches+=1
  return matches

def is_criminal(dna_sample):
  dna_data = read_dna(dna_sample)
  codons = dna_codons(dna_data)
  num_matches = match_dna(codons)
  if num_matches >= 3:
    print "Matches: %i. Check the result with other suspects" % num_matches
  else:
    print "Matches: %i. The suspect should be released." % num_matches
      
is_criminal('suspect1.txt')
is_criminal('suspect2.txt')
is_criminal('suspect3.txt')




#2

Please explain the above line. Thanks.


Okay, now we see…

Next, we’ll want to make sure that the iterator i doesn’t exceed the length of dna, so let’s check for that on the next line.

Inside the for loop, add a line that checks if the iterator, when incremented by 3, exceeds the length of dna.

if i + 3 < len(dna):

#3

Thank you, that was a typo that I didn’t notice, now it works ! Justice rules !


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.