Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Median of Three by PythonLearner
from typing import Iterable
def median_three(numbers: Iterable[int]) -> Iterable[int]:
buffer = []
buffer_size = 3
middle_index = int((buffer_size)/2)
for i, number in enumerate(numbers):
buffer.append(number)
if i < buffer_size-1:
yield number
if len(buffer) == buffer_size:
yield sorted(buffer)[middle_index]
buffer.pop(0)
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]
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 6, 2019