A simple pedometer can be modeled by a pendulum with two switches on opposite sides—one at x=0 and one at x=l. When the pendulum contacts the far switch, the ambulator can be assumed to have taken half a step. When it contacts the near switch, the step is completed.
Given a list of integers representing positions of the pendulum, determine the number of full steps recorded on the pedometer.
Input
An integer l>0, the length of the track.
A list of integers representing the positions of the pedometer's pendulum at each time.
Output
The number of full steps measured. A step is taken when the pendulum contacts the far switch (x>=l) and then the near switch (x<=0).
Test cases
8, [8, 3, 0, 1, 0, 2, 2, 9, 4, 7]
1
The pendulum immediately makes contact with the far switch at x=8 at t=0. Then it touches the near switch at t=2 and t=4, completing one step. After that, it touches the far switch again at x=9 at t=8, but it never touches the near switch again.
1, [1, 5, -1, -4, -1, 1, -2, 8, 0, -4]
3
15, [10, -7, -13, 19, 0, 22, 8, 9, -6, 21, -14, 12, -5, -12, 5, -3, 5, -15, 0, 2, 11, -11, 12, 5, 16, 14, 27, -5, 13, 0, -7, -2, 11, -8, 27, 15, -10, -10, 4, 21, 29, 21, 2, 5, -7, 15, -7, -14, 13, 27]
7
7, [5, 4, 0]
0
7, [5, 8, 6, 1, 2]
0
7, [5, 4, 0]
? Is that 0 or 1? That is - do you assume that a swing is always "full-length"? Or7, [5, 8, 6, 1, 2]
? Is that 0 or 1? \$\endgroup\$8, [0 8 0 8]
should give1
, not2
. Am I correct? \$\endgroup\$