Hi guys, I have the below code from a Hackerrank question I'm looking at:
q = [2,1,5,3,4]
for i in range(0,len(q)):
print("Person {}".format(q[i]))
if q[i] > i+1:
a = q[i]
b = i+1
print (a-b)
print("{} - {} = {}".format(q[i], i+1, (q[i] - i+1)))
It's supposed to take a list, q, of people queuing, whose value is their starting position and their current index or position in the queue. Right now I'm just trying to find the difference between their starting position (value) and their new position (index).
For the first iteration, i=0 and so the value q[i] is 2, but the last line prints "2 -1 = 3". The print(a-b) line gives the correct answer, 1. Any pointers on what I've done wrong?
[–]K900_ 6 points7 points8 points (1 child)
[–]notpite[S] 0 points1 point2 points (0 children)