0

I'm truly at a loss here. Seems like I've fully comprehended everything this Pset has thrown at me, yet somehow when I run this code all of my values (for each individual STR) are 1 less than the correct answer. A hard coded adjustment (like setting count = 1) results in some answers being correct, others missing one matching value. Any tips would be much appreciated.


import sys
import csv
from sys import argv, exit

# check for proper command line arguments
if len(argv) != 3:
    print("error: command line must contain three arguments")
    exit(1)

# create a list of lists, each corresponding to a given suspect and the number of repetitions within their respective STRs
suspects = []
# create a list simply of STRs, to be tested
STR_list = []
# create a dict to test each suspect against
test_dict = {}
test_list = []

matches = 0
index = 0

# iterate through csv file, creating new lists through each iteration
with open (argv[1], newline = '') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        suspects.append(row)

# create a list of STRs, then create an empty dictionary full of those STRs
for i in suspects[0]:
    STR_list.append(i)
for i in STR_list:
    test_dict[i] = 0

# remove 'name'
suspects.pop(0)

# create a new string, DNA, with the dna from TXTfile
with open(argv[2], newline = '') as txtfile:
    dna = txtfile.read()

# iterate through both the STR_list and dna
for i in range(1, len(STR_list)):
    count = 0
    for j in range(len(dna)):
        if dna[j : j + (2* len(STR_list[i]))] == STR_list[i] * 2: # if there is a repeated STR...
            count += 1 # add 1 count (repeat)
    if count >= test_dict[STR_list[i]]: # if this number of STR repetitions is greatest
        test_dict[STR_list[i]] = count # set test_dict
        count = 0 #reset count

# create new test_list, filled with only the values of newly completed test_dict
test_list = list(test_dict.values())
# print(test_dict)

# iterate through the subjects and the test list -- if there is matching number of repetitions, count +1 matches
for i in range(len(suspects)):
    for j in range(len(test_list)):
        if suspects[i][j] == str(test_list[j]):
            matches += 1
        # print full match
        if matches == len(test_list) - 1: # or matches == len(test_list) - 2:
            print(suspects[i][0])
            exit()
    matches = 0

print("No match")

You must log in to answer this question.

Browse other questions tagged .