Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Median of Three using yield and lambda solution in Clear category for Median of Three by hbczmxy
"""
Median of Three
Elementary+
Given an iterable of ints , create and return a new iterable whose first two elements are the same as in items,
after which each element equals the median of the three elements in the original list ending in that position.
Wait...You don't know what the "median" is? Go check out the separate "Median" mission on CheckiO.
Input: Iterable of ints.
Output: Iterable of ints.
The mission was taken from Python CCPS 109 Fall 2018.
It’s being taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen
"""
from typing import Iterable
def median_three(els: Iterable[int]) -> Iterable[int]:
median_of_three = lambda x: sorted(x)[1]
for index, item in enumerate(els):
if index in (0, 1):
yield item
else:
# Wrong: els[index-2:index]; this contain only two items
yield median_of_three(els[index-2:index+1])
if __name__ == '__main__':
print("Example:")
print(list(median_three([1, 2, 3, 4, 5, 6, 7])))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(median_three([1, 2, 3, 4, 5, 6, 7])) == [1, 2, 2, 3, 4, 5, 6]
assert list(median_three([1])) == [1]
assert list(median_three([5,2,9,1,7,4,6,3,8])) == [5,2,5,2,7,4,6,4,6]
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 18, 2021
Comments: