1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Comparisons issue in DNA (cs50 PS6) [closed]

Discussão em 'Python' iniciado por Stack, Novembro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on CS50’s DNA problem, where you read a CSV database of people and their STR counts, compute the longest consecutive runs of each STR in a given DNA sequence, and then compare those counts to each row in the database to find a match.

    My code calculates the results dictionary correctly (the longest STR runs for the input sequence). But when I compare results to each row in the CSV, I'm getting confusing output and I can't tell why.

    Here’s what’s happening:

    Before comparing, I print the values from both dictionaries for each subsequence.

    But the program prints the same row[...] value multiple times (for example, Alice’s row prints 3 times, Bob’s 2 times, Charlie’s once), even when I change the input file or DNA sequence.

    The numbers printed under “Row:” and “Results:” are supposed to represent the STR counts from the CSV row and from my computed results, but I don't understand why the same values appear multiple times or why the output repeats in this specific pattern.

    The comparison always fails, even when the values appear to match.

    The unexpected behavior is:

    The same row values print multiple times.

    The number of repeats seems consistent (e.g., Alice prints 3 times no matter the file).

    Sometimes the Row: and Results: values match, but the code still prints “No match”.

    I can't tell what the duplicated prints represent or why the comparison behaves this way.

    What could cause the Row: values to repeat like this, and what exactly do these printed numbers represent during the loop?

    Her's my code:

    from csv import DictReader
    import sys


    def main():

    if len(sys.argv) != 3:
    print("Error: Incorrect number of arguments")
    return 1

    rows = []
    filename = sys.argv[1]
    with open(filename) as file:
    file_reader = DictReader(file)
    for row in file_reader:
    rows.append(row)

    dna_sequence = []
    f = sys.argv[2]
    with open(f) as file:
    dna_sequence = file.read()

    subsequences = list(rows[0].keys())[1:] # creates a list of keys

    results = {}
    for subsequence in subsequences:
    results[subsequence] = longest_match(dna_sequence, subsequence)

    for row in rows:
    match = True

    for subsequence in subsequences:
    if int(row[subsequence]) != results[subsequence]:
    match = False
    print("No match")
    break

    if match:
    print(row["Name"])
    break

    def longest_match(sequence, subsequence):

    longest_run = 0
    subsequence_length = len(subsequence)
    sequence_length = len(sequence)

    for i in range(sequence_length):

    count = 0

    while True:
    start = i + count * subsequence_length
    end = start + subsequence_length

    if sequence[start:end] == subsequence:
    count += 1

    else:
    break
    longest_run = max(longest_run, count)

    return longest_run

    main()

    Continue reading...

Compartilhe esta Página