Problem Statement: An array is accepted if and only if it has the following structure:
- First a1 elements equal 1.
- Next a2 elements equal 2.
- Next a3 elements equal 3.
- Next a4 elements equal 4.
- Next a5 elements equal 5.
- Next a6 elements equal 6.
- Next a7 elements equal 7.
Where:
- a(i) can be any non-zero positive integer.
- There are no other elements in array.
Even though the algorithm for this problem seems pretty easy to implement, I am having some difficulty with the code. Here is what I have written.
print("Enter list: ")
arr = [int(x) for x in input().split()]
print(arr)
i = 0
if arr[0] == 1: # to check whether input starts with a 1.
if arr[i] == 1: #If next number is also 1, then
while arr[i] == 1: #while numbers are equal to 1, increment i
i = i + 1
if arr[i] == 2: #If next number is 2, then
while arr[i] == 2: #same procedure as above ... and so on
i = i + 1
if arr[i] == 3:
while arr[i] == 3:
i = i + 1
if arr[i] == 4:
while arr[i] == 4:
i = i + 1
if arr[i] == 5:
while arr[i] == 5:
i = i + 1
if arr[i] == 6:
while arr[i] == 6:
i = i + 1
if arr[i] == 7:
while arr[i] == 7:
i = i + 1
if arr[-1] == 7: #to check if last number is a 7
print("Accepted")
else:
print("not")
else:
print("not")
else:
print("not")
else:
print("not")
else:
print("not")
else:
print("not")
else:
print("not")
else:
print("not")
else:
print("not")
I seem to be getting some kind of indexing error where it says:
while arr[i] == 7:
IndexError: list index out of range
I don't understand why I am encountering this error. As far as I can tell, I am not exceeding the list index.