# # Question 1 # # Part A unic = len([x for x in set(nuts) if x[0] == 'c']) # Part B solo = len([x for x in set(nuts) if nuts.count(x) == 1]) # Part C truenut = [x for x in set(nuts) if x.endswith("nut")] # Part D nuttiest = sorted([(nuts.count(x),x) for x in nuts])[-1][1] # Part E: temp = sorted([(nuts.count(x),x) for x in set(nuts)]) freqs = [x[1] for x in temp] # ALTERNATE SOLUTION temp = sorted(set(nuts)) freqs = [ (x,nuts.count(x) for x in temp ] # alph ordered freqs = sorted(freqs, key=operator.itemgetter(1)) freqs = [ x[0] for x in freqs ] # # Question 2 # # Part A # # given 'hello' and 'ohell', checks for 'hello' in 'ohellohell' # by concatenating rotated string, the start should line up with the end, # meaning the original string must be there if no letters are out of order # because all possible rotations can be found in there # Part B # # given 'hello' and 'ohell', find rotation point, i.e., 'ello' and 'h' # checks every index, i.e., every possible rotation, to see where the point # of rotation occurs # Part C # # given 'hello' and 'ohell', compares the two lists # ['elloh', 'hello', 'llohe', 'lohel', 'ohell'] # ['elloh', 'hello', 'llohe', 'lohel', 'ohell'] # checks if both strings produce exactly the same rotations for all possible # indices, sorts them to guarantee rotations they appear in the same order no # matter the order in which they were generated # # Question 3 # # #1 pass # #2 fail 4 # #3 pass # This code does not remove the friend's beaten card thus # allowing it to beaten multiple times. # #1 pass # #2 pass # #3 fail 2 # This code does not sort the friend's cards thus # it does not choose the best card to remove. # #1 fail 3 # #2 fail 4 # #3 pass # This code removes all the firend's cards your card can beat thus # it does not reflect how each hand matches up overall. # # Question 4 # # Part A # A movie with 11 elements is BOTH top grossing and top rated. # The last element is the amount of money grossed by the movie. # Part B # In this case, result is given as an empty dictionary and it is returned # as a dictionary with each entry newly created such that its keys are # (title, year) and its values are only [rank, rating]. # In this case, result is given as a dictionary already containing some data # and it is returned as a dictionary with some of its entries updated such # that its value list is appended to include [ rank, rating ] # Part C def mostProfitableDirectors (movies, count): directors = {} for movieInfo in movies.values(): key = movieInfo[0] # director's name if len(movieInfo) == 10: value = float(movieInfo[9]) # profit else: value = float(movieInfo[7]) # rating or profit if value > 100: # not a rating if key not in directors: directors[key] = 0 directors[key] += int(value) return sorted([(v, k) for (k,v) in directors.items() ], reverse=True)[:count] # Part D def mostMoviesPerDecades (movies): movieDecades = {} for (title,year) in movies: key = year[:3] movieDecades[key] = movieDecades.get(key, 0) + 1 return sorted([ (count, decade+'0s') for (decade,count) in movieDecades.items() ], reverse=True) # ALTERNATE SOLUTION def mostMoviesPerDecades (movies): decades = [ k[1][:3]+'0s' for k in movies ] return sorted([ (decades.count(d), d) for d in set(decades) ], reverse=True)