Greater than previous. Given a list of numbers, find and print all the elements that are greater than the previous element in the list.
|
numbers = list(map(int, input().split())) for i in range(1, len(numbers)): if numbers[i] > numbers[i - 1]: print(numbers[i]) |
Explanation of the Code
- Reading Input:
numbers = list(map(int, input().split()))
- The
input()
function reads a line of text from the user.
- The
split()
method splits this line into a list of strings based on spaces.
map(int, ...)
converts each string in the list to an integer.
list(...)
converts the map object to a list and stores it in the variable numbers
.
- Iterating Through the List and Printing Elements Greater Than Previous:
for i in range(1, len(numbers)):
if numbers[i] > numbers[i - 1]:
print(numbers[i])
- Use a
for
loop to iterate through the list numbers
starting from index 1 to the end of the list.
- In each iteration, check if the current element (
numbers[i]
) is greater than the previous element (numbers[i - 1]
).
- If the current element is greater, print it.