User973
User973

Reputation: 27

Return index with enumerate

I am basically trying to return the starting index of a DNA string where A and G form the majority over the next five letters.

See code

def a_g_majority_positions(dna):

    ### BEGIN SOLUTION
    list=[]
    for index, item in enumerate(dna):
            count=0
            x=0
            index1=index
            while count < 5:
                letter=dna[index1]
                index1+=1
                count+=1
                if letter == 'A':
                    x+=1
                elif letter == 'G':
                    x+=1
            if x>=3:
                list.append(index)
    return list




    ### END SOLUTION
a_g_majority_positions("AACCGGTTAACCGGTT")

I always get a string index out of range error. The correct answer for the dna at the end is [0,1,4,5,8,9]

Upvotes: 2

Views: 147

Answers (3)

Prune
Prune

Reputation: 77857

Use the count method to count the letters of interest. Start the run of five up until you don't have enough positions left:

def a_g_majority_positions(dna):

    lst = []
    for start5 in range(len(dna)-4):
        five = dna[start5:start5+5]
        if five.count('A') + five.count('G') >= 3:
            lst.append(start5)
    return lst

Or, for the one-statement version, checking each character for being in "AG":

lst = [start5 for start5 in range(len(dna)-4)
       if sum(char in "AG"
              for char in dna[start5:start5+5]) >= 3]

Output in either case is

[0, 1, 4, 5, 8, 9]

Upvotes: 1

caburke
caburke

Reputation: 268

You need to break early from the function when index is greater than len(dna) - 5. Otherwise you try to access dna[len(dna)], which is out of range.

def a_g_majority_positions(dna):

### BEGIN SOLUTION
list=[]
for index, item in enumerate(dna):
        count=0
        x=0
        index1=index
        if index > len(dna) - 5:
            break
        while count < 5:
            letter=dna[index1]
            index1+=1
            count+=1
            if letter == 'A':
                x+=1
            elif letter == 'G':
                x+=1
        if x>=3:
            list.append(index)
return list




### END SOLUTION
a_g_majority_positions("AACCGGTTAACCGGTT")

# Result [0, 1, 4, 5, 8, 9]

Upvotes: 0

user3657941
user3657941

Reputation:

You need to break out of the for loop when there are less than 5 letters left:

def a_g_majority_positions(dna):
    result = list()
    for index, item in enumerate(dna):
        if index + 5 >= len(dna):
            break
        count = 0
        x = 0
        while count < 5:
            letter = dna[index + count]
            count += 1
            if letter == 'A':
                x += 1
            elif letter == 'G':
                x += 1
        if x >= 3:
            result.append(index)
    return result

print(a_g_majority_positions("AACCGGTTAACCGGTT"))

Output

[0, 1, 4, 5, 8, 9]

Note

Don't use list as a variable name. It's built-in class and you will introduce hard to find errors by using it as a variable name.

Upvotes: 0

Related Questions