jessibird jessibird - 10 days ago 4
Python Question

What is the counter doing inside the while loop?

count = 0
i = 0
while (i < n-1):
i += (c[i + 2]==0) +1
count += 1


(from hackerrank.com)

In the above code, c is a list:

[0, 0, 1, 0, 0, 1, 0, 0]


What is the fourth line of the code doing?

Answer

It takes the i+2 element of c. If that value is zero, it increases i by 2. If that value is anything else, it increases i by 1.

Comments