Traceback (most recent call last): IndexError: list index out of range
from sys import argv, exit
import csv
import sys
def main ():
if len(sys.argv) < 3:
print("Usage: python dna.py data.csv sequence.txt")
exit(1)
# open csv_file contain the database
with open(sys.argv[1]) as csvfile:
r = csv.reader(csvfile)
# open txt_file contain the DNA sequence
with open(sys.argv[2]) as txtfile:
x = txtfile.read()
y = [v for v in [max_substring(x, seq) for seq in next(r)[1:]] if v >= 0]
print_match(r, y)
# count the maximum substring
def max_substring(x, sub_str):
if not x and not sub_str:
return -1
sub_repeat = len(x)*[0]
for s in range(len(x) - len(sub_str), -1, -1):
a = s + len(sub_str)
if x[s: a] == sub_str:
b = len(x) - 1
c = 1 + sub_repeat[a - 1]
sub_repeat[s] = 1 if a > b else c
return max(sub_repeat)
# Make some assertions
assert max_substring('abcabbcab', 'ab') == 1
assert max_substring('', '') == -1
assert max_substring('abcabbcab', 'abcabbcab') == 1
# print the result
def print_match(r,y):
for line in r:
if [int(val) for val in line[1:]] == y:
print(line[0])
return
print("No match")
# run the functions & class
if __name__ == "__main__" :
main()
Traceback (most recent call last): IndexError: list index out of range
[new output error [1]: https://i.stack.imgur.com/lKKD8.png