how to fix "string index out of range " in python??

0 votes
asked Oct 26, 2019 by Hussien Ali (120 points)
this is the code , how i can fix it?

file_first="AACCGTTTGATTG"
file_second="AACGTTTGAGCTG"
i_first=len(file_first)
i_second=len(file_second)
counter=0
pos_count=""
jj=-1
ii=-1
for i in range(0,i_first):
    for j in range(0,i_second):
        if (file_first[i]==file_second[j]):
            i_search=i_second-j
            counter1=0
            ii=i
            jj=j
            pos_count1=""
            z=0
            for z in range(i_search,i_second):
                if(file_first[i+z]==file_second[j+z]):
                    counter1=counter1+1
                    pos_count1=pos_count1+file_first[i+z]
                else:
                    break
            if counter1>counter :
                counter=counter1
                ii=i
                jj=j
                pos_count=pos_count1

print("the counter is ",counter," \t postion is :",pos_count," \n postion in first file is :",ii," \n postion in second file is :",jj)

1 Answer

0 votes
answered Oct 27, 2019 by gameforcer (2,950 points)

Your "z" variable is wrong. You should make so it starts from 1 (to compare next letter in the strings; you already compared letters in i and j, so now you should compare i+1 and j+1) and ends on last letter of shorter string.

i_first - i

and

j_first - j 

should measure how many characters left to compare in each string so you just have to pick the minimum from them as follows:

for z in range(1, min(i_first - i, i_second - j)):

Also from the way you declared it your counter1 variable should start from 1, not 0 becuse you already found one letter in common. Similarly your pos_count1 should start with the found letter:

counter1 = 1
pos_count1=file_first[i]

ii and jj variables don't register first nor last position of found substring, they're just wrong... If you wanted them to be first position of a substring in your strings instead then you can use .index() method, for example:

str = "not a substring"

pos = str.index("substring")

print(pos)

Your print function at the end is way too long for one line so it's better to either break it with enters or split into few prints.

2 extra tips:

range(0, 5)

is the same as

range(5)

and this:

var = var + 1

is the same as

var += 1

Here's corrected code:

file_first="AACCGTTTGATTG"
file_second="AACGTTTGAGCTG"
i_first=len(file_first)
i_second=len(file_second)
counter=0
pos_count=""

for i in range(i_first):
    for j in range(i_second):
        if (file_first[i]==file_second[j]):
            
            counter1 = 1
            pos_count1=file_first[i]
            for z in range(1, min(i_first - i, i_second - j)):
                if file_first[i+z] == file_second[j+z]:
                    counter1 += 1
                    pos_count1 += file_first[i+z]
                else:
                    break
            if counter1>counter :
                counter=counter1
                pos_count=pos_count1

print(" the counter is ",counter)
print("postion is :",pos_count)
print("postion in first file is :", file_first.index(pos_count))
print("postion in second file is :", file_second.index(pos_count))

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...