0

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[1])
ValueError: max() arg is an empty sequence

Please help me figure out where the bug is.

My code:

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[1]
sequencefile = sys.argv[2]

#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[1])
    max_value = max_tuple[1]
    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")
0

This error is just telling you that the dict d is empty (i.e. no matches were found). So you need to sanity check this. Replace:

max_tuple = max(d.items(), key=lambda k: k[1])
max_value = max_tuple[1]

with something like:

if d:
    max_tuple = max(d.items(), key=lambda k: k[1])
    max_value = max_tuple[1]
else:
    max_value = 0

That being said, you're not using the key from the tuple so you could simplify this whole block a lot with d.values() and a ternary operator:

max_value = max(d.values()) if d else 0
1
  • Awesome, thank you for you help! :)
    – Alexander
    Dec 5 '20 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .