Last active
August 29, 2015 14:24
-
-
Save kirca/1d80d7e5192c13aae8cf to your computer and use it in GitHub Desktop.
Mergesort in Python, autogenerated test data with hypothesis
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
from hypothesis import given | |
from hypothesis.strategies import lists, integers | |
def mergesort(lst): | |
def merge(l1, l2): | |
if not l1: | |
return l2 | |
if not l2: | |
return l1 | |
return ([l1[0]] + merge(l1[1:], l2) | |
if l1[0] < l2[0] | |
else [l2[0]] + merge(l1, l2[1:])) | |
length = len(lst) | |
if length < 2: | |
return lst | |
return merge( | |
mergesort(lst[:length/2]), | |
mergesort(lst[length/2:])) | |
@given(lists(integers())) | |
def test_mergesort(lst): | |
assert sorted(lst) == mergesort(lst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment