I'm so close the finish line, but I get error on one out of the 21 CS50 tests.
The test that is generating the error is number #2, which is:
python dna.py databases/small.csv sequences/2.txt
And I get the error:
Traceback (most recent call last): File "dna.py", line 53, in <module> max_tuple = max(d.items(), key=lambda k: k) ValueError: max() arg is an empty sequence
Please help me figure out where the bug is.
import csv import sys from collections import defaultdict # Check that two command line arguments are passed with the program (database and sequence to test) if len(sys.argv) != 3: print("Usage: python dna.py data.csv sequence.txt") exit(1) # Store argv inputs into variables csvfile = sys.argv sequencefile = sys.argv #Open CSV file and read into memory csv_file = open (csvfile, "r") csv_reader = csv.DictReader(csv_file) # Read into the specific fields of the CSV file (for later out branching out the STRs) fields = csv_reader.fieldnames # Store the different STRs in variables STRs = fields[1:] # Removes the name column and store it in a new variable # Open sequence file sequence_file = open (sequencefile, "r") sequence_reader = sequence_file.read() sequence_file.seek(0) # Create a dict to store each index in the DNA sequence (key), and it's count for current STRs (value) d = defaultdict(int) # Create a dict to store each STR (key) and the longest consecutive repeat of it in the sequence (value) e = defaultdict(int) for x in STRs: for counter, y in enumerate(sequence_reader): # Read in a chunk of len(x) from sequence file, store it in current_chunk sequence_file.seek(counter) current_chunk = sequence_file.read(len(x)) # While current_chunk == x, increment the counter in the dict with 1, read in next chunk index = counter #Add a index to move forward checking next chunk in the sequence iteration = 1 while current_chunk == x: d[counter] += 1 currentindex = index + (iteration * len(x)) sequence_file.seek(currentindex) current_chunk = sequence_file.read(len(x)) iteration += 1 # Store the highest count for the current STR # Get the highest value from the "d" array and store it in a variable max_value max_tuple = max(d.items(), key=lambda k: k) max_value = max_tuple d.clear() # Add one instance to the "e" array with key = Current STR (x) and value = "high" e[x] = max_value # Compare each person in the database with the numbers in the "e" array, print out match for row in csv_reader: matches = 0 for STR in STRs: if e[STR] != int(row[STR]): pass else: matches = matches + 1 if matches == len(STRs): print(row["name"]) exit(1) # If reached here, no match was found print("No match")