-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_4.py
61 lines (46 loc) · 1.2 KB
/
problem_4.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
"""
Solution of;
Project: Problems vs Algorithms
Problem 4: Dutch National Flag Problem
"""
def sort_012(input_list):
"""
Given an input array consisting on only 0, 1, and 2, sort the array in a single traversal.
Args:
input_list(list): List to be sorted
"""
# if input_list is empty
if input_list == []:
return "list is not valid!"
zeros = []
ones = []
twos = []
result = []
# iterate for items
for item in input_list:
if item == 0:
zeros.append(0)
elif item == 1:
ones.append(1)
else:
twos.append(2)
result += zeros
result += ones
result += twos
return result
def test_function(test_case):
if test_case == []:
print([])
print("Pass")
return
sorted_array = sort_012(test_case)
print(sorted_array)
if sorted_array == sorted(test_case):
print("Pass")
else:
print("Fail")
test_function([0, 0, 2, 2, 2, 1, 1, 1, 2, 0, 2])
test_function([2, 1, 2, 0, 0, 2, 1, 0, 1, 0, 0, 2, 2,
2, 1, 2, 0, 0, 0, 2, 1, 0, 2, 0, 0, 1])
test_function([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2])
test_function([])