Skip to content

Commit ff6bbbb

Browse files
committed
refactor
1 parent 0ab3bac commit ff6bbbb

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

README.md

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ geometry: margin=1in
66

77
# Introduction
88

9-
List of several and useful `Python` data structures to know for coding interviews.
9+
This guide includes a list of several and useful `Python` data structures to know for coding interviews.
1010

11-
It is intended to show main data structures incorporated in the language
11+
It is intended to show the main data structures incorporated in the language
1212
and their useful functions. More advance `Python` feature will not be shown here.
1313

14-
Information described here, can be integrated with the following list:
14+
Additional links:
1515

1616
| **Topic** | **Link** |
1717
|---|---|
@@ -42,7 +42,7 @@ Information described here, can be integrated with the following list:
4242
'True'
4343
>>> int('10')
4444
10
45-
>>> int('10a') # ValueError: invalid literal for int() with base 10: '10a'
45+
>>> int('10a') # `ValueError: invalid literal for int() with base 10: '10a'`
4646

4747
# Operations
4848
>>> 2 * 2
@@ -53,6 +53,8 @@ Information described here, can be integrated with the following list:
5353
2.0
5454
>>> 4 // 2 # `//` is the integer division
5555
2
56+
>>> 3 % 2
57+
1
5658

5759
# `min` and `max`
5860
>>> min(4, 2)
@@ -97,6 +99,11 @@ inf
9799
0
98100
1
99101
2
102+
>>> for i in range(2, -1, -1): print(i) # Equivalent to `reversed(range(3))`
103+
...
104+
2
105+
1
106+
0
100107

101108
# `enumerate`
102109
>>> for i, v in enumerate(range(3)): print(i, v)
@@ -171,7 +178,7 @@ inf
171178
[2]
172179
>>> l.index(2)
173180
0
174-
>>> l.index(12) # ValueError: 12 is not in list
181+
>>> l.index(12) # `ValueError: 12 is not in list`
175182

176183
# More compact way to define a list
177184
>>> l = [0] * 5
@@ -220,7 +227,7 @@ True
220227
>>> l = [10, 2, 0, 1]
221228
>>> sorted(l) # It returns a new list
222229
[0, 1, 2, 10]
223-
>>> l
230+
>>> l # Original list is not sorted
224231
[10, 2, 0, 1]
225232

226233
# Sort by a different key
@@ -229,9 +236,9 @@ True
229236
... ('Luke', 20),
230237
... ('Anna', 18),
231238
... ]
232-
>>> sorted(students, key=lambda s: s[1])
239+
>>> sorted(students, key=lambda s: s[1]) # It returns a new list
233240
[('Anna', 18), ('Luke', 20), ('Mark', 21)]
234-
>>> students.sort(key=lambda s: s[1])
241+
>>> students.sort(key=lambda s: s[1]) # In-place
235242
>>> students
236243
[('Anna', 18), ('Luke', 20), ('Mark', 21)]
237244
```
@@ -244,7 +251,7 @@ True
244251
>>> len(s)
245252
13
246253

247-
>>> s[0] = 'h' # Strings are immutable. So you will get: `TypeError: 'str' object does not support item assignment`
254+
>>> s[0] = 'h' # Strings are immutable: `TypeError: 'str' object does not support item assignment`
248255
>>> s += ' Another string' # A new string will be created, so concatenation is quite slow
249256

250257
>>> s = 'Hello'
@@ -255,7 +262,7 @@ True
255262
>>> ''.join(l)
256263
'hello'
257264

258-
>>> 'Hello' in s
265+
>>> 'lo' in s
259266
True
260267
>>> ord('a')
261268
97
@@ -266,7 +273,7 @@ True
266273
## Stacks
267274

268275
```python
269-
>>> stack = [] # We can use a list to simulate a stack
276+
>>> stack = [] # We can use a normal list to simulate a stack
270277

271278
>>> stack.append(0) # `O(1)`
272279
>>> stack.append(1)
@@ -344,7 +351,7 @@ True
344351
{1, 2}
345352
>>> len(s)
346353
2
347-
>>> s.add(1) # Duplicate elements are not allowed
354+
>>> s.add(1) # Duplicate elements are not allowed per definition
348355
>>> s
349356
{1, 2}
350357
>>> s.add('a') # We can mix types
@@ -417,22 +424,24 @@ False
417424
{'a': 'hello, world!', 'b': 11, 1: 'a new element'}
418425

419426
>>> d[0] += 10 # KeyError: 0
420-
>>> d.get(0, 'a default value') # Return a default value if key does not exist
421-
'a default value'
422-
>>> d.get(1, 'a default value') # Key `1` exists, so the actual value will be returned
427+
>>> d.get(0, 1) # Return `1` as default value since key `0` does not exist
428+
1
429+
>>> d.get(1, '?') # Key `1` exists, so the actual value will be returned
423430
'a new element'
431+
>>> d.get(10) is None
432+
True
424433
```
425434

426435
## Heaps
427436

428-
The following commands show how to work with a min heap.
429-
Currently, Python does not have public methods for the max heap.
437+
The following commands show how to work with a `min heap`.
438+
Currently, `Python` does not have public methods for the `max heap`.
430439
You can overcome this problem by applying one of the following strategies:
431440

432441
1. Invert the value of each number. So, for example, if you want to add
433442
1, 2 and 3 in the min heap, you can `heappush` -3, -2 and -1.
434443
When you `heappop` you invert the number again to get the proper value.
435-
This solution clearly works if your domain is composed by numbers ≥ 0.
444+
This solution clearly works if your domain is composed by numbers >= 0.
436445
1. [Invert your object comparison](https://stackoverflow.com/a/40455775).
437446

438447
```python
@@ -503,21 +512,21 @@ True
503512
```python
504513
>>> from collections import defaultdict
505514

506-
>>> dd = defaultdict(int)
507-
>>> dd['x'] += 1
508-
>>> dd
515+
>>> d = defaultdict(int)
516+
>>> d['x'] += 1
517+
>>> d
509518
defaultdict(<class 'int'>, {'x': 1})
510-
>>> dd['x'] += 2
511-
>>> dd
519+
>>> d['x'] += 2
520+
>>> d
512521
defaultdict(<class 'int'>, {'x': 3})
513-
>>> dd['y'] += 10
514-
>>> dd
522+
>>> d['y'] += 10
523+
>>> d
515524
defaultdict(<class 'int'>, {'x': 3, 'y': 10})
516525

517-
>>> dd = defaultdict(list)
518-
>>> dd['x'].append(1)
519-
>>> dd['x'].append(2)
520-
>>> dd
526+
>>> d = defaultdict(list)
527+
>>> d['x'].append(1)
528+
>>> d['x'].append(2)
529+
>>> d
521530
defaultdict(<class 'list'>, {'x': [1, 2]})
522531
```
523532

@@ -548,15 +557,15 @@ c 2
548557
```python
549558
>>> from collections import OrderedDict
550559

551-
>>> od = OrderedDict()
560+
>>> d = OrderedDict()
552561

553-
>>> od['first'] = 1
554-
>>> od['second'] = 2
555-
>>> od['third'] = 3
556-
>>> od
562+
>>> d['first'] = 1
563+
>>> d['second'] = 2
564+
>>> d['third'] = 3
565+
>>> d
557566
OrderedDict([('first', 1), ('second', 2), ('third', 3)])
558567

559-
>>> for k, v in od.items():
568+
>>> for k, v in d.items():
560569
... print(k, v)
561570
...
562571
first 1

0 commit comments

Comments
 (0)