Last active
February 19, 2019 13:43
-
-
Save purpleP/a027d7ca654af0f652f22a37e499a40a to your computer and use it in GitHub Desktop.
merge
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
from collections import Counter | |
import hypothesis.strategies as st | |
import hypothesis as hp | |
import pytest | |
def merge(xs, ys): | |
xs, ys = iter(xs), iter(ys) | |
none = object() | |
y = next(ys, none) | |
if y is not none: | |
while True: | |
try: | |
x = next(xs) | |
if x > y: | |
x, xs, y, ys = y, ys, x, xs | |
yield x | |
except StopIteration: | |
yield y | |
break | |
else: | |
ys = xs | |
yield from ys | |
@hp.given(st.lists(st.integers()), st.lists(st.integers())) | |
def test_merge(xs, ys): | |
merged = list(merge(sorted(xs), sorted(ys))) | |
assert all(x <= y for x, y in zip(merged, merged[1:])) | |
assert Counter(xs + ys) == Counter(merged) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment