Created
July 13, 2014 18:07
-
-
Save ciupicri/3358aa7a7294cebf166c to your computer and use it in GitHub Desktop.
max coroutine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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