-
-
Save 1328/5bc87c23076faae9ce9002b4a247d117 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def merge(a_list,b_list): | |
| a_it = iter(a_list) | |
| b_it = iter(b_list) | |
| a = next(a_it, None) | |
| b = next(b_it, None) | |
| while True: | |
| if a is None: | |
| yield b # clear buffer | |
| yield from b_it | |
| break | |
| if b is None: | |
| yield a # clear buffer | |
| yield from a_it | |
| break | |
| if a<b: | |
| yield a | |
| a = next(a_it, None) | |
| else: | |
| yield b | |
| b = next(b_it, None) | |
| def solve(a, b): | |
| total = len(a) + len(b) | |
| odd = total % 2 | |
| mid = (total-1) // 2 | |
| it = merge(a,b) | |
| count = 0 | |
| for idx, x in enumerate(it): | |
| if idx != mid: | |
| continue | |
| if odd: | |
| return x | |
| else: | |
| return (x + next(it))/2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment