Skip to content

Instantly share code, notes, and snippets.

@sayan01
Created December 20, 2020 10:09
Show Gist options
  • Save sayan01/af701ccacc115598cd6aaba71c16fef7 to your computer and use it in GitHub Desktop.
Save sayan01/af701ccacc115598cd6aaba71c16fef7 to your computer and use it in GitHub Desktop.
dna.py for CS50
# first arg = database csv
# second arg = sequence
import csv
from sys import argv, exit
# if user does not adhere to usage, quit
if len(argv) != 3:
print("Usage: python dna.py data.csv sequence.txt")
exit(1)
# open the passed files and close them after work done
with open(argv[1]) as dataf, open(argv[2]) as seqf:
datao = csv.reader(dataf) # open database as csv object
seq = seqf.read() # read entire sequence into one string
data = [] # two dimensional array to store database
for row in datao: # add all the rows of database to 2D array
data.append(row)
header = data[0:1][0] # copy the first row (column headings) to 'header'
data = data[1:] # remove header from data itself
for row in data: # iterate over each row of data
this = True # boolean flag where true = this person is match
for i in range(1, len(row)): # for all the STR mentioned in database, check current person with seq
this = this and header[i]*int(row[i]) in seq and header[i]*(int(row[i])+1) not in seq
if this: # if all the STR matches for this person, print and exit
print(row[0])
exit(0)
print("No match") # if no match then we come here, so print no match
@TacitBrawler
Copy link

DAMN!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment