-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.insert.at.py
executable file
·81 lines (54 loc) · 1.08 KB
/
02.insert.at.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# Type: exercise
# Teaches: list-indexing, list-slicing, list-comparison
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
Use only slicing.
1. Insert 100 between 1 and 2.
"""
l[2:2] = [100]
"""
2. Insert 99 at the very beginning.
"""
l[0:0] = [99]
"""
3. Insert 98 at the very end.
"""
l[len(l):len(l)] = [98]
"""
4. Insert 97 before 98.
"""
l[-1:-1] = [97]
"""
5. Replace [2,3,4] with [20,30,40] in a single operation.
"""
l[4:7] = [20,30,40]
"""
6. Assign a copy of the list to the variable 'b'.
"""
b = l[:]
"""
7. Replace 100 with -100.
"""
l[3:4] = [-100]
"""
8. Did 'b' change? What is 'l == b'?
"""
False
"""
9. Starting with 0, replace every third entry with the string "cat", in a
single operation.
"""
l[1::3] = ["cat"]*5
"""
10. Same as 9 but with fewer cats. Keep the first cat but replace the remaining with: squirrel, dog, chipmunk, bear.
"""
l[4::3] = ["squirrel", "dog", "chipmunk", "bear"]
"""
11. Is your list equal to this?
"""
answer =\
[ 99, 'cat', 1, -100, 'squirrel', 30, 40
, 'dog', 6, 7, 'chipmunk', 9, 97, 'bear'
]
l == answer