Python Forum
index error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: index error (/thread-31342.html)



index error - surim - Dec-05-2020

I don't know why these codes makes error message.
Please help me


def dynamic_sort(C):
    C.sort(key = lambda x: x[1])  
    n = len(C)                   
    cache = []               
   
    for y in range(0, n):         
        cache.append(C[y][2])    
    
    return cache

def dynamic_job(C):
    cache = dynamic_sort(C)    
    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]:         #this line makes the error message(IndexError: list index out of range)
                if cache[k] + C[i][2] > cache[i]:
                    cache[i] = cache[k] + C[i][2] 

    max = 0                      
    for i in range (0, n+1):     
        if max < cache[i]:        
            max = cache[i]       
    return max                    

    



C = [[2,5,6], [4,6,5], [6,7,4], [1,3,5], [5,8,11], [7,9,2]] 

print(dynamic_job(C))



RE: index error - buran - Dec-05-2020

Please, post the entire traceback that you get. We need to see the whole thing.
Take a time to read What to include in a post


RE: index error - surim - Dec-05-2020

(Dec-05-2020, 09:51 AM)surim Wrote: I don't know why these codes makes error message.
Please help me


def dynamic_sort(C):
    C.sort(key = lambda x: x[1])  
    n = len(C)                   
    cache = []               
   
    for y in range(0, n):         
        cache.append(C[y][2])    
    
    return cache

def dynamic_job(C):
    cache = dynamic_sort(C)    
    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]:         #this line makes the error message(IndexError: list index out of range)
                if cache[k] + C[i][2] > cache[i]:
                    cache[i] = cache[k] + C[i][2] 

    max = 0                      
    for i in range (0, n+1):     
        if max < cache[i]:        
            max = cache[i]       
    return max                    

    



C = [[2,5,6], [4,6,5], [6,7,4], [1,3,5], [5,8,11], [7,9,2]] 

print(dynamic_job(C))



Error:
Traceback (most recent call last): File "F:/알개/dynamin_jobschedule.py", line 32, in <module> print(dynamic_job(C)) File "F:/알개/dynamin_jobschedule.py", line 16, in dynamic_job if C[k][1] <= C[i][0]: IndexError: list index out of range



RE: index error - surim - Dec-05-2020

thank you for your reply and I added whole the error messages.


RE: index error - deanhystad - Dec-05-2020

    n = len(C)                   
    for i in range(1, n+1):       
        for k in range(0, i):     
            if C[k][1] <= C[i][0]: 
n is the number of things in C. In Python the first index is 0 and the last index is len-1. If n is 3 there is a C[0], C[1] and C[2]. If you tried to get C[3] you will get a "list index out of range" error because there is no C[3].

When you do the loop "for i in range(1, n+1)" you try to look at C[n]. Remember that the possible index numbers range from 0 to len-1, so C[len(C)] is out of range.