edges = [] for line in open("data/twitter.txt"): name_a, name_b = line.strip().split(';') edges.append((name_a, name_b)) print(edges[:10]) def following(user, edges): "Return a list of all users USERS is following." followees = [] for follower, followee in edges: if follower == user: followees.append(followee) return followees print(following("@Fox", edges)) %timeit following("@Fox", edges) edge_dict = {} for line in open("data/twitter.txt"): name_a, name_b = line.strip().split(';') if name_a in edge_dict: edge_dict[name_a].append(name_b) else: edge_dict[name_a] = [name_b] def following2(user, edges): return edges[user] %timeit following2("@Fox", edge_dict) edges = [] for line in open("data/twitter.txt"): name_a, name_b = line.strip().split(';') # repeatedly add edges to the network (1000 times) for i in range(1000): edges.append((name_a, name_b)) %timeit following("@Fox", edges) edge_dict = {} for line in open("data/twitter.txt"): name_a, name_b = line.strip().split(';') for i in range(1000): if name_a in edge_dict: edge_dict[name_a].append(name_b) else: edge_dict[name_a] = [name_b] %timeit following2("@Fox", edge_dict) def translate(word): "Convert a word to latin." vowels = 'aeiouAEIOU' start = 0 end = '' # loop over all characters in word for i, char in enumerate(word): # if this character is not a vowel if char not in vowels: # it is a consonant, so add it to the end. end += char # if it is a vowel else: # we set the starting position to # the position of this character start = i break return word[start:] + end + 'ay' # write your code here # do not modify the code below, it is for testing your answer only! # it should output True if you did well print(starts_with_vowel("egg") == True) print(starts_with_vowel("bacon") == False) def starts_with_vowel(word): "Return True if WORD starts with a vowel, False otherwise." vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') return word.startswith(vowels) def add_suffix(word, suffix): "Return WORD with SUFFIX attached." # insert your code here # do not modify the code below, it is for testing your answer only! # it should output True if you did well print(add_suffix("egg", "ay") == "eggay") print(add_suffix("egg", "oing") == "eggoing") def add_ay(word): "Return WORD with 'ay' attached." return add_suffix(word, "ay") def translate(word, suffix): if starts_with_vowel(word): return add_suffix(word, suffix) return translate(word[1:] + word[0], suffix) def pig_latinize(word): "Pig latinize WORD." return translate(word, "ay") from IPython.core.display import HTML def css_styling(): styles = open("styles/custom.css", "r").read() return HTML(styles) css_styling()