Skip to content

Commit 08595ee

Browse files
committed
answers for queue practice probs
1 parent 13f0d8f commit 08595ee

File tree

3 files changed

+139
-76
lines changed

3 files changed

+139
-76
lines changed

data_structures/6_Queue/6_queue.ipynb

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
{
1818
"cell_type": "code",
19-
"execution_count": 4,
19+
"execution_count": 9,
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
@@ -25,7 +25,7 @@
2525
},
2626
{
2727
"cell_type": "code",
28-
"execution_count": 5,
28+
"execution_count": 10,
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
@@ -36,7 +36,7 @@
3636
},
3737
{
3838
"cell_type": "code",
39-
"execution_count": 6,
39+
"execution_count": 11,
4040
"metadata": {},
4141
"outputs": [
4242
{
@@ -45,7 +45,7 @@
4545
"[135, 132.12, 131.1]"
4646
]
4747
},
48-
"execution_count": 6,
48+
"execution_count": 11,
4949
"metadata": {},
5050
"output_type": "execute_result"
5151
}
@@ -56,7 +56,7 @@
5656
},
5757
{
5858
"cell_type": "code",
59-
"execution_count": 7,
59+
"execution_count": 12,
6060
"metadata": {},
6161
"outputs": [
6262
{
@@ -65,7 +65,7 @@
6565
"131.1"
6666
]
6767
},
68-
"execution_count": 7,
68+
"execution_count": 12,
6969
"metadata": {},
7070
"output_type": "execute_result"
7171
}
@@ -76,7 +76,7 @@
7676
},
7777
{
7878
"cell_type": "code",
79-
"execution_count": 8,
79+
"execution_count": 13,
8080
"metadata": {},
8181
"outputs": [
8282
{
@@ -85,7 +85,7 @@
8585
"[135, 132.12]"
8686
]
8787
},
88-
"execution_count": 8,
88+
"execution_count": 13,
8989
"metadata": {},
9090
"output_type": "execute_result"
9191
}
@@ -96,7 +96,7 @@
9696
},
9797
{
9898
"cell_type": "code",
99-
"execution_count": 9,
99+
"execution_count": 14,
100100
"metadata": {},
101101
"outputs": [
102102
{
@@ -105,7 +105,7 @@
105105
"132.12"
106106
]
107107
},
108-
"execution_count": 9,
108+
"execution_count": 14,
109109
"metadata": {},
110110
"output_type": "execute_result"
111111
}
@@ -116,7 +116,7 @@
116116
},
117117
{
118118
"cell_type": "code",
119-
"execution_count": 10,
119+
"execution_count": 15,
120120
"metadata": {},
121121
"outputs": [
122122
{
@@ -125,7 +125,7 @@
125125
"135"
126126
]
127127
},
128-
"execution_count": 10,
128+
"execution_count": 15,
129129
"metadata": {},
130130
"output_type": "execute_result"
131131
}
@@ -136,18 +136,18 @@
136136
},
137137
{
138138
"cell_type": "code",
139-
"execution_count": 11,
139+
"execution_count": 16,
140140
"metadata": {},
141141
"outputs": [
142142
{
143143
"ename": "IndexError",
144144
"evalue": "pop from empty list",
145145
"output_type": "error",
146146
"traceback": [
147-
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
148-
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
149-
"\u001b[1;32m<ipython-input-11-09580442e205>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mwmt_stock_price_queue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
150-
"\u001b[1;31mIndexError\u001b[0m: pop from empty list"
147+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
148+
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
149+
"\u001b[0;32m/var/folders/nk/nzndlkn14wx_mdp2_t31tqmc0000gn/T/ipykernel_96232/1260761002.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mwmt_stock_price_queue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
150+
"\u001b[0;31mIndexError\u001b[0m: pop from empty list"
151151
]
152152
}
153153
],
@@ -164,7 +164,7 @@
164164
},
165165
{
166166
"cell_type": "code",
167-
"execution_count": 14,
167+
"execution_count": null,
168168
"metadata": {},
169169
"outputs": [],
170170
"source": [
@@ -174,7 +174,7 @@
174174
},
175175
{
176176
"cell_type": "code",
177-
"execution_count": 16,
177+
"execution_count": null,
178178
"metadata": {},
179179
"outputs": [
180180
{
@@ -197,7 +197,7 @@
197197
},
198198
{
199199
"cell_type": "code",
200-
"execution_count": 17,
200+
"execution_count": null,
201201
"metadata": {},
202202
"outputs": [
203203
{
@@ -217,7 +217,7 @@
217217
},
218218
{
219219
"cell_type": "code",
220-
"execution_count": 18,
220+
"execution_count": null,
221221
"metadata": {},
222222
"outputs": [
223223
{
@@ -237,7 +237,7 @@
237237
},
238238
{
239239
"cell_type": "code",
240-
"execution_count": 19,
240+
"execution_count": null,
241241
"metadata": {},
242242
"outputs": [
243243
{
@@ -257,7 +257,7 @@
257257
},
258258
{
259259
"cell_type": "code",
260-
"execution_count": 20,
260+
"execution_count": null,
261261
"metadata": {},
262262
"outputs": [
263263
{
@@ -275,9 +275,14 @@
275275
"q.pop()"
276276
]
277277
},
278+
{
279+
"cell_type": "markdown",
280+
"metadata": {},
281+
"source": []
282+
},
278283
{
279284
"cell_type": "code",
280-
"execution_count": 21,
285+
"execution_count": null,
281286
"metadata": {
282287
"scrolled": true
283288
},
@@ -307,11 +312,13 @@
307312
},
308313
{
309314
"cell_type": "code",
310-
"execution_count": 29,
315+
"execution_count": 17,
311316
"metadata": {},
312317
"outputs": [],
313318
"source": [
314319
"from collections import deque\n",
320+
"import time\n",
321+
"import threading\n",
315322
"\n",
316323
"class Queue:\n",
317324
" \n",
@@ -328,12 +335,43 @@
328335
" return len(self.buffer)==0\n",
329336
" \n",
330337
" def size(self):\n",
331-
" return len(self.buffer)"
338+
" return len(self.buffer)\n",
339+
"\n",
340+
" def place_order(self, orders):\n",
341+
" for order in orders:\n",
342+
" self.enqueue(order)\n",
343+
" time.sleep(0.5)\n",
344+
"\n",
345+
" def serve_order(self):\n",
346+
" while not self.is_empty:\n",
347+
" time.sleep(2)\n",
348+
" print(self.dequeue())\n",
349+
"\n",
350+
" def food_ordering_system(self, orders):\n",
351+
" q = Queue()\n",
352+
" t1 = threading.Thread(target=q.place_order, args=(orders,))\n",
353+
" t2 = threading.Thread(target=q.serve_order)\n",
354+
" t1.start()\n",
355+
" time.sleep(1)\n",
356+
" t2.start()\n",
357+
" t1.join()\n",
358+
" t2.join()"
359+
]
360+
},
361+
{
362+
"cell_type": "code",
363+
"execution_count": 18,
364+
"metadata": {},
365+
"outputs": [],
366+
"source": [
367+
"orders = ['pizza', 'samosa', 'pasta', 'biryani', 'burger']\n",
368+
"q = Queue()\n",
369+
"q.food_ordering_system(orders)"
332370
]
333371
},
334372
{
335373
"cell_type": "code",
336-
"execution_count": 33,
374+
"execution_count": null,
337375
"metadata": {},
338376
"outputs": [],
339377
"source": [
@@ -358,7 +396,7 @@
358396
},
359397
{
360398
"cell_type": "code",
361-
"execution_count": 34,
399+
"execution_count": null,
362400
"metadata": {},
363401
"outputs": [
364402
{
@@ -378,7 +416,7 @@
378416
},
379417
{
380418
"cell_type": "code",
381-
"execution_count": 32,
419+
"execution_count": null,
382420
"metadata": {},
383421
"outputs": [
384422
{
@@ -398,7 +436,7 @@
398436
},
399437
{
400438
"cell_type": "code",
401-
"execution_count": 35,
439+
"execution_count": null,
402440
"metadata": {},
403441
"outputs": [
404442
{
@@ -433,7 +471,7 @@
433471
"name": "python",
434472
"nbconvert_exporter": "python",
435473
"pygments_lexer": "ipython3",
436-
"version": "3.7.3"
474+
"version": "3.9.5"
437475
}
438476
},
439477
"nbformat": 4,

data_structures/6_Queue/myqueue.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Data structure tutorial exercise: Queue
2+
from collections import deque
3+
import threading
4+
import time
5+
6+
7+
class Queue:
8+
9+
def __init__(self):
10+
self.buffer = deque()
11+
12+
def enqueue(self, val):
13+
self.buffer.appendleft(val)
14+
15+
def dequeue(self):
16+
return self.buffer.pop()
17+
18+
def is_empty(self):
19+
return len(self.buffer) == 0
20+
21+
def size(self):
22+
return len(self.buffer)
23+
24+
# exercise prob 1) food ordering system
25+
26+
def place_order(self, orders):
27+
for order in orders:
28+
self.enqueue(order)
29+
time.sleep(0.5)
30+
31+
def serve_order(self):
32+
while not self.is_empty:
33+
time.sleep(2)
34+
print(self.dequeue())
35+
36+
def food_ordering_system(self, orders):
37+
q = Queue()
38+
t1 = threading.Thread(target=q.place_order, args=(orders,))
39+
t2 = threading.Thread(target=q.serve_order)
40+
t1.start()
41+
time.sleep(1)
42+
t2.start()
43+
t1.join()
44+
t2.join()
45+
46+
# exercise prob 2) print binary numbers from 1 to 10
47+
48+
def front(self):
49+
return self.buffer.popleft()
50+
51+
52+
def binary_numbers(n):
53+
q = Queue()
54+
q.enqueue('1')
55+
entry = None
56+
for i in range(n):
57+
entry = q.dequeue()
58+
print(entry)
59+
q.enqueue(entry + '0')
60+
q.enqueue(entry + '1')
61+
62+
63+
if __name__ == '__main__':
64+
# exercise prob 1)
65+
orders = ['pizza', 'samosa', 'pasta', 'biryani', 'burger']
66+
q = Queue()
67+
q.food_ordering_system(orders) # not sure how to test
68+
69+
# exercise prob 2)
70+
binary_numbers(10)

0 commit comments

Comments
 (0)