This task can be achieved by iteration, updating the maximum, and printing out the maximum after each iteration.
Nagendra C.
asked 12/26/22Maximum You are given N number of inputs. Print the maximum of them after each input.
Maximum You are given N number of inputs. Print the maximum of them after each input.
Explanation In the example, you are given 6 inputs 1, 0, 3, 2, 9, 8
1 is maximum in the input 1.
1 is maximum in the input 1, 0.
3 is maximum in the input 1, 0, 3.
3 is maximum in the input 1, 0, 3, 2.
9 is maximum in the input 1, 0, 3, 2, 9.
9 is maximum in the input 1, 0, 3, 2, 9,
so,the output should be
1
3
3
9
9
Follow
1
Add comment
More
Report
1 Expert Answer
nums = [int(i) for i in input().split()] # get input of the form `1 3 5 2 1` and convert it to a list of integers: [1,3,5,2,1]
maximum = nums[0] # initialize the maximum to the first element in the list
for num in nums:
if num > maximum: #if the current value is greater than the maximum
maximum = num #update the maximum to be the current value
print(maximum) #print the maximum after each iteration
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.