The number of elements that are greater than the previous one. A sequence consists of integer numbers and ends with the number 0. Determine how many elements in this sequence are greater than their immediate predecessor.
|
previous = int(input()) count = 0 while True: current = int(input()) if current == 0: break if current > previous: count += 1 previous = current print(count) |
Explanation of the Code
- Initialize Variables:
previous = int(input())
count = 0
- Initialize
previous
to the first input integer to keep track of the previous element in the sequence.
- Initialize
count
to 0 to keep track of the number of elements greater than their predecessor.
- Reading Input and Counting Elements Greater Than Their Predecessor:
while True:
current = int(input())
if current == 0:
break
if current > previous:
count += 1
previous = current
- Use a
while True
loop to continuously read input.
- Convert the input to an integer using
int()
.
- If the integer is 0, break the loop.
- If the integer is greater than
previous
, increment count
by 1.
- Update
previous
to the current integer.
- Printing the Count of Elements Greater Than Their Predecessor:
- Print the value of
count
after exiting the loop.