-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig_sorting.py
63 lines (51 loc) · 1.42 KB
/
big_sorting.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
#https://www.hackerrank.com/challenges/big-sorting/problem?isFullScreen=true
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'bigSorting' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts STRING_ARRAY unsorted as parameter.
#
def bigSorting(unsorted):
# Write your code here
bucket = dict()
index = 0
sort = []
while unsorted:
for item in unsorted:
idx = len(item) - 1 - index
if idx >= 0:
c = item[idx]
val = int(c)
if val not in bucket:
bucket[val] = []
bucket[val].append(item)
unsorted = []
for k in range(0, 10):
i = 0
while k in bucket and i < len(bucket[k]):
item = bucket[k][i]
if len(item) > index + 1:
unsorted.append(bucket[k].pop(i))
else:
sort.append(bucket[k].pop(i))
index += 1
# print(unsorted)
# print(bucket, '\n')
return sort
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
unsorted = []
for _ in range(n):
unsorted_item = input()
unsorted.append(unsorted_item)
result = bigSorting(unsorted)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()