n = int(raw_input())
s = raw_input().strip().split(' ')
ar = index = [0]*n; done = [False]*n
for a in range(n): ar[a] = int(s[a])
k=0
for a in range(5):
m = 1000000001
for i in range(n):
if done[i]: continue
if ar[i] < m: m = ar[i]
if m == 1000000001:
sys.stdout.write('-1\n')
break
#print m
for i in range(n):
if ar[i] == m:
index[k] = i
k+=1
done[i]=True
print index
The algorithm is quite simple. ar is an array of n (>= 5) integers. I intend to store the 0 based positions of the first 5 smallest integers of the array in index.
After taking n as input, n space separated integers are inputted on the next line.
The problem is very weird – for the following input:
7
6 17 5 3 13 8 10
when I uncomment #print m in the code, it prints 3 3 0 3 0 (expected output is 3 5 6 8 10); also, an IndexError occurs (that is just a symptom of the real problem).
Variable done is working as expected (it contains a list of boolean values, where, if done[i] is True, then ar[i] should not be taken into consideration while looking for min(ar)
I did a lot of debugging by printing values of variables at different locations, but couldn’t reason out anything.
I’m not really following your code, but this:
seems quite strange to do in python, try to replace it with:
And see if it works.
This example, may help to see why that seems strange:
You may be interested in taking a loook at Other languages have “variables”, Python has “names”.
Side note:
Try to make your code more readable:
;Also (if I understood what you’re trying to do), your code could be rewritten like: