0

I am trying to learn python and did some research here but was unable to put the bits of info together... My instructor has handed me an additional task for which after searching online parts of code I found it difficult to finish...actually I am learning python for the last two weeks.

I have a file named

dna.txt

which has below data stored

ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGCCTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGGAAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCCCTGCAGGAACTTCTTCTGGAAGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAGTTTAATTACAGACCTGAA

I have then details of three males as in eye color, hair color, etc...and I am supposed to have a program running that would check the data stored in the txt file read it and them compare it with the info on the males and print out who is the match...

what I have been able to put together is:

  evidence = open("dna.txt", "r").read()

john= {
    "gender": "TGCAGGAACTTC",
    "race": "AAAACCTCA",
    "color_hair": "TTAGCTATCGC",
    "color_eyes": "AAGTAGTGAC",
    "type_face": "ACCACAA"
}

matthew = {
    "gender": "TGCAGGAACTTC",
    "race": "AAAACCTCA",
    "color_hair": "CCAGCAATCGC",
    "color_eyes": "TTGTGGTGGC",
    "type_face": "AGGCCTCA"
}

edward = {
    "gender": "TGCAGGAACTTC",
    "race": "AAAACCTCA",
    "color_hair": "GCCAGTGCCG",
    "color_eyes": "GGGAGGTGGC",
    "type_face": "GCCACGG"
}

and now I am stuck what next, on my previous "easier" assignment without using dicts I continued with if, and, elif, etc...but not sure if in this case would use the same..

I did some tries with

if evidence.find('John') > -1 :
    print "john is the thief!"

elif evidence.find('Matthew') > -1 :
    print "matthew is the thief!"

elif evidence.find('Edward') > -1 :
    print "edward is the thief!"

but not sure if this the right move, although the code is running but does not actually print the name of the guy. again I have no previous experience with python...I might completely miss the solution, especially since I got instructions to use "arrays & "dictionaries" - if I got it right

thanks folks for helping me out..

Davor
  • 11
  • 2

1 Answers1

0

When calling evidence.find('Edward'), you are passing a string, so it is searching for that string in evidence, which it isn't.

You want to search for each element of each dictionary in the string. This can be done with python's excellent iteritems():

matching_fields = 0
for key, value in edward:
  if value in example:
    matching_fields += 1

This will count how many of the values exist anywhere within the example string. You can repeat this process for each suspect, and the one with the most matches should be your answer!

DISCLAIMER: I don't know how DNA works, if gender and color_hair etc. need to be in a certain place, you can use slicing:

matching_fields = 0
for key, value in edward:
  if value == example[expected_location:length]:
    matching_fields += 1

EDIT: Something like this should be close:

evidence = open("dna.txt", "r").read()

john= {
    "gender": "TGCAGGAACTTC",
    "race": "AAAACCTCA",
    "color_hair": "TTAGCTATCGC",
    "color_eyes": "AAGTAGTGAC",
    "type_face": "ACCACAA"
}

matthew = {
    "gender": "TGCAGGAACTTC",
    "race": "AAAACCTCA",
    "color_hair": "CCAGCAATCGC",
    "color_eyes": "TTGTGGTGGC",
    "type_face": "AGGCCTCA"
}

edward = {
    "gender": "TGCAGGAACTTC",
    "race": "AAAACCTCA",
    "color_hair": "GCCAGTGCCG",
    "color_eyes": "GGGAGGTGGC",
    "type_face": "GCCACGG"
}

suspects = [john, matthew, edward]
best_guess = None
highest_similarity = 0

for suspect in suspects: # iterate through potential suspects
  matches = 0
  for key, value in suspect.iteritems(): # iterate through current suspects attributes
    if value in evidence:
      matches += 1
  if matches > highest_similarity:
    highest_similarity = matches
    best_guess = suspect

for key, val in list(locals().iteritems()):
     if val is best_guess:  # NOTE: 'is' for checking identity
         suspect_name = key
         print suspect_name, " is the culprit!"

After the first for loop, best_guess is the dictionary that most closely matches example. See this answer for how to get the name of the suspect dictionary.

Community
  • 1
  • 1
Will
  • 4,299
  • 5
  • 32
  • 50
  • ok thanks for the info, but I need to go thru all three dicts (john, matthew and edward) and check the elements and compare with the data in the txt file? sorry I am lost – Davor Apr 27 '16 at 20:16
  • That sounds right, but again, I don't know how DNA works. Is it correct to say that `if all sequences associated with a suspect are found anywhere in the example, then he is our best suspect`? Or do you have to check specific locations for each of the attributes? – Will Apr 27 '16 at 20:19
  • I believe since the data in the txt file is longer, that it should only search for the suspect details, and if all 5 are a match (can be found) then it should print out the persons name, if I do a simple search for johns `"type_face": "ACCACAA"`, I can see that ACCACAA is not in the file, its not found – Davor Apr 27 '16 at 20:32
  • hmm if I include your edited code and change the `value in example` to `value in evidence` I can see below exit `best_guess is the culprit!` `edward is the culprit!` `suspect is the culprit!` what I am missing? Process finished with exit code 0 – Davor Apr 27 '16 at 21:37