Skip to content

Instantly share code, notes, and snippets.

@kirca
Last active August 29, 2015 14:24
Show Gist options
  • Save kirca/1d80d7e5192c13aae8cf to your computer and use it in GitHub Desktop.
Save kirca/1d80d7e5192c13aae8cf to your computer and use it in GitHub Desktop.
Mergesort in Python, autogenerated test data with hypothesis
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