0
import csv
from sys import argv

if len(argv) != 3:
    print("Usage: python dna.py arg1.csv arg2.txt")
    exit(1)
database = open(argv[1])
sequence = open(argv[2])
s = sequence.read()
csv = csv.reader(database)
headers = next(csv, None)
hlength = len(headers)
column = {}
dic = {}
for h in headers:
    column[h] = []
clength = len(column)
temp = 0
count = 0
for i in range(1, hlength):
    slength = len(s) - len(headers[i]) + 1
    dic[headers[i]] = 0
    highest = 0
    n = len(headers[i]) - 1
    for j in range(slength):
        if s[j : j + n + 1] == headers[i]:
            highest += 1
            j += n
        if highest > dic[headers[i]]:
            dic[headers[i]] = highest
        if s[j : j + n + 1] != headers[i]:
            temp = 1
        if highest < dic[headers[i]] and temp == 1:
            highest = 0
            temp = 0
for row in csv:
    count = 0
    for i in range(1, hlength):
        print(row[i], dic[headers[i]])
        if int(row[i]) == dic[headers[i]]:
            count += 1
    if count == hlength - 1:
        print(row[0])
        exit()
    else:
        count = 0

print("No match")
0

j += n won't work the way you want it to, at least not in a Python for loop, as there's no j++ (or j += 1) at the end, but the loop picks the next number from the iterator range(slength) returned.

You could use a while loop instead, and do all the incrementing manually. You don't even need that many ifs.

3
  • Thanks a lot! But I didnt understand the reasoning behind why j += n wouldnt work Feb 19 '20 at 18:28
  • There's no j += 1. The loop will set j to all those numbers, but does not care how you change it, because the range iterator has its own counter.
    – Blauelf
    Feb 19 '20 at 18:40
  • Oh ok thanks a lot! Feb 20 '20 at 4:12

You must log in to answer this question.

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