-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path10-DutchNationalFlagProblem.py
More file actions
50 lines (41 loc) · 1.33 KB
/
Copy path10-DutchNationalFlagProblem.py
File metadata and controls
50 lines (41 loc) · 1.33 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
49
50
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
"""
index = 0
next_0 = 0
next_2 = len(input_list) - 1
while index <= next_2:
if input_list[index] == 0:
input_list[index] = input_list[next_0] # Makes sure we don't accidentially overwrite a value
input_list[next_0] = 0
next_0 += 1
index += 1
elif input_list[index] == 2:
input_list[index] = input_list[next_2] # Makes sure we don't accidentially overwrite a value
input_list[next_2] = 2
next_2 -= 1
else:
index += 1
return input_list
def test_function(test_case):
sorted_array = sort_012(test_case)
if sorted_array == sorted(test_case):
print("Pass")
else:
print("Fail")
test_function([])
test_function([0])
test_function([1])
test_function([2])
test_function([0, 0])
test_function([0, 1])
test_function([2, 1])
test_function([0, 1, 2])
test_function([1, 2, 0])
test_function([2, 1, 0])
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])