-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path067_N smallest elements in original order.py
88 lines (63 loc) · 2.47 KB
/
067_N smallest elements in original order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Codewars Coding Challenge
Day 67/366
Level 6kyu
N smallest elements in original order
Your task is to write a function that does just what the title suggests (so, fair warning, be aware that you are not getting out of it just throwing a lame bas sorting method there) with an array/list/vector of integers and the expected number n of smallest elements to return.
Also:
the number of elements to be returned cannot be higher than the array/list/vector length;
elements can be duplicated;
in case of duplicates, just return them according to the original order (see third example for more clarity).
Same examples and more in the test cases:
first_n_smallest([1,2,3,4,5],3) == [1,2,3]
first_n_smallest([5,4,3,2,1],3) == [3,2,1]
first_n_smallest([1,2,3,4,1],3) == [1,2,1]
first_n_smallest([1,2,3,-4,0],3) == [1,-4,0]
first_n_smallest([1,2,3,4,5],0) == []
Performance version by FArekkusu also available.
def first_n_smallest(arr, n):
pass
https://www.codewars.com/kata/5aec1ed7de4c7f3517000079/train/python
"""
# My Solution
def first_n_smallest(arr, n):
arr = sorted(enumerate(arr), key=lambda x: x[1])[:n]
return [i[1] for i in sorted(arr, key=lambda x: x[0])]
"""
Sample Tests
import codewars_test as test
from solution import first_n_smallest
@test.describe("Fixed Tests")
def fixed_tests():
@test.it('Basic Test Cases')
def basic_test_cases():
test.assert_equals(first_n_smallest([1,2,3,4,5],3), [1,2,3])
test.assert_equals(first_n_smallest([5,4,3,2,1],3), [3,2,1])
test.assert_equals(first_n_smallest([1,2,3,1,2],3), [1,2,1])
test.assert_equals(first_n_smallest([1,2,3,-4,0],3), [1,-4,0])
test.assert_equals(first_n_smallest([1,2,3,4,5],0), [])
test.assert_equals(first_n_smallest([1,2,3,4,5],5), [1,2,3,4,5])
test.assert_equals(first_n_smallest([1,2,3,4,2],4), [1,2,3,2])
test.assert_equals(first_n_smallest([2,1,2,3,4,2],2), [2,1])
test.assert_equals(first_n_smallest([2,1,2,3,4,2],3), [2,1,2])
test.assert_equals(first_n_smallest([2,1,2,3,4,2],4), [2,1,2,2])
"""
"""
Solutions From Codewars
=1=
def first_n_smallest(arr, n):
m = sorted(arr)[:n]
return [m.pop(m.index(i)) for i in arr if i in m]
=2=
def first_n_smallest(l, n):
a = sorted(l)[:n]
m = []
for i in l:
if i in a:
m.append(i)
a.pop(a.index(i))
return m
=3=
def first_n_smallest(arr, n):
return [x[1] for x in sorted(sorted(enumerate(arr), key=lambda x: x[1])[:n])]
"""