|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | """ |
4 | | -The version done in class |
| 4 | +One solution... |
5 | 5 | """ |
6 | 6 |
|
7 | 7 |
|
8 | 8 | def swap(seq): |
9 | | - return seq[-1:]+seq[1:-1]+seq[:1] |
| 9 | + """with the first and last items exchanged""" |
| 10 | + return seq[-1:] + seq[1:-1] + seq[:1] |
10 | 11 |
|
11 | 12 |
|
12 | 13 | assert swap('something') == 'gomethins' |
13 | 14 | assert swap(tuple(range(10))) == (9, 1, 2, 3, 4, 5, 6, 7, 8, 0) |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def rem(seq): |
| 18 | + """With every other item removed""" |
17 | 19 | return seq[::2] |
18 | 20 |
|
| 21 | + |
19 | 22 | assert rem('a word') == 'awr' |
20 | 23 |
|
21 | 24 |
|
22 | 25 | def rem4(seq): |
| 26 | + """With the first and last 4 items removed, and every other item in between""" |
23 | 27 | return seq[4:-4:2] |
24 | 28 |
|
25 | | -print(rem4((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), )) |
| 29 | + |
| 30 | +a_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) |
| 31 | +print(rem4(a_tuple)) |
| 32 | +assert rem4(a_tuple) == (5, 7) |
26 | 33 |
|
27 | 34 |
|
28 | 35 | def reverse(seq): |
| 36 | + """With the elements reversed (just with slicing)""" |
29 | 37 | return seq[::-1] |
30 | 38 |
|
31 | 39 |
|
32 | 40 | print(reverse('a string')) |
| 41 | +assert reverse([3, 6, 1, 8, 3, 7]) == [7, 3, 8, 1, 6, 3] |
33 | 42 |
|
34 | 43 |
|
35 | 44 | def thirds(seq): |
36 | | - i = len(seq)//3 |
| 45 | + """With the middle third, then last third, then the first third in the new order""" |
| 46 | + i = len(seq) // 3 |
37 | 47 | return seq[i:-i] + seq[-i:] + seq[:i] |
38 | 48 |
|
39 | | -print (thirds(tuple(range(12)))) |
| 49 | + |
| 50 | +print(thirds(tuple(range(12)))) |
| 51 | +assert thirds(tuple(range(12))) == (4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3) |
| 52 | + |
| 53 | + |
0 commit comments