Back
I am trying to execute the following python code:
def construct(s, k, a): index = 0 # Finding the index which is not -1 for i in range(s): if (a[i]!=-1): index = i break # Calculating the values of the indexes index-1 to 0 for i in range(index-1, -1, -1): if (a[i]==-1): a[i]=(a[i + 1]-1 + k)% k # Calculating the values of the indexes index + 1 to n for i in range(index + 1, s): if(a[i]==-1): a[i]=(a[i-1]+1)% k print(a)# Driver codes, k = 6, 7a = [1, 2, 3, 4, 5]construct(s, k, a)
def construct(s, k, a):
index = 0
# Finding the index which is not -1
for i in range(s):
if (a[i]!=-1):
index = i
break
# Calculating the values of the indexes index-1 to 0
for i in range(index-1, -1, -1):
if (a[i]==-1):
a[i]=(a[i + 1]-1 + k)% k
# Calculating the values of the indexes index + 1 to n
for i in range(index + 1, s):
if(a[i]==-1):
a[i]=(a[i-1]+1)% k
print(a)
# Driver code
s, k = 6, 7
a = [1, 2, 3, 4, 5]
construct(s, k, a)
I get the following error:
IndexError: list index out of range
IndexError occurs when the list index is out of range, you need to make sure that your code is not accessing list index that is not valid.
If you want to get a deeper understanding of python you can watch this youtube video:
31k questions
32.8k answers
501 comments
693 users