0

this is my code. whenever i run this code it always returns no match. i cant seem to get the error. pls help me.


import csv
import sys

def main():

    # TODO: Check for command-line usage
    if len(sys.argv) != 3:
        print("command-line usage")
    else:
        # TODO: Read database file into a variable
        dna_database = []
        file = open(sys.argv[1], "r")
        reader = csv.DictReader(file)
        for row in reader:
            dna_database.append(row)

        # TODO: Read DNA sequence file into a variable
        filer = open(sys.argv[2] , "r")
        read = filer.read()

        # TODO: Find longest match of each STR in DNA sequence
        strs = list(dna_database[0].keys())[1:]
        longest_matches = {}
        for each_str in range(len(strs)):
            longest_matches[strs[each_str]] = longest_match(read,strs[each_str])

        # TODO: Check database for matching profiles
        for each_profile in dna_database:
            no_strs = 0
            for str in strs:
                if longest_matches[str] == int(each_profile[str]):
                    no_strs += 1

                if no_strs == len(strs):
                     print(f"{each_profile['name']}")
                     sys.exit(0)

        print("no match")
        filer.close()
        file.close()


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()
5
  • please fix your indentation it makes no sense and is inconsistent
    – UpAndAdam
    Commented Mar 18 at 15:13
  • If your code matches this post, it won't run due to indentation errors. I added indents to main() and longest_match(). (I also formatted your post so main() is included in the formatted code block.) Once that id corrected, it gets the right answer for several tests I spot checked.
    – kcw78
    Commented Mar 21 at 12:40
  • bad edit @kcw78 you missed indentation of a number of lines which is why i didnt indent it myself it wasnt clear what the intent was.. but you left some lines out of main that are clearly part of main... updating
    – UpAndAdam
    Commented Mar 22 at 14:07
  • OP if you can follow up and let us know and or provide an answer that would be good
    – UpAndAdam
    Commented Apr 5 at 15:26
  • I’m voting to close this question because resolved by typo in indentation
    – UpAndAdam
    Commented Apr 5 at 15:26

0

You must log in to answer this question.

Browse other questions tagged .