Last active
September 29, 2022 10:27
-
-
Save voidnerd/c475fc438580d0421edd9d3ee75b95d5 to your computer and use it in GitHub Desktop.
CS50 Problem Set 6 - DNA Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from csv import reader, DictReader | |
from sys import argv, exit | |
sequences = {} | |
#validate input | |
if len(argv) < 3: | |
print("Usage:", "python dna.py data.csv sequence.txt") | |
exit(1); | |
# get maximum number of STR sequence | |
def get_max(dna, STR): | |
i = 0 | |
j = len(STR) | |
max = 0; | |
for x in range(len(dna)): | |
if dna[i:j] == STR: | |
temp = 0; | |
while dna[i:j] == STR: | |
temp += 1 | |
i+= len(STR) | |
j+= len(STR) | |
if(temp > max): | |
max = temp | |
else: | |
i+=1 | |
j+=1 | |
return max | |
with open(argv[2], 'r') as dnafile: | |
dna = dnafile.read() | |
# open peopleFile to get sequence keyes | |
with open(argv[1], "r") as peopleFile: | |
peopleReader = reader(peopleFile) | |
for row in peopleReader: | |
header = row | |
header.pop(0) | |
for item in header: | |
sequences[item] = 0; | |
break | |
# give sequence keys value from max function | |
for key in sequences: | |
ans = get_max(dna, key) | |
sequences[key] = ans | |
# print persons if found | |
with open(argv[1], "r") as peopleFile: | |
people = DictReader(peopleFile) | |
for person in people: | |
match = 0 | |
for key in sequences: | |
if int(person[key]) == sequences[key]: | |
match += 1 | |
if match == len(sequences): | |
print(person["name"]) | |
exit(0) | |
print("No match") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment