# Define a procedure, add_to_index, # that takes 3 inputs: # - an index: [[<keyword>,[<url>,...]],...] # - a keyword: String # - a url: String # 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): for entry in index: if entry[0] == keyword: entry[1].append(url) return index.append([keyword, [url]]) def lookup(index, keyword): for entry in index: if entry[0] == keyword: return entry[1] return [] def add_page_to_index(index, url, content): words = content.split() for word in words: add_to_index(index, word, url) 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']]]
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question