CompSci 6 Test 2 Fall 2011 Problem 1 (14 pts): ['salt', 'sage', 'cumin', 'cloves', 'basil'] ['salt', 'sage', 'cumin'] set(['cloves', 'basil', 'salt', 'sage', 'cumin']) set(['salt']) set(['basil', 'cloves']) ['flower', 'tree', 'frog'] set(['brown', 'red']) Problem 2 (8 pts): beser lesers Who is the most wins? ['Coach'] ['43fg', '83jkp', '73', '93k'] Problem 3 (6 pts): 4 10 43 Problem 4, Part A (6 pts): def topLevelDomain(url): ''' url is a string representing a url. Parts of the url may be separated by 0 or more '/'s, the first part will have at least one '.' in it. returns the last word before the first '/' or the last word if there is no '/' ''' parts = url.split("/") domainparts = parts[0].split(".") return domainparts[-1] ALT SOLN def topLevelDomain(url): index = url.find("/") if index > -1: url = url[:index] wordlist = url.split(".") return wordlist[len(wordlist)-1] Problem 4, Part B (6 pts): def domainsVisited(file): setall = set([]) for line in file: line = line.strip() setall.add(topLevelDomain(line)) answer = list(setall) answer.sort() return answer Problem 5, Part A (7 pts): def fileToList(file): #assume file open for reading. read in data # and return in this format: # list of lists, each list has three strings answer = [] for line in file: pos = line.find(" ") museum = line[:pos] line = line[pos+1:] pos = line.find(" ") artist = line[:pos] line = line[pos+1:] answer.append([museum, artist, line.strip()]) return answer ALT SOLN: def fileToList(file): answer = [] for line in file: mylist = line.split() painting = ' '.join(mylist[2:]) answer.append([mylist[0], mylist[1], painting]) return answer ALT SOLN: def fileToList(file): answer = [] for line in file: mylist = line.split() painting = '' for word in mylist[2:] painting += word + " " painting = painting.strip() answer.append([mylist[0], mylist[1], painting]) return answer ALT SOLN: def fileToList(file): answer = [] for line in file: line = line.replace(' ', ",", 2) # replace first two blanks with commas mylist = line.split(",") # split on commas answer.append(mylist) return answer Problem 5, Part B (9 pts): def artistInMostMuseums(data): aToM = {} #setup map artist to list of museums for item in data: if item[1] in aToM: aToM[item[1]].add(item[0]) else: # not in dictionary aToM[item[1]] = set([item[0]]) # use map to find max max = 0 artist = "" for item in aToM.keys(): if len(aToM[item]) > max: max = len(aToM[item]) artist = item return artist ALT SOLN: def artistInMostMuseums(data): dict = {} #setup map of museums to artists for item in data: if item[0] in dict: dict[item[0]].add(item[1]) else: dict[item[0]] = set([item[1]]) allartists = [] for v in dict.values(): allartists += [v] tuples = [(allartists.count(art),art) for art in set(allartists)] tuples.sort() return tuples[-1][1] Problem 6 (14 pts): def popularFish(data, num): fishcnt = {} for item in data: list = item.split(":") list = list[1:] # remove tank name for fish in list: if fish in fishcnt: fishcnt[fish] += 1 else: fishcnt[fish] = 1 popfish = [] for item in fishcnt.keys(): if fishcnt[item] > num: popfish.append(item) popfish.sort() return popfish ALT SOLN: def popularFish(data, num): allfish = [] for item in data: list = item.split(":") list = list[1:] for fish in list: allfish.append(fish) unique = set(allfish) popfish = [] for fish in unique: if allfish.count(fish) > num: popfish.append(fish) popfish.sort() return popfish