Skip to content

Commit 4877f51

Browse files
committed
add complexity for stacks, queues and heaps
1 parent ca44bcf commit 4877f51

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ OUTPUT_FILE = artifacts/python-for-coding-interview.pdf
33

44
.PHONY: linter
55
linter:
6-
markdownlint README.md --disable MD013
6+
markdownlint README.md --disable MD013 MD025
77

88
.PHONY: pdf
99
pdf:

README.md

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Information described here, can be integrated with the following list:
6868
>>> 2 ** 3 # Equivalent to `pow(2, 3)`
6969
8
7070

71-
# Math functions from the proper package
71+
# Math functions from the `math` package
7272
>>> import math
7373
>>> math.ceil(7.2)
7474
8
@@ -103,23 +103,24 @@ TODO
103103

104104
>>> t[0] = 10 # Tuples are immutable: `TypeError: 'tuple' object does not support item assignment`
105105

106-
>>> a, b, c = t
106+
>>> a, b, c = t # Unpacking
107107
>>> a
108108
1
109109
>>> b
110110
2
111111
>>> c
112112
'str'
113-
>>> a, _, _ = t # Get first element and ignore the other two values
113+
>>> a, _, _ = t # Unpacking: ignore second and third elements
114114
>>> a
115115
1
116116
```
117117

118118
## Lists
119119

120-
More info about time complexity for lists can be found [here][python-time-complexity].
120+
`Python` uses `Timsort` algorithm in `sort` and `sorted` (<https://en.wikipedia.org/wiki/Timsort>).
121121

122122
```python
123+
# Define a list
123124
>>> l = [1, 2, 'a']
124125
>>> type(l) # <class 'list'>
125126
>>> len(l)
@@ -129,6 +130,7 @@ More info about time complexity for lists can be found [here][python-time-comple
129130
>>> l[-1] # Last element of the list (equivalent to `l[len(l) - 1]`)
130131
'a'
131132

133+
# Some useful methods
132134
>>> l.append('b') # `O(1)`
133135
>>> l.pop() # `O(1)` just for the last element
134136
'b'
@@ -144,12 +146,35 @@ More info about time complexity for lists can be found [here][python-time-comple
144146
0
145147
>>> l.index(12) # ValueError: 12 is not in list
146148

149+
# More compact way to define a list
147150
>>> l = [0] * 5
148151
>>> l
149152
[0, 0, 0, 0, 0]
150153
>>> len(l)
151154
5
155+
>>> [k for k in range(5)]
156+
[0, 1, 2, 3, 4]
152157

158+
# Compact way to define 2D arrays
159+
>>> rows, cols = 2, 3
160+
>>> m = [[0] * cols for _ in range(rows)]
161+
>>> len(m) == rows
162+
True
163+
>>> all(len(m[k]) == cols for k in range(rows))
164+
True
165+
166+
# Built-in methods
167+
>>> l = [1, 2, 3, 4]
168+
>>> sum(l)
169+
10
170+
>>> any(v == 4 for v in l)
171+
True
172+
>>> any(v == 5 for v in l)
173+
False
174+
>>> all(v > 0 for v in l)
175+
True
176+
177+
# Sort list in-place (`sort`)
153178
>>> l = [10, 2, 0, 1]
154179
>>> l
155180
[10, 2, 0, 1]
@@ -160,12 +185,14 @@ More info about time complexity for lists can be found [here][python-time-comple
160185
>>> l
161186
[10, 2, 1, 0]
162187

188+
# Sort a list a return a new one (`sorted`)
163189
>>> l = [10, 2, 0, 1]
164190
>>> sorted(l) # It returns a new list
165191
[0, 1, 2, 10]
166192
>>> l
167193
[10, 2, 0, 1]
168194

195+
# Sort by a different key
169196
>>> students = [
170197
... ('Mark', 21),
171198
... ('Luke', 20),
@@ -176,13 +203,6 @@ More info about time complexity for lists can be found [here][python-time-comple
176203
>>> students.sort(key=lambda s: s[1])
177204
>>> students
178205
[('Anna', 18), ('Luke', 20), ('Mark', 21)]
179-
180-
>>> rows, cols = 2, 3
181-
>>> m = [[0] * cols for _ in range(rows)]
182-
>>> len(m) == rows
183-
True
184-
>>> all(len(m[k]) == cols for k in range(rows))
185-
True
186206
```
187207

188208
## Strings
@@ -215,7 +235,7 @@ True
215235
```python
216236
>>> stack = []
217237

218-
>>> stack.append(0)
238+
>>> stack.append(0) # `O(1)`
219239
>>> stack.append(1)
220240
>>> stack.append(2)
221241

@@ -227,7 +247,7 @@ True
227247
>>> stack[-1] # Top of the stack
228248
2
229249

230-
>>> stack.pop()
250+
>>> stack.pop() # `O(1)`
231251
2
232252
>>> stack.pop()
233253
1
@@ -250,7 +270,7 @@ True
250270
>>> queue = deque()
251271

252272
# Enqueue -> append()
253-
>>> queue.append(0)
273+
>>> queue.append(0) # `O(1)`
254274
>>> queue.append(1)
255275
>>> queue.append(2)
256276

@@ -263,7 +283,7 @@ True
263283
2
264284

265285
# Dequeue -> popleft()
266-
>>> queue.popleft()
286+
>>> queue.popleft() # `O(1)`
267287
0
268288
>>> queue.popleft()
269289
1
@@ -295,6 +315,8 @@ True
295315
>>> s.add('a') # We can mix types
296316
>>> s
297317
{1, 2, 'a'}
318+
>>> 1 in s # `O(1)`
319+
True
298320

299321
>>> s0 = {1, 2, 'a'}
300322
>>> s0
@@ -356,7 +378,7 @@ dict_values(['hello, world', 11])
356378
a hello, world
357379
b 11
358380

359-
>>> 'a' in d
381+
>>> 'a' in d # `O(1)`
360382
True
361383
>>> 1 in d
362384
False
@@ -395,7 +417,7 @@ You can overcome this problem by applying one of the following strategies:
395417
[1, 2, 3]
396418

397419
>>> min_heap = []
398-
>>> heapq.heappush(min_heap, 3)
420+
>>> heapq.heappush(min_heap, 3) # `O(log n)`
399421
>>> heapq.heappush(min_heap, 2)
400422
>>> heapq.heappush(min_heap, 1)
401423

@@ -404,7 +426,7 @@ You can overcome this problem by applying one of the following strategies:
404426
>>> len(min_heap)
405427
>>> min_heap[0]
406428
1
407-
>>> heapq.heappop(min_heap)
429+
>>> heapq.heappop(min_heap) # `O(log n)`
408430
1
409431
>>> min_heap
410432
[2, 3]
@@ -416,11 +438,6 @@ You can overcome this problem by applying one of the following strategies:
416438
>>> heapq.heappop(min_heap) # `IndexError: index out of range`
417439
```
418440

419-
## Searching
420-
421-
```python
422-
```
423-
424441
### collections.namedtuple
425442

426443
```python

0 commit comments

Comments
 (0)