Commit 25fe79b8 authored by steinbac's avatar steinbac

finished first exercise about scaling the payload for sorting lists

parent 6b2fafb7
import pytest
@pytest.fixture
def testlist():
value = [ (1,2,'def'), (2,-4,'ghi'), (3,6,'abc') ]
value.extend(value)
value.extend(value)
value.extend(value)
value.extend(value)
return value
def simple_sort(alist, n):
nlist = [ (x[n], x) for x in alist ]
nlist.sort()
return [ val for (key,val) in nlist ]
def test_simple_sort(benchmark, testlist):
sortedlist = benchmark(simple_sort,testlist,2)
assert sortedlist[0] == (3,6,'abc')
def inplace_sort(alist, n):
alist[:] = [ (x[n], x) for x in alist ]
alist.sort()
alist[:] = [ val for (key,val) in alist ]
return
def test_inplace_sort(benchmark, testlist):
benchmark(inplace_sort,testlist,2)
assert testlist[0] == (3,6,'abc')
import operator
def operator_sort(alist, n):
temp = alist[:]
temp.sort(key=operator.itemgetter(n))
return temp
def test_operator_sort(benchmark, testlist):
result = benchmark(operator_sort,testlist,2)
assert result[0] == (3,6,'abc')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment