Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7633ef8

Browse files
stacks, queues and dequeues implemented
1 parent aeea7ca commit 7633ef8

File tree

2 files changed

+292
-7
lines changed

2 files changed

+292
-7
lines changed

‎Arrays in Python.ipynb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,6 @@
533533
"print(unique_char_check(\"abcde\"))\n",
534534
"print(unique_char_check(\"abcdde\"))"
535535
]
536-
},
537-
{
538-
"cell_type": "code",
539-
"execution_count": null,
540-
"metadata": {},
541-
"outputs": [],
542-
"source": []
543536
}
544537
],
545538
"metadata": {

‎Stacks, Queues, Deques.ipynb

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Stacks\n",
8+
"Addition and deletion of items takes place through one end called the **top**. The enclosed end is called the **bottom**. An example is a stack of plates. New plates are placed at the top. Removal of plates also take place from the top. The item which is closer to the bottom is the one which had stayed in the stack the longest. The newer items stay close to the top. This is called **LIFO (Last In First Out)**.\n",
9+
"\n",
10+
"Insertion is called **push**. This means addition of item to the top. Deletion is called **pop**. This means removal of first item from the top. \n",
11+
"\n",
12+
"This data structure can be used for reversal of list."
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 7,
18+
"metadata": {},
19+
"outputs": [
20+
{
21+
"name": "stdout",
22+
"output_type": "stream",
23+
"text": [
24+
"is empty True\n",
25+
"peeak me\n",
26+
"size 3\n",
27+
"is empty False\n",
28+
"is empty True\n"
29+
]
30+
}
31+
],
32+
"source": [
33+
"\"\"\"\n",
34+
"Stack class implementation\n",
35+
"\"\"\"\n",
36+
"\n",
37+
"class Stack(object):\n",
38+
" def __init__(self):\n",
39+
" self.items = []\n",
40+
" \n",
41+
" def is_empty(self):\n",
42+
" return self.items == []\n",
43+
" \n",
44+
" def push(self, item):\n",
45+
" self.items.append(item)\n",
46+
" \n",
47+
" def pop(self):\n",
48+
" return self.items.pop()\n",
49+
" \n",
50+
" def peek(self):\n",
51+
" return self.items[len(self.items) - 1]\n",
52+
" \n",
53+
" def size(self):T\n",
54+
" return len(self.items)\n",
55+
" \n",
56+
"s = Stack()\n",
57+
"print(\"is empty\", s.is_empty())\n",
58+
"s.push(1)\n",
59+
"s.push(\"me\")\n",
60+
"print(\"peeak\", s.peek())\n",
61+
"s.push(True)\n",
62+
"print(\"size\", s.size())\n",
63+
"print(\"is empty\", s.is_empty())\n",
64+
"s.pop()\n",
65+
"s.pop()\n",
66+
"s.pop()\n",
67+
"print(\"is empty\", s.is_empty())"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": [
74+
"## Queues\n",
75+
"It is an ordered collection of items where addition of new items happen at one end, called the **rear** and removal of existing items occur at the other end, called the **front**. This is called First In First Out **(FIFO)**. **Enqueue** is adding an item at the rear. **Dequeue** is removal of item from the front."
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 9,
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"size 0\n",
88+
"dequeue 1\n",
89+
"size 1\n"
90+
]
91+
}
92+
],
93+
"source": [
94+
"\"\"\"\n",
95+
"Queue class Implementation\n",
96+
"\"\"\"\n",
97+
"class Queue(object):\n",
98+
" def __init__(self):\n",
99+
" self.items = []\n",
100+
" \n",
101+
" def is_empty(self):\n",
102+
" return self.items == []\n",
103+
" \n",
104+
" def enqueue(self, item):\n",
105+
" self.items.insert(0, item)\n",
106+
" \n",
107+
" def dequeue(self):\n",
108+
" return self.items.pop()\n",
109+
" \n",
110+
" def size(self):\n",
111+
" return len(self.items)\n",
112+
" \n",
113+
"q = Queue()\n",
114+
"print(\"size\", q.size())\n",
115+
"q.enqueue(1)\n",
116+
"q.enqueue(2)\n",
117+
"print(\"dequeue\", q.dequeue())\n",
118+
"print(\"size\", q.size())"
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"metadata": {},
124+
"source": [
125+
"## Deque\n",
126+
"Double ended queue. It is similar to queue, except that it has an unrestricted nature of addition and removal of items. New items can be added to front or rear of deque. Same is for removal of item. Hence, it has characteristics of stack as well as queue."
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 11,
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"size 2\n",
139+
"Hello World\n",
140+
"size 0\n",
141+
"is empty? True\n"
142+
]
143+
}
144+
],
145+
"source": [
146+
"\"\"\"\n",
147+
"Deque class implementation\n",
148+
"\"\"\"\n",
149+
"\n",
150+
"class Deque(object):\n",
151+
" def __init__(self):\n",
152+
" self.items = []\n",
153+
" \n",
154+
" def is_empty(self):\n",
155+
" return self.items == []\n",
156+
" \n",
157+
" def add_front(self, item):\n",
158+
" self.items.append(item)\n",
159+
" \n",
160+
" def add_rear(self, item):\n",
161+
" self.items.insert(0, item)\n",
162+
" \n",
163+
" def remove_front(self):\n",
164+
" return self.items.pop()\n",
165+
" \n",
166+
" def remove_rear(self):\n",
167+
" return self.items.pop(0)\n",
168+
" \n",
169+
" def size(self):\n",
170+
" return len(self.items)\n",
171+
" \n",
172+
"d = Deque()\n",
173+
"d.add_front(\"Hello\")\n",
174+
"d.add_rear(\"World\")\n",
175+
"print(\"size\", d.size())\n",
176+
"print(d.remove_front(), d.remove_rear())\n",
177+
"print(\"size\", d.size())\n",
178+
"print(\"is empty?\", d.is_empty())"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"metadata": {},
184+
"source": [
185+
"## Interview Questions"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 15,
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"True\n",
198+
"False\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"\"\"\"\n",
204+
"Balanced Parentheses Check: has all three types of parantheses. Given a string of brackets only \n",
205+
"(no spaces or other characters), check if it is balanced\n",
206+
"\"\"\"\n",
207+
"\n",
208+
"def balance_check(s):\n",
209+
" if len(s)%2 != 0:\n",
210+
" return False\n",
211+
" opening = set('({[')\n",
212+
" matches = set([('(',')'),('{','}'),('[',']')])\n",
213+
" stack = []\n",
214+
" for i in s:\n",
215+
" if i in opening:\n",
216+
" stack.append(i)\n",
217+
" else:\n",
218+
" if len(stack) == 0:\n",
219+
" return False\n",
220+
" last_open = stack.pop()\n",
221+
" if (last_open, i) not in matches:\n",
222+
" return False\n",
223+
" return len(stack) == 0\n",
224+
"\n",
225+
"print(balance_check('[]'))\n",
226+
"print(balance_check('[[[({(})]]]'))"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 19,
232+
"metadata": {},
233+
"outputs": [
234+
{
235+
"name": "stdout",
236+
"output_type": "stream",
237+
"text": [
238+
"1\n",
239+
"2\n"
240+
]
241+
}
242+
],
243+
"source": [
244+
"\"\"\"\n",
245+
"Implement a Queue using 2 Stacks\n",
246+
"\"\"\"\n",
247+
"class Queue2Stacks(object):\n",
248+
" def __init__(self):\n",
249+
" self.in_stack = []\n",
250+
" self.out_stack = []\n",
251+
" \n",
252+
" def enqueue(self, item):\n",
253+
" self.in_stack.append(item)\n",
254+
" \n",
255+
" def dequeue(self):\n",
256+
" if not self.out_stack:\n",
257+
" while self.in_stack:\n",
258+
" self.out_stack.append(self.in_stack.pop())\n",
259+
" return self.out_stack.pop()\n",
260+
" \n",
261+
"q = Queue2Stacks()\n",
262+
"q.enqueue(1)\n",
263+
"q.enqueue(2)\n",
264+
"q.enqueue(3)\n",
265+
"print(q.dequeue())\n",
266+
"q.enqueue(4)\n",
267+
"print(q.dequeue())"
268+
]
269+
}
270+
],
271+
"metadata": {
272+
"kernelspec": {
273+
"display_name": "Python 3",
274+
"language": "python",
275+
"name": "python3"
276+
},
277+
"language_info": {
278+
"codemirror_mode": {
279+
"name": "ipython",
280+
"version": 3
281+
},
282+
"file_extension": ".py",
283+
"mimetype": "text/x-python",
284+
"name": "python",
285+
"nbconvert_exporter": "python",
286+
"pygments_lexer": "ipython3",
287+
"version": "3.7.3"
288+
}
289+
},
290+
"nbformat": 4,
291+
"nbformat_minor": 2
292+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /