Skip to content

Commit ddb4d17

Browse files
feat(04): add an exercise, "Swap Index" (#6)
Co-authored-by: HyoJeong Yun <[email protected]>
1 parent cb42aac commit ddb4d17

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

04-functions-modules/guide.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# functions and modules
22

33
## MinMax
4+
45
- difficulty: ★☆☆
56
- time: ~ 15m for beginners
67
- background: list, iteration
78

89
Implement `min`, `max` functions on your own.
910

11+
## Swap Index
12+
13+
- difficulty: ★☆☆
14+
- time: ~ 15m for beginners
15+
- background: list
16+
17+
Implement a function which swaps indices of list.
1018

1119
## Hanoi
1220
- difficulty: ★☆☆
@@ -22,4 +30,3 @@ Implement `hanoi` function on your own. `hanoi` function is a guide that tells h
2230
- background: function, recursion
2331

2432
Implement a function which calculates binomial coefficient, $\prescript{}{n}C_k$.
25-
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def swap_indices(ls, i, j):
2+
"""
3+
Swap ls[i] and ls[j].
4+
5+
>>> ls = [0, 1, 2, 3]
6+
>>> swap_indices(ls, 0, 2)
7+
>>> ls
8+
[2, 1, 0, 3]
9+
>>> swap_indices(ls, 1, 2)
10+
>>> ls
11+
[2, 0, 1, 3]
12+
"""
13+
ls[i], ls[j] = ls[j], ls[i]
14+
15+
16+
if __name__ == "__main__":
17+
import doctest
18+
19+
doctest.testmod(verbose=True)

04-functions-modules/swap_index.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def swap_indices(ls, i, j):
2+
"""
3+
Swap ls[i] and ls[j].
4+
5+
>>> ls = [0, 1, 2, 3]
6+
>>> swap_indices(ls, 0, 2)
7+
>>> ls
8+
[2, 1, 0, 3]
9+
>>> swap_indices(ls, 1, 2)
10+
>>> ls
11+
[2, 0, 1, 3]
12+
"""
13+
pass
14+
15+
16+
if __name__ == "__main__":
17+
import doctest
18+
19+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)