Skip to content

Instantly share code, notes, and snippets.

@ciupicri
Created July 13, 2014 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ciupicri/3358aa7a7294cebf166c to your computer and use it in GitHub Desktop.
Save ciupicri/3358aa7a7294cebf166c to your computer and use it in GitHub Desktop.
max coroutine
#!/usr/bin/env python3
def max_coroutine(initial_value):
max_value = initial_value
while True:
next_value = (yield max_value)
max_value = max(next_value, max_value)
def main():
L = [1, 2, 0, 7, 5, 9, 13, 10, 8]
src_it = iter(L)
my_max_co = max_coroutine(next(src_it))
print("Initial maximum value: {0}".format(next(my_max_co)))
for i in src_it:
print("Maximum value after appending {0}: {1}".format(
i, my_max_co.send(i)))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment