-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
48 lines (36 loc) · 1.27 KB
/
solution.py
File metadata and controls
48 lines (36 loc) · 1.27 KB
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
import math
def radix_base(values_to_sort, base):
# validate args
# -----
if (values_to_sort is None) or (values_to_sort == []):
raise ValueError("invalid arguments")
if (base < 2):
raise ValueError("invalid arguments")
for element in values_to_sort:
if not isinstance(element, int) or element < 0:
raise ValueError("invalid list element")
# -----
# calculate length (in base) of largest element
max_val = max(values_to_sort)
max_length = 0
while max_val > 0:
max_val = max_val // base
max_length += 1
for i in range(max_length):
# set empty list of buckets for categorization
buckets = [[] for _ in range(base)]
# adding elements to their respective buckets
for element in values_to_sort:
# calculate where the digit placement is
place = (element // base**i) % base
# add to bucket
buckets[place].append(element)
# concatenate the buckets into one list
values_to_sort = concat_list_elements(buckets)
return values_to_sort
def concat_list_elements(array):
new_list = []
for innerlist in array:
for ele in innerlist:
new_list.append(ele)
return new_list