I make a simple code:
arr = [4, 9, 5, 3, 2, 10]
kpmp = [] # Empty list to store the comparison
for i in arr:
if i>0:
x = 0
j = i
while j>=0:
j-=1
if arr[i] > arr[j]:
x+=1
kpmp.append(x)
print(kpmp)
But, I got this error:
Traceback (most recent call last):
File "python", line 11, in <module>
IndexError: list index out of range
Which is this line if arr[i] > arr[j]:
The expected output I'd like to print is a list [0, 0, 1, 3, 4, 0]
Explanation:
arr = [4, 9, 5, 3, 2, 10]
1. 4 has nothing to compare so the result is 0
2. 9 > 4 the result is 0
3. 4 < 5 < 9 the result is 1
4. 3 < 4 < 5 < 9 the result is 3
5. 2 < 3 < 4 < 5 < 9 the result is 4
6. Well, 10 is the greatest in the list so the result is 0
I got stuck here, kinda basic knowledge with Python, though.
Thanks for your help!