0

I've been stuck on this question for a week. After countless hours of debugs, I decided to post it here. Please refer to images and code snippets below and kindly explain to me what might be the problem here.

from sys import argv
import csv
from csv import reader


def get_max_str_repeats(sequence, dna_str):
    count = 0
    max_reps = 0
    i = 0

    while i + len(dna_str) < len(sequence):
        if sequence[i:i+(len(dna_str))] == dna_str:
            #print(sequence[i:i+(len(dna_str))])
            i += len(dna_str)
            count += 1
            if i + len(dna_str) > len(sequence):
                max_reps = count

        else:
            if max_reps < count:
                max_reps = count
                count = 0
            i += 1
    #print(f"\t STR: {dna_str} | Max Rep: {max_reps}")
    return max_reps  # Lavender sequence 5 GAAA returns 48 but should be 47 TCTG should return 41 returns 43


if len(argv) != 3:
    print("Program usage: dna.py csvfile.csv textfile.txt")
    exit(1)

list_of_names = []


with open(argv[1], "r") as file:  # for assigning read csv values to dictionary
    readerr = csv.DictReader(file)
    for row in readerr:
        list_of_names.append(row)

with open(argv[1], 'r') as header_of_file:  # to read nucleotides from header
    csv_reader = reader(header_of_file)
    header = next(csv_reader)

dna_strs = []

for columns in header[1:]:  # column names (dna strs) except "name" column
    dna_strs.append(columns)


with open(argv[2], "r") as f:

    sequence = str(f.read())

    for person in list_of_names:
        nucleotide_match_count = 0
        for dna_str in dna_strs:
            #print(person, end="")
            if get_max_str_repeats(sequence, dna_str) == int(person[dna_str]):
                nucleotide_match_count += 1
                if nucleotide_match_count == len(dna_strs):
                    print(person["name"])
                    exit(0)

    print("No match")

Lavender results

Luna results

Overall results

2
  • Please provide more context. The full text of your program would be helpful.
    – juls
    Apr 11, 2021 at 0:00
  • I added the full text of my program Apr 11, 2021 at 10:12

1 Answer 1

0

I found the problem. It turned out that I forgot the reset the counter in else condition. Also, my while loop should be less than or equal instead of just less than and I don't need if condition to check whether i + len(dna_str) > len(sequence) since I am already checking it in my while statement. Correct code of function get_max_str_repeats() is below.

def get_max_str_repeats(sequence, dna_str):
count = 0
max_reps = 0
i = 0

while i + len(dna_str) <= len(sequence):
    if sequence[i:i+(len(dna_str))] == dna_str:
        #print(sequence[i:i+(len(dna_str))])
        i += len(dna_str)
        count += 1

    else:
        if max_reps < count:
            max_reps = count
            count = 0
        i += 1
        count = 0

#print(f"\t STR: {dna_str} | Max Rep: {max_reps}")
return max_reps
2
  • Nice! Can you accept your own answer, so this no longer appears as unanswered?
    – juls
    Apr 11, 2021 at 14:07
  • Yep, after 15 hours of time limit I will gladly do that :) Apr 11, 2021 at 16:14

You must log in to answer this question.

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