-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_words_index.py
45 lines (39 loc) · 1.32 KB
/
key_words_index.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
# Returns a list of the urls associated with the keyword. If the keyword
# is not in the index, returns an empty list.
def lookup(index,keyword):
result = []
for i in index:
if i[0] == keyword:
result = i[1]
break
return result
# If the keyword is already in the index, add the url
# to the list of urls associated with that keyword.
# If the keyword is not in the index, add an entry to the index: [keyword,[url]]
def add_to_index(index,keyword,url):
item = []
found = False
for i in index:
if keyword == i[0]:
i[1].append(url)
found = True
break
if not found:
item.append(keyword)
item.append([url])
index.append(item)
return index
def main():
index = []
index2 = [['udacity', ['http://udacity.com', 'http://npr.org']],
['computing', ['http://acm.org']]]
add_to_index(index,'udacity','http://udacity.com')
add_to_index(index,'computing','http://acm.org')
add_to_index(index,'udacity','http://npr.org')
print(index)
#>>> [['udacity', ['http://udacity.com', 'http://npr.org']],
#>>> ['computing', ['http://acm.org']]]
print('lookup : {}'.format(lookup(index2,'udacity')))
#>>> ['http://udacity.com','http://npr.org']
if __name__ == '__main__':
main()