Skip to content

Commit 44ace56

Browse files
committed
add heaps sections and several container datatypes
1 parent 0a17a92 commit 44ace56

File tree

1 file changed

+161
-10
lines changed

1 file changed

+161
-10
lines changed

README.md

Lines changed: 161 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,28 @@ True
107107

108108

109109
## Strings
110-
- ord
111-
- chr
110+
```python
111+
>>> s = 'Hello, world!'
112+
>>> type(s) # <class 'str'>
113+
>>> len(s)
114+
13
115+
116+
>>> s[0] = 'h' # Strings are immutable. So you will get: `TypeError: 'str' object does not support item assignment`
117+
118+
>>> ls = list(s)
119+
>>> ls
120+
['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']
121+
>>> ls[0] = 'h'
122+
>>> ''.join(ls)
123+
'hello, world!'
124+
125+
>>> 'Hello' in s
126+
True
127+
>>> ord('a')
128+
97
129+
>>> chr(97)
130+
'a'
131+
```
112132

113133

114134
## Stacks
@@ -182,35 +202,166 @@ True
182202
```
183203

184204

205+
## Sets
206+
```python
207+
```
208+
209+
185210
## Hash Tables
186-
TODO
211+
```python
212+
```
187213

188214

189215
## Heaps
190-
TODO
216+
The following commands show how to work with a min heap.
217+
Currently, Python does not have public methods for the max heap.
218+
You can overcome this problem by applying one of the following strategies:
219+
1. Invert the value of each number. So, for example, if you want to add
220+
1, 2 and 3 in the min heap, you can `heappush` -3, -2 and -1.
221+
When you `heappop` you invert the number again to get the proper value.
222+
This solution clearly works if your domain is composed by numbers &ge; 0.
223+
1. [Invert your object comparison](https://stackoverflow.com/a/40455775).
224+
225+
226+
```python
227+
>>> import heapq
228+
229+
>>> min_heap = [3, 2, 1]
230+
>>> heapq.heapify(min_heap)
231+
>>> min_heap
232+
[1, 2, 3]
233+
234+
>>> min_heap = []
235+
>>> heapq.heappush(min_heap, 3)
236+
>>> heapq.heappush(min_heap, 2)
237+
>>> heapq.heappush(min_heap, 1)
238+
239+
>>> min_heap
240+
[1, 3, 2]
241+
>>> len(min_heap)
242+
>>> min_heap[0]
243+
1
244+
>>> heapq.heappop(min_heap)
245+
1
246+
>>> min_heap
247+
[2, 3]
248+
249+
>>> heapq.heappop(min_heap)
250+
2
251+
>>> heapq.heappop(min_heap)
252+
3
253+
>>> heapq.heappop(min_heap) # `IndexError: index out of range`
254+
```
191255

192256

193257
## Searching
194-
TODO
258+
```python
259+
```
195260

196261

197262
## collections
198-
263+
More information about container datatypes can be found
264+
in [the official documentation][python-collections].
199265

200266
### namedtuple
201-
TODO
267+
```python
268+
>>> from collections import namedtuple
202269

270+
>>> Point = namedtuple('Point', 'x y')
203271

204-
### Counter
205-
TODO
272+
>>> p0 = Point(1, 2)
273+
>>> p0
274+
Point(x=1, y=2)
275+
>>> p0.x
276+
1
277+
>>> p0.y
278+
2
279+
280+
>>> p1 = Point(x=1, y=2)
281+
>>> p0 == p1
282+
True
283+
284+
# Python >= 3.6.1
285+
>>> from typing import Any, NamedTuple
286+
>>>
287+
>>> class Point(NamedTuple):
288+
... x: int
289+
... y: int
290+
...
291+
292+
>>> p0 = Point(1, 2)
293+
>>> p1 = Point(x=1, y=2)
294+
>>> p0 == p1
295+
True
296+
```
206297

207298

208299
### defaultdict
209-
TODO
300+
```python
301+
>>> from collections import defaultdict
302+
303+
>>> dd = defaultdict(int)
304+
>>> dd['x'] += 1
305+
>>> dd
306+
defaultdict(<class 'int'>, {'x': 1})
307+
>>> dd['x'] += 2
308+
>>> dd
309+
defaultdict(<class 'int'>, {'x': 3})
310+
>>> dd['y'] += 10
311+
>>> dd
312+
defaultdict(<class 'int'>, {'x': 3, 'y': 10})
313+
314+
>>> dd = defaultdict(list)
315+
>>> dd['x'].append(1)
316+
>>> dd['x'].append(2)
317+
>>> dd
318+
defaultdict(<class 'list'>, {'x': [1, 2]})
319+
```
320+
321+
322+
### Counter
323+
```python
324+
>>> from collections import Counter
325+
326+
>>> c = Counter('abcabcaa')
327+
>>> c
328+
Counter({'a': 4, 'b': 2, 'c': 2})
329+
>>> c.keys()
330+
dict_keys(['a', 'b', 'c'])
331+
>>> c.items()
332+
dict_items([('a', 4), ('b', 2), ('c', 2)])
333+
>>> for k, v in c.items():
334+
... print(k, v)
335+
...
336+
a 4
337+
b 2
338+
c 2
339+
>>> c['d'] # It acts as a `defaultdict` for missing keys
340+
0
341+
```
210342

211343

212344
### OrderedDict
345+
```python
346+
>>> from collections import OrderedDict
347+
348+
>>> od = OrderedDict()
349+
350+
>>> od['first'] = 1
351+
>>> od['second'] = 2
352+
>>> od['third'] = 3
353+
>>> od
354+
OrderedDict([('first', 1), ('second', 2), ('third', 3)])
355+
356+
>>> for k, v in od.items():
357+
... print(k, v)
358+
...
359+
first 1
360+
second 2
361+
third 3
362+
```
213363

214364

215365

216366
[python-time-complexity]: https://wiki.python.org/moin/TimeComplexity
367+
[python-collections]: https://docs.python.org/3/library/collections.html

0 commit comments

Comments
 (0)