Skip to content

Instantly share code, notes, and snippets.

@alpden550
Created July 10, 2020 10:14
Show Gist options
  • Save alpden550/6fc858cc6f1b8cd206e20814667647ba to your computer and use it in GitHub Desktop.
Save alpden550/6fc858cc6f1b8cd206e20814667647ba to your computer and use it in GitHub Desktop.
# https://cs50.harvard.edu/x/2020/psets/6/dna/
import csv
import re
import sys
from pathlib import Path
def get_dna(filename):
return Path(filename).read_text()
def get_users_from_csv(filepath):
users = []
with open(filepath) as csv_file:
users_data = csv.DictReader(csv_file)
for user in users_data:
users.append(dict(user))
return users
def count_substring(substring, sequence):
counters = []
pattern = re.compile(r'({})+'.format(substring))
matches = pattern.finditer(sequence)
for match in matches:
count = match.end() - match.start()
counters.append(round(count / len(substring)))
return max(counters) if counters else []
def main(data_csv, sequence):
users = get_users_from_csv(data_csv)
dna = get_dna(sequence)
for user in users:
match = 0
for key, value in user.items():
if not key == 'name':
counted_str = count_substring(key, dna)
if not counted_str == int(value):
continue
match += 1
if match == len(user) - 1:
print(user['name'])
return
print('No match')
if __name__ == "__main__":
if not len(sys.argv) == 3:
print('Usage: python dna.py data.csv sequence.txt')
exit()
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment