-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
executable file
·42 lines (33 loc) · 936 Bytes
/
Copy path7.py
File metadata and controls
executable file
·42 lines (33 loc) · 936 Bytes
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
# simulate a dictionary using a list
#add key "k" with value "v" to dictionary "d"
def add(a, k, v):
b=[k,v]
if len(a) ==0:
a.append(b)
print a
return a
for i in a:
if i[0] == b[0]:
c=a.index(i)
a.remove(i)
a.insert(c,b)
elif not b in a:
a.append(b)
print a
return a
#return value corresponding to key "k"
#return None if key k is not present
def get(d, k):
for num in d:
if num[0]==k:
print num[1]
return num[1]
return None
print '\n\n*******************OUTPUT********************\n'
assert(add([], "hello", 10) ==[["hello", 10]])
assert(add([["hello", 10]], "world", 20) == [["hello", 10], ["world", 20]])
assert(add([["hello",10],["world",20]], "hello", 30) == [["hello",30],["world",20]])
print '\n'
assert(get([["hello",10],["world",20]], "world") == 20)
assert(get([["abc",1],["def",2]], "ijk") == None)
print '\nAll conditions are verified..\n'