DNA
문제 링크
https://cs50.harvard.edu/x/2024/psets/6/dna/
실제 코드 구현
import csv
import sys
def main():
# TODO: Check for command-line usage
if len(sys.argv) != 3:
return
# TODO: Read database file into a variable
with open(sys.argv[1]) as file:
reader = csv.DictReader(file)
database = list(reader)
# TODO: Read DNA sequence file into a variable
with open(sys.argv[2]) as file:
DNAs = file.read()
# TODO: Find longest match of each STR in DNA sequence
matches = dict()
for i in database[0]:
if i != 'name':
matches[i] = longest_match(DNAs, i)
# TODO: Check database for matching profiles
for i in range(len(database)):
check = 0
for k, v in database[i].items():
if k != 'name' and matches[k] == int(v):
check += 1
if check == len(matches):
print(database[i]['name'])
return
print("No match")
return
def longest_match(sequence, subsequence):
"""Returns length of longest run of subsequence in sequence."""
# Initialize variables
longest_run = 0
subsequence_length = len(subsequence)
sequence_length = len(sequence)
# Check each character in sequence for most consecutive runs of subsequence
for i in range(sequence_length):
# Initialize count of consecutive runs
count = 0
# Check for a subsequence match in a "substring" (a subset of characters) within sequence
# If a match, move substring to next potential match in sequence
# Continue moving substring and checking for matches until out of consecutive matches
while True:
# Adjust substring start and end
start = i + count * subsequence_length
end = start + subsequence_length
# If there is a match in the substring
if sequence[start:end] == subsequence:
count += 1
# If there is no match in the substring
else:
break
# Update most consecutive matches found
longest_run = max(longest_run, count)
# After checking for runs at each character in seqeuence, return longest run found
return longest_run
main()
후기
확실히 파이썬으로 코드를 짜니 쉽고 간단하게 짤 수 있었다.
list 로도 구현가능하고 dict 로도 구현 가능했는데
dict 가 직관적으로 더 적합할 것 같은 생각이 들어 dict를 선택했다.
'입문 > CS50X' 카테고리의 다른 글
[CS50X] 2024 Week6 Python (0) | 2024.03.12 |
---|---|
[CS50X] 2024 Week5 Data Structures Problem Set 5 (Inheritance, Speller) (0) | 2024.03.10 |
[CS50X] 2024 Week5 Data Structures (1) | 2024.03.07 |
[CS50X] Week3 번외편(Sort 구현 - Selection, Bubble, Merge, 비트연산자 ) (0) | 2024.03.05 |
[CS50X] 2024 Week4 Memory Problem Set 4 (Volume, Filter, Recover) (0) | 2024.02.29 |