def quicksort(l):
match l:
case a, *b:
l1 = quicksort([e for e in b if e <= a])
l2 = quicksort([e for e in b if e > a])
return l1 + [a] + l2
case _:
return l
import codewars_test as test
from solution import quicksort
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(quicksort([]), [])
test.assert_equals(quicksort([1]), [1])
test.assert_equals(quicksort([1,2,3,4,5]), [1,2,3,4,5])
test.assert_equals(quicksort([5,4,3,2,1]), [1,2,3,4,5])
test.assert_equals(quicksort([3,1,2,5,4]), [1,2,3,4,5])
"Ask for forgiveness, not permission"-style. This is about 40% faster in the general case.
Refactor to one-liner.