-
Notifications
You must be signed in to change notification settings - Fork 0
/
8 kyu Smallest unused ID
46 lines (33 loc) · 1.26 KB
/
8 kyu Smallest unused ID
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
"""
https://www.codewars.com/
Hey awesome programmer!
You've got much data to manage and of course you use zero-based and non-negative ID's to make each data item unique!
Therefore you need a method, which returns the smallest unused ID for your next new data item...
Note: The given array of used IDs may be unsorted. For test reasons there may be duplicate IDs, but you don't have to find or remove them!
Go on and code some pure awesomeness!
"""
def next_id(arr):
sorted_lst = sorted(arr)
# remove duplicates form the list
sorted_lst = list(set(sorted_lst))
# delete negative numbers from the list
new_sorted_lst = []
for i in range(len(sorted_lst)):
if sorted_lst[i] >= 0:
new_sorted_lst.append(sorted_lst[i])
# return the smallest unused ID
for i in range(len(new_sorted_lst)):
if new_sorted_lst[i] != i:
return i
return len(new_sorted_lst)
# tests:
print(next_id([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
print(next_id([5, 4, 3, 2, 1]))
print(next_id([0, 1, 2, 3, 5]))
print(next_id([0, 0, 0, 0, 0, 0]))
print(next_id([]))
print(next_id([0, 0, 1, 1, 2, 2]))
print(next_id([0, 1, 1, 1, 3, 2]))
print(next_id([0, 1, 0, 2, 0, 3]))
print(next_id([9, 8, 0, 1, 7, 6]))
print(next_id([9, 8, 7, 6, 5, 4]))