I am constantly on the mentioned Kata time:

task

You'll get an array of random integers and a number n. You'll need to extract n least integers out of it, preserving the original order.## Examples

`performant_smallest ([1, 2, 3, 4, 5], 3) == [1, 2, 3] performant_smallest ([5, 4, 3, 2, 1], 3) == [3, 2, 1] performant_smallest ([1, 2, 3, 4, 1], 3) == [1, 2, 1] performant_smallest ([1, 2, 3, -4, 0], 3) == [1, -4, 0] performant_smallest ([2, 1, 3, 2, 3], 3) == [2, 1, 2]`

## Remarks

- The number
`n`

is always smaller than the array length- The array contains duplicates, and these must be returned in order of occurrence

Your solution must be at leastso fastas a reference, the tests passed in`12000+ ms`

- If you have failed some tests, try again. Otherwise, your solution is not efficient enough.
## test Suite

`Tests: 1500 Array size: 4500 Speed: [-100..100] Number of items to return: 50-100% of the array Note: You can not use Python 2`

I wrote different programs for it:

```
by heapq import *
from collections import counters as counters
from copy import copy
def heap_pop (lst, n):
Heap, res, i = copy (lst), [0]* n, 0
heapify
c = count (heappop (heap) for _ in range (s))
for itm in lst:
if c.get (itm, 0)> 0:
res[i] = itm
i + = 1
c[itm] - = 1
if i == n: break
res res
def sorting (lst, n):
res, i = [0]* n, 0
c = count (sorted)[:n])
for itm in lst:
if c.get (itm, 0)> 0:
res[i] = itm
i + = 1
c[itm] - = 1
if i == n: break
res res
def n_smallest (lst, n):
Heap, res, i = copy (lst), [0]* n, 0
heapify
c = count (nsmallest (n, heap))
for itm in lst:
if c.get (itm, 0)> 0:
res[i] = itm
i + = 1
c[itm] - = 1
if i == n: break
res res
def counter_sort (lst, n):
c = count (lst)
d, x, res, sm = sorted (c), {}, [0]* n, 0
for k in d:
z = min (c[k], n-sm)
x[k] = z
sm + = z
if sm == n: break
sm = 0
for itm in lst:
if x.get (itm, 0)> 0:
res[sm] = itm
sm + = 1
x[itm] - = 1
if sm == n: break
res res
def cs2 (lst, n):
c = count (lst)
d, x, res, sm = sorted (c), {}, [0]* n, 0
for k in d:
z = min (c[k], n-sm)
x[k] = z
sm + = z
if sm == n: break
sm = 0
for itm in (itm for itm in lst, if itm in x):
if x.get (itm, 0)> 0:
res[sm] = itm
sm + = 1
x[itm] - = 1
if sm == n: break
res res
def csx (lst, n):
c, sm = {i: 0 for i in the range (-100, 101)}, 0
for itm in lst: c[itm] + = 1
d, x, res, sm = sorted (c), {}, [0]* n, 0
for k in d:
z = min (c[k], n-sm)
x[k] = z
sm + = z
if sm == n: break
sm = 0
for itm in (itm for itm in lst, if itm in x):
if x.get (itm, 0)> 0:
res[sm] = itm
sm + = 1
x[itm] - = 1
if sm == n: break
res res
```

I have all compared (4500 random numbers in [-100, 100], and `r`

in the [2250, 4500]`with 10000 iterations) to simulate the actual data set. I am very surprised that Counter Sort did not work, considering it was a linear time and it was just two`

O (2)`Iterations in it.`

counter_sort () `was clearly the best performer, but it comes down to 1068 of the test cases before it fails.

[![enter image description here][2]][2]