Python: Generate the running maximum, minimum value of the elements of an iterable
Python Itertools: Exercise-3 with Solution
Write a Python program to generate the running maximum, minimum value of the elements of an iterable.
Sample Solution:
Python Code:
from itertools import accumulate
def running_max_product(iters):
return accumulate(iters, max)
#List
result = running_max_product([1,3,2,7,9,8,10,11,12,14,11,12,7])
print("Running maximum value of a list:")
for i in result:
print(i)
#Tuple
result = running_max_product((1,3,3,7,9,8,10,9,8,14,11,15,7))
print("Running maximum value of a Tuple:")
for i in result:
print(i)
def running_min_product(iters):
return accumulate(iters, min)
#List
result = running_min_product([3,2,7,9,8,10,11,12,1,14,11,12,7])
print("Running minimum value of a list:")
for i in result:
print(i)
#Tuple
result = running_min_product((1,3,3,7,9,8,10,9,8,0,11,15,7))
print("Running minimum value of a Tuple:")
for i in result:
print(i)
Sample Output:
Running maximum value of a list: 1 3 3 7 9 9 10 11 12 14 14 14 14 Running maximum value of a Tuple: 1 3 3 7 9 9 10 10 10 14 14 15 15 Running minimum value of a list: 3 2 2 2 2 2 2 2 1 1 1 1 1 Running minimum value of a Tuple: 1 1 1 1 1 1 1 1 1 0 0 0 0
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to create an iterator from several iterables in a sequence and display the type and elements of the new iterator.
Next: Write a Python program to construct an infinite iterator that returns evenly spaced values starting with a specified number and step.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework