-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo44.py
60 lines (43 loc) · 984 Bytes
/
demo44.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
def replace_items(L, D):
""" (list of int, dict of {int: int}) -> NoneType
Some of the items in L may be keys in D. Replace those items with the
associated values in D.
>>> L = [1]
>>> replace_items(L, {1: 2})
>>> L
[2]
>>> L = [3]
>>> replace_items(L, {1: 2})
>>> L
[3]
>>> L = [1, 3, 1]
>>> replace_items(L, {1: 2, 3: 4, 5: 6})
>>> L
[2, 4, 2]
"""
for x in range(len(L)):
if L[x] in D:
L[x] = D[L[x]]
def test(L, D):
for k in D:
for i in range(len(L)):
if L[i] == k:
L[i] = D[k]
#import doctest
#doctest.testmod()
if __name__ == '__main__':
L = [1]
replace_items(L, {1: 2})
print L
L = [3]
replace_items(L, {1: 2})
print L
L = [1, 3, 1]
replace_items(L, {1: 2, 3: 4, 5: 6})
print L
L = [0,1]
replace_items(L, {0:1, 1:2})
print L
L2 = [3]
test(L2, {1: 2})
print L2