procedure return a list of strings that break the source string up by the characters in the splitlist.
def split_string(source,splitlist): output = [] atsplit = True for char in source: if char in splitlist: atsplit = True: else: if atsplit: output.append(char) atsplit = False else: output[-1] = output[-1] + char return output
cut duplicated url on index list
def add_to_index(index, keyword, url): for entry in index: if entry[0] == keyword: if not url in entry[1]: entry[1].append(url) return # not found, add new keyword to index index.append([keyword, [url]])
One way search engines rank pages is to count the number of times a searcher clicks on a returned link. This indicates that the person doing the query thought this was a useful link for the query, so it should be higher in the rankings next time.
def record_user_click(index,keyword,url): urls = lookup(index, keyword) if urls: for entry in urls: if entry[0] == url: entry[1] = entry[1] + 1 def add_to_index(index,keyword,url): for entry in index: for entry[0] == keyword: for elment in entry[1]: if element[0] = url: return entry[1].append([url,0]) return index.append([keyword,[[url,0]]])
How program run fast making things fast. It depends algorithm analysis that most programmer spend time in procedure, well defined sequence of steps that can be executed mechanically. So gatta think about time and memory said cost of computer.
time consuming
import time def time_execution(code): start = time.clock() result = eval(code) run_time = time.clock() - start return result, run_time def spin_loop(n): i = 0 while i < n: i = i + 1 print time_execution('2 ** 10') Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.8.2] on linux (1024, 1.6000000000002124e-05) print time_execution('spin_loop(10000000)') (None, 0.33208099999999996)
Creating an Empty Hash Table, make_hashtable, that takes as input a number, nbuckets, and returns an empty hash table with nbuckets empty buckets.
def make_hashtable(nbuckets): i = 0 table = [] while i < nbuckets: table.append([]) i = i + 1 return table
for loop, we do not need variable like i.
def make_hashtable(nbuckets): i = 0 table = [] for unused < range(0, nbuckets): tabale.append([]) return table
Update hashtable, if key is already in the table, change the value to the new value.
def hashtable_update(htable,key,value): bucket = hashtable_get_bucket(htable, key) for entry in hashtable if entry[0] == key: entry[1] = value bucket.append([key, value])