Skip to content

Commit c3da321

Browse files
committed
add sort and sorted methods
1 parent 27d54ad commit c3da321

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.PHONY: linter
2+
linter:
3+
markdownlint README.md

README.md

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,33 @@ inf
6464
TODO
6565

6666

67-
## Arrays
67+
## Tuples
68+
```python
69+
>>> t = (1, 2, 'str')
70+
>>> type(t)
71+
<class 'tuple'>
72+
>>> t
73+
(1, 2, 'str')
74+
>>> len(t)
75+
3
76+
77+
>>> t[0] = 10 # Tuples are immutable: `TypeError: 'tuple' object does not support item assignment`
78+
79+
>>> a, b, c = t
80+
>>> a
81+
1
82+
>>> b
83+
2
84+
>>> c
85+
'str'
86+
>>> a, _, _ = t # Get first element and ignore the other two values
87+
>>> a
88+
1
89+
```
90+
91+
92+
93+
## Lists
6894
More info about time complexity for lists can be found [here][python-time-complexity].
6995
```python
7096
>>> l = [1, 2, 'a']
@@ -97,6 +123,33 @@ More info about time complexity for lists can be found [here][python-time-comple
97123
>>> len(l)
98124
5
99125

126+
>>> l = [10, 2, 0, 1]
127+
>>> l
128+
[10, 2, 0, 1]
129+
>>> l.sort() # It changes the original list
130+
>>> l
131+
[0, 1, 2, 10]
132+
>>> l.sort(reverse=True) # It changes the original list
133+
>>> l
134+
[10, 2, 1, 0]
135+
136+
>>> l = [10, 2, 0, 1]
137+
>>> sorted(l) # It returns a new list
138+
[0, 1, 2, 10]
139+
>>> l
140+
[10, 2, 0, 1]
141+
142+
>>> students = [
143+
... ('Mark', 21),
144+
... ('Luke', 20),
145+
... ('Anna', 18),
146+
... ]
147+
>>> sorted(students, key=lambda s: s[1])
148+
[('Anna', 18), ('Luke', 20), ('Mark', 21)]
149+
>>> students.sort(key=lambda s: s[1])
150+
>>> students
151+
[('Anna', 18), ('Luke', 20), ('Mark', 21)]
152+
100153
>>> rows, cols = 2, 3
101154
>>> m = [[0] * cols for _ in range(rows)]
102155
>>> len(m) == rows
@@ -131,29 +184,6 @@ True
131184
```
132185

133186

134-
## Tuples
135-
```python
136-
>>> t = (1, 2, 'str')
137-
>>> type(t)
138-
<class 'tuple'>
139-
>>> t
140-
(1, 2, 'str')
141-
>>> len(t)
142-
3
143-
144-
>>> t[0] = 10 # Tuples are immutable: `TypeError: 'tuple' object does not support item assignment`
145-
146-
>>> a, b, c = t
147-
>>> a
148-
1
149-
>>> b
150-
2
151-
>>> c
152-
'str'
153-
>>> a, _, _ = t # Get first element and ignore the other two values
154-
>>> a
155-
1
156-
```
157187

158188

159189
## Stacks

0 commit comments

Comments
 (0)