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 f318b4a

Browse files
modification and updations
1 parent 839e2f5 commit f318b4a

File tree

6 files changed

+757
-112
lines changed

6 files changed

+757
-112
lines changed

‎Arrays in Python.ipynb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"The two byte location alloted to a character is called **cell**. Now, each cell must be of same size, not necessarily of 2 bytes.\n",
1515
"\n",
1616
"## Referential Arrays\n",
17-
"Consider an array of strings. All strings will have different length. Hence they cannot be stored in consecutive locations (as discussed earlier). Hence in an array of strings, a reference of each string is stored. All references take up equal space, i.e. each cell has a reference to a string stored in another location.\n",
17+
"Consider an array of strings. All strings will have different length. Hence they cannot be stored in consecutive locations (as discussed earlier). Hence in an array of strings, a reference of each string is stored. All references take up equal space, i.e. each cell has a reference to a string that is stored in another location.\n",
1818
"In python, list and tuples are referential in nature. \n",
1919
"\n",
2020
"In slices, it looks like a new list is formed. In reality, it holds the reference to the same objects as the original list. Changes made to slice do not change the original list. The new list element now points to a new object that is different from the old list.\n",
@@ -275,7 +275,7 @@
275275
},
276276
{
277277
"cell_type": "code",
278-
"execution_count": 51,
278+
"execution_count": 2,
279279
"metadata": {},
280280
"outputs": [
281281
{
@@ -284,7 +284,7 @@
284284
"text": [
285285
"First Implementation\n",
286286
"5\n",
287-
"3\n",
287+
"2\n",
288288
"Second Implementation\n",
289289
"5\n",
290290
"2\n",
@@ -311,7 +311,7 @@
311311
"\n",
312312
"print(\"First Implementation\")\n",
313313
"print(find_missing_element([1,2,3,4,5], [1,2,3,4]))\n",
314-
"print(find_missing_element([1,2,3,4,5], [1,2,5,4]))\n",
314+
"print(find_missing_element([1,2,3,4,5], [1,5,3,4]))\n",
315315
"\n",
316316
"import collections\n",
317317
"\n",
@@ -324,21 +324,21 @@
324324
" if d[num] == 0:\n",
325325
" return num\n",
326326
" else:\n",
327-
" d[num] -= 0\n",
327+
" d[num] -= 1\n",
328328
"\n",
329329
"print(\"Second Implementation\")\n",
330-
"print(find_missing_element([1,2,3,4,5], [1,2,3,4]))\n",
331-
"print(find_missing_element([1,2,3,4,5], [1,5,3,4]))\n",
330+
"print(find_missing_element2([1,2,3,4,5], [1,2,3,4]))\n",
331+
"print(find_missing_element2([1,2,3,4,5], [1,5,3,4]))\n",
332332
"\n",
333-
"def find_missing_element2(arr1, arr2): # clever trick O(n)\n",
333+
"def find_missing_element3(arr1, arr2): # clever trick O(n)\n",
334334
" result = 0\n",
335335
" for num in arr1+arr2:\n",
336336
" result ^= num\n",
337337
" return result\n",
338338
"\n",
339339
"print(\"Third Implementation\")\n",
340-
"print(find_missing_element([1,2,3,4,5], [1,2,3,4]))\n",
341-
"print(find_missing_element([1,2,3,4,5], [1,5,3,4]))"
340+
"print(find_missing_element3([1,2,3,4,5], [1,2,3,4]))\n",
341+
"print(find_missing_element3([1,2,3,4,5], [1,5,3,4]))"
342342
]
343343
},
344344
{
@@ -551,7 +551,7 @@
551551
"name": "python",
552552
"nbconvert_exporter": "python",
553553
"pygments_lexer": "ipython3",
554-
"version": "3.7.3"
554+
"version": "3.7.4"
555555
}
556556
},
557557
"nbformat": 4,

‎Dictionaries.ipynb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
"contains (in) |O(1)\n",
1515
"iteration |O(n)"
1616
]
17-
},
18-
{
19-
"cell_type": "code",
20-
"execution_count": null,
21-
"metadata": {},
22-
"outputs": [],
23-
"source": []
2417
}
2518
],
2619
"metadata": {
@@ -39,7 +32,7 @@
3932
"name": "python",
4033
"nbconvert_exporter": "python",
4134
"pygments_lexer": "ipython3",
42-
"version": "3.7.3"
35+
"version": "3.7.4"
4336
}
4437
},
4538
"nbformat": 4,

‎Linked Lists.ipynb

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
"metadata": {},
66
"source": [
77
"## Singly Linked Lists\n",
8-
"It is a collection of nodes. Each node has an element and a reference to the next node in the collections. **HEAD** is a member that points to the first node in the collection. The last node is called **TAIL**. The last node is has a reference to *None*. Traversing a linked list is also called **link hopping** or **pointer hopping**.\n",
8+
"It is a collection of nodes. Each node has an element and a reference to the next node in the collections. **HEAD** is a member that points to the first node in the collection. The last node is called **TAIL**. The last node has a reference to *None*. Traversing a linked list is also called **link hopping** or **pointer hopping**.\n",
9+
"\n",
10+
"Linked List have constant time insertion and deletion at any position, while arrays require O(n). Accessing an element takes O(k) to access kth position element. This is because one has to traverse from head to the kth position. Arrays, on the rather hand, require constant time to perform the operation. An important property of a linked list is it does not have a predefined fixed size. Hence can be expanded at run-time. \n",
911
"\n",
10-
"An important property of a linked list is it does not have a predefined fixed size.\n",
1112
"To insert an element to the HEAD:\n",
1213
"* create a new node with the element\n",
1314
"* new node point to head reference\n",
14-
"* head point to ne node\n",
15+
"* head point to new node\n",
1516
"\n",
1617
"To insert an element to the TAIL:\n",
1718
"* create a new node\n",
@@ -23,7 +24,7 @@
2324
"\n",
2425
"Removing an element from TAIL: not an easy task\n",
2526
"\n",
26-
"O(n) -> accessing element\n",
27+
"O(n) -> accessing element\n",
2728
"O(1) -> insertion and deletion of an element "
2829
]
2930
},
@@ -37,7 +38,7 @@
3738
"output_type": "stream",
3839
"text": [
3940
"1\n",
40-
"<__main__.Node object at 0x10c6005c0>\n",
41+
"<__main__.Node object at 0x103ece550>\n",
4142
"2\n"
4243
]
4344
}
@@ -72,14 +73,15 @@
7273
},
7374
{
7475
"cell_type": "code",
75-
"execution_count": 5,
76+
"execution_count": 6,
7677
"metadata": {},
7778
"outputs": [
7879
{
7980
"name": "stdout",
8081
"output_type": "stream",
8182
"text": [
82-
"1\n"
83+
"1\n",
84+
"3\n"
8385
]
8486
}
8587
],
@@ -88,7 +90,7 @@
8890
" def __init__(self, ele):\n",
8991
" self.prev_node = None\n",
9092
" self.value = ele\n",
91-
" self.next_node = Node\n",
93+
" self.next_node = None\n",
9294
" \n",
9395
"a = DoublyLLNode(1)\n",
9496
"b = DoublyLLNode(2)\n",
@@ -99,7 +101,8 @@
99101
"b.next_node = c\n",
100102
"c.prev_node = b\n",
101103
"\n",
102-
"print(b.prev_node.value)"
104+
"print(b.prev_node.value) # a\n",
105+
"print(b.next_node.value) # c"
103106
]
104107
},
105108
{
@@ -111,7 +114,7 @@
111114
},
112115
{
113116
"cell_type": "code",
114-
"execution_count": 2,
117+
"execution_count": 5,
115118
"metadata": {},
116119
"outputs": [
117120
{
@@ -125,19 +128,14 @@
125128
],
126129
"source": [
127130
"\"\"\"\n",
128-
"Singly Linked List Cycle List: check if the list has cycles\n",
129-
"\"\"\"\n",
130-
"class Node(object):\n",
131-
" def __init__(self, ele):\n",
132-
" self.value = ele\n",
133-
" self.nextnode = None\n",
134-
" \n",
131+
"Singly Linked List Cycle Check: check if the list has cycles\n",
132+
"\"\"\" \n",
135133
"def cycle_check(node):\n",
136134
" marker1 = node\n",
137135
" marker2 = node\n",
138-
" while marker2!=None and marker2.nextnode!=None:\n",
139-
" marker1 = marker1.nextnode\n",
140-
" marker2 = marker2.nextnode.nextnode\n",
136+
" while marker2!=None and marker2.next_node!=None:\n",
137+
" marker1 = marker1.next_node\n",
138+
" marker2 = marker2.next_node.next_node\n",
141139
" if marker2 == marker1:\n",
142140
" return True\n",
143141
" return False\n",
@@ -146,16 +144,16 @@
146144
"b = Node(2)\n",
147145
"c = Node(3)\n",
148146
"\n",
149-
"a.nextnode = b\n",
150-
"b.nextnode = c\n",
147+
"a.next_node = b\n",
148+
"b.next_node = c\n",
151149
"print(cycle_check(a))\n",
152-
"c.nextnode = a\n",
150+
"c.next_node = a\n",
153151
"print(cycle_check(a))"
154152
]
155153
},
156154
{
157155
"cell_type": "code",
158-
"execution_count": 11,
156+
"execution_count": 10,
159157
"metadata": {},
160158
"outputs": [
161159
{
@@ -175,33 +173,33 @@
175173
"\n",
176174
"def reverse_list(node):\n",
177175
" previous = None\n",
178-
" nextnode = None\n",
176+
" next_node = None\n",
179177
" current = node\n",
180178
" while current:\n",
181-
" nextnode = current.nextnode\n",
182-
" current.nextnode = previous\n",
179+
" next_node = current.next_node\n",
180+
" current.next_node = previous\n",
183181
" previous = current\n",
184-
" current = nextnode\n",
182+
" current = next_node\n",
185183
"\n",
186184
"a = Node(1)\n",
187185
"b = Node(2)\n",
188186
"c = Node(3)\n",
189187
"d = Node(4)\n",
190188
"\n",
191-
"a.nextnode = b\n",
192-
"b.nextnode = c\n",
193-
"c.nextnode = d\n",
189+
"a.next_node = b\n",
190+
"b.next_node = c\n",
191+
"c.next_node = d\n",
194192
"\n",
195193
"reverse_list(a)\n",
196194
"# now, d is the head node. \n",
197-
"print(d.nextnode.value)\n",
198-
"print(c.nextnode.value)\n",
199-
"print(b.nextnode.value)"
195+
"print(d.next_node.value)\n",
196+
"print(c.next_node.value)\n",
197+
"print(b.next_node.value)"
200198
]
201199
},
202200
{
203201
"cell_type": "code",
204-
"execution_count": 16,
202+
"execution_count": 11,
205203
"metadata": {},
206204
"outputs": [
207205
{
@@ -210,7 +208,7 @@
210208
"3"
211209
]
212210
},
213-
"execution_count": 16,
211+
"execution_count": 11,
214212
"metadata": {},
215213
"output_type": "execute_result"
216214
}
@@ -225,12 +223,12 @@
225223
" left_pointer = head\n",
226224
" right_pointer = head\n",
227225
" for i in range(n-1):\n",
228-
" if not right_pointer.nextnode:\n",
226+
" if not right_pointer.next_node:\n",
229227
" raise LookupError(\"List is short\")\n",
230-
" right_pointer = right_pointer.nextnode\n",
231-
" while right_pointer.nextnode:\n",
232-
" right_pointer = right_pointer.nextnode\n",
233-
" left_pointer = left_pointer.nextnode\n",
228+
" right_pointer = right_pointer.next_node\n",
229+
" while right_pointer.next_node:\n",
230+
" right_pointer = right_pointer.next_node\n",
231+
" left_pointer = left_pointer.next_node\n",
234232
" return left_pointer.value\n",
235233
" \n",
236234
" \n",
@@ -239,9 +237,9 @@
239237
"c = Node(3)\n",
240238
"d = Node(4)\n",
241239
"\n",
242-
"a.nextnode = b\n",
243-
"b.nextnode = c\n",
244-
"c.nextnode = d\n",
240+
"a.next_node = b\n",
241+
"b.next_node = c\n",
242+
"c.next_node = d\n",
245243
"\n",
246244
"nth_last(2, a)"
247245
]
@@ -263,7 +261,7 @@
263261
"name": "python",
264262
"nbconvert_exporter": "python",
265263
"pygments_lexer": "ipython3",
266-
"version": "3.7.3"
264+
"version": "3.7.4"
267265
}
268266
},
269267
"nbformat": 4,

0 commit comments

Comments
(0)

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