Skip to content

Commit 6c455a2

Browse files
Chore : Added S1_Stacked_Python.ipynb to repository
1 parent e6d7620 commit 6c455a2

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed

Stack/S1_Stacked_Python.ipynb

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "code",
19+
"execution_count": 4,
20+
"metadata": {
21+
"id": "scionqiNLfUT"
22+
},
23+
"outputs": [],
24+
"source": [
25+
"class Node:\n",
26+
" def __init__(self,value):\n",
27+
" self.data = value\n",
28+
" self.next = None"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"source": [
34+
"class Node:\n",
35+
" def __init__(self, data):\n",
36+
" self.data = data\n",
37+
" self.next = None\n",
38+
"\n",
39+
"class Stack:\n",
40+
" def __init__(self):\n",
41+
" self.top = None\n",
42+
"\n",
43+
" def is_empty(self):\n",
44+
" return self.top is None\n",
45+
"\n",
46+
" def push(self, value):\n",
47+
" new_node = Node(value)\n",
48+
" new_node.next = self.top\n",
49+
" self.top = new_node\n",
50+
"\n",
51+
" def peek(self):\n",
52+
" if self.is_empty():\n",
53+
" return 'Stack Empty'\n",
54+
" return self.top.data\n",
55+
"\n",
56+
" def pop(self):\n",
57+
" if self.is_empty():\n",
58+
" return 'Stack Empty'\n",
59+
" else:\n",
60+
" data = self.top.data\n",
61+
" self.top = self.top.next # ❌ Fix: Use assignment (=), not comparison (==)\n",
62+
" return data\n",
63+
" def traverse(self):\n",
64+
" temp = self.top\n",
65+
" while temp:\n",
66+
" print(temp.data)\n",
67+
" temp = temp.next\n"
68+
],
69+
"metadata": {
70+
"id": "Fd2sQw4YLk1e"
71+
},
72+
"execution_count": 48,
73+
"outputs": []
74+
},
75+
{
76+
"cell_type": "code",
77+
"source": [
78+
"def reverse_string(text):\n",
79+
" s = Stack()\n",
80+
"\n",
81+
" for i in text:\n",
82+
" s.push(i)\n",
83+
"\n",
84+
" res = \"\"\n",
85+
"\n",
86+
" while not s.is_empty(): # ✅ Use `is_empty()` instead of `isempty()`\n",
87+
" res += s.pop()\n",
88+
"\n",
89+
" print(res)\n"
90+
],
91+
"metadata": {
92+
"id": "bb8dHVcJLmRC"
93+
},
94+
"execution_count": 49,
95+
"outputs": []
96+
},
97+
{
98+
"cell_type": "code",
99+
"source": [
100+
"def text_editor(text, pattern):\n",
101+
" u = Stack()\n",
102+
" r = Stack()\n",
103+
"\n",
104+
" for i in text:\n",
105+
" u.push(i)\n",
106+
"\n",
107+
" for i in pattern:\n",
108+
" if not u.is_empty() and i == u.peek():\n",
109+
" data = u.pop()\n",
110+
" r.push(data) # ✅ Store removed character in redo stack\n",
111+
" else:\n",
112+
" if not r.is_empty(): # ✅ Check redo stack before popping\n",
113+
" data = r.pop()\n",
114+
" u.push(data) # ✅ Restore character from redo stack\n",
115+
"\n",
116+
" res = \"\"\n",
117+
" while not u.is_empty():\n",
118+
" res = u.pop() + res # ✅ Rebuild string in correct order\n",
119+
"\n",
120+
" return res\n"
121+
],
122+
"metadata": {
123+
"id": "DEXt7fDJLpl5"
124+
},
125+
"execution_count": 50,
126+
"outputs": []
127+
},
128+
{
129+
"cell_type": "code",
130+
"source": [
131+
"text_editor('america' , 'shut')"
132+
],
133+
"metadata": {
134+
"colab": {
135+
"base_uri": "https://localhost:8080/",
136+
"height": 35
137+
},
138+
"id": "azohncHhPXSY",
139+
"outputId": "653e7cf4-37fb-440d-9097-00e59c309467"
140+
},
141+
"execution_count": 51,
142+
"outputs": [
143+
{
144+
"output_type": "execute_result",
145+
"data": {
146+
"text/plain": [
147+
"'america'"
148+
],
149+
"application/vnd.google.colaboratory.intrinsic+json": {
150+
"type": "string"
151+
}
152+
},
153+
"metadata": {},
154+
"execution_count": 51
155+
}
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"source": [
161+
"s = Stack()"
162+
],
163+
"metadata": {
164+
"id": "_eDr87U3LtrK"
165+
},
166+
"execution_count": 52,
167+
"outputs": []
168+
},
169+
{
170+
"cell_type": "code",
171+
"source": [
172+
"s.is_empty()"
173+
],
174+
"metadata": {
175+
"colab": {
176+
"base_uri": "https://localhost:8080/"
177+
},
178+
"id": "8itEwN69LvnJ",
179+
"outputId": "c8eaa31e-c3e7-4408-e642-5983baedc21b"
180+
},
181+
"execution_count": 53,
182+
"outputs": [
183+
{
184+
"output_type": "execute_result",
185+
"data": {
186+
"text/plain": [
187+
"True"
188+
]
189+
},
190+
"metadata": {},
191+
"execution_count": 53
192+
}
193+
]
194+
},
195+
{
196+
"cell_type": "code",
197+
"source": [
198+
"reverse_string('hello')"
199+
],
200+
"metadata": {
201+
"colab": {
202+
"base_uri": "https://localhost:8080/"
203+
},
204+
"id": "JZVFp_cDLrg-",
205+
"outputId": "f6bee1d9-072e-4d4d-b67e-64fd4f0a3220"
206+
},
207+
"execution_count": 54,
208+
"outputs": [
209+
{
210+
"output_type": "stream",
211+
"name": "stdout",
212+
"text": [
213+
"olleh\n"
214+
]
215+
}
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"source": [
221+
"s.peek()"
222+
],
223+
"metadata": {
224+
"colab": {
225+
"base_uri": "https://localhost:8080/",
226+
"height": 35
227+
},
228+
"id": "-fyU5ELEL1Wh",
229+
"outputId": "e84f389e-c978-4508-9072-5cf1aaaab6d5"
230+
},
231+
"execution_count": 11,
232+
"outputs": [
233+
{
234+
"output_type": "execute_result",
235+
"data": {
236+
"text/plain": [
237+
"'Stack Empty'"
238+
],
239+
"application/vnd.google.colaboratory.intrinsic+json": {
240+
"type": "string"
241+
}
242+
},
243+
"metadata": {},
244+
"execution_count": 11
245+
}
246+
]
247+
},
248+
{
249+
"cell_type": "code",
250+
"source": [
251+
"s.pop()"
252+
],
253+
"metadata": {
254+
"colab": {
255+
"base_uri": "https://localhost:8080/",
256+
"height": 35
257+
},
258+
"id": "XIA39lHAL3Vx",
259+
"outputId": "3c38af68-e1a9-49cf-a52e-4a40068b37b7"
260+
},
261+
"execution_count": 12,
262+
"outputs": [
263+
{
264+
"output_type": "execute_result",
265+
"data": {
266+
"text/plain": [
267+
"'Stack Empty'"
268+
],
269+
"application/vnd.google.colaboratory.intrinsic+json": {
270+
"type": "string"
271+
}
272+
},
273+
"metadata": {},
274+
"execution_count": 12
275+
}
276+
]
277+
},
278+
{
279+
"cell_type": "code",
280+
"source": [
281+
"s.traverse()"
282+
],
283+
"metadata": {
284+
"id": "6fP69M2rLx5h"
285+
},
286+
"execution_count": 13,
287+
"outputs": []
288+
}
289+
]
290+
}
30.8 KB
Loading

0 commit comments

Comments
 (0)