Skip to content

Commit 1484ee9

Browse files
committed
docs: add sort 1, 2 and 0 lab
1 parent afb66e0 commit 1484ee9

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"graffitiCellId": "id_pdy0t0f"
7+
},
8+
"source": [
9+
"### Problem Statement\n",
10+
"\n",
11+
"Write a function that takes an input array (or Python list) consisting of only `0`s, `1`s, and `2`s, and sorts that array in a single traversal.\n",
12+
"\n",
13+
"Note that if you can get the function to put the `0`s and `2`s in the correct positions, this will aotumatically cause the `1`s to be in the correct positions as well."
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 1,
19+
"metadata": {
20+
"graffitiCellId": "id_vsgisrd"
21+
},
22+
"outputs": [],
23+
"source": [
24+
"def sort_012(input_list):\n",
25+
" next_pos_0 = 0\n",
26+
" next_pos_2 = len(input_list) - 1\n",
27+
"\n",
28+
" front_index = 0\n",
29+
"\n",
30+
" while front_index <= next_pos_2:\n",
31+
" if input_list[front_index] == 0:\n",
32+
" input_list[front_index] = input_list[next_pos_0]\n",
33+
" input_list[next_pos_0] = 0\n",
34+
" next_pos_0 += 1\n",
35+
" front_index += 1\n",
36+
" elif input_list[front_index] == 2: \n",
37+
" input_list[front_index] = input_list[next_pos_2] \n",
38+
" input_list[next_pos_2] = 2\n",
39+
" next_pos_2 -= 1\n",
40+
" else:\n",
41+
" front_index += 1"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {
47+
"graffitiCellId": "id_rrxcwca"
48+
},
49+
"source": [
50+
"<span class=\"graffiti-highlight graffiti-id_rrxcwca-id_1f2p5yd\"><i></i><button>Show Solution</button></span>"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 2,
56+
"metadata": {
57+
"graffitiCellId": "id_2sqv48t"
58+
},
59+
"outputs": [],
60+
"source": [
61+
"def test_function(test_case):\n",
62+
" sort_012(test_case)\n",
63+
" print(test_case)\n",
64+
" if test_case == sorted(test_case):\n",
65+
" print(\"Pass\")\n",
66+
" else:\n",
67+
" print(\"Fail\")\n"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 3,
73+
"metadata": {
74+
"graffitiCellId": "id_x3ai5yy"
75+
},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"[0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2]\n",
82+
"Pass\n"
83+
]
84+
}
85+
],
86+
"source": [
87+
"test_case = [0, 0, 2, 2, 2, 1, 1, 1, 2, 0, 2]\n",
88+
"test_function(test_case)"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 4,
94+
"metadata": {
95+
"graffitiCellId": "id_t8sucox"
96+
},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]\n",
103+
"Pass\n"
104+
]
105+
}
106+
],
107+
"source": [
108+
"test_case = [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]\n",
109+
"test_function(test_case)"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 5,
115+
"metadata": {
116+
"graffitiCellId": "id_mk5p9ba"
117+
},
118+
"outputs": [
119+
{
120+
"name": "stdout",
121+
"output_type": "stream",
122+
"text": [
123+
"[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]\n",
124+
"Pass\n"
125+
]
126+
}
127+
],
128+
"source": [
129+
"test_case = [2, 2, 0, 0, 2, 1, 0, 2, 2, 1, 1, 1, 0, 1, 2, 0, 2, 0, 1]\n",
130+
"test_function(test_case)"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": null,
136+
"metadata": {
137+
"graffitiCellId": "id_p3tj97t"
138+
},
139+
"outputs": [],
140+
"source": []
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"metadata": {
146+
"graffitiCellId": "id_0sohrou"
147+
},
148+
"outputs": [],
149+
"source": []
150+
}
151+
],
152+
"metadata": {
153+
"graffiti": {
154+
"firstAuthorId": "dev",
155+
"id": "id_ni46pwa",
156+
"language": "EN"
157+
},
158+
"kernelspec": {
159+
"display_name": "Python 3",
160+
"language": "python",
161+
"name": "python3"
162+
},
163+
"language_info": {
164+
"codemirror_mode": {
165+
"name": "ipython",
166+
"version": 3
167+
},
168+
"file_extension": ".py",
169+
"mimetype": "text/x-python",
170+
"name": "python",
171+
"nbconvert_exporter": "python",
172+
"pygments_lexer": "ipython3",
173+
"version": "3.6.3"
174+
}
175+
},
176+
"nbformat": 4,
177+
"nbformat_minor": 2
178+
}

0 commit comments

Comments
 (0)