# 1A 'DUKE BLUE' ['d', 'ke bl', 'e'] 6 3 ['b', 'd'] ['a'] [0, 2, 4, 6] ['fried', 'eggs'] ['a', 'b', ['c']] ['a', 'b', ['c', 'd']] ['c', 'd'] ['c', 'd', 'e'] [0, 2, 7] [5, 8, 4] [0, 6, 1] 4 3 True False # 1B [1, 2, 3] [3, 4, 3] ['azure', 'tomato', 'sangria'] # 2A call: encrypt('anchor') return value: 'anchor-way' # 2B call: encrypt('brr') return value: None correct value: 'brr-way' # 2C def encrypt(word): if word[0].lower() in 'aeiou': return word + '-way' elif word[:2].lower() == 'qu': return word[2:] + '-' + word[:2] + 'ay' else: for i in range(1, len(word)): if word[i].lower() in 'aeiouy': return word[i:] + '-' + word[:i] + 'ay' return word + '-way' # Added line # 3A rarewords = [k for k,v in words.items() if v < 10] rarewords = [x for x in words if words[x] < 10] # 3B ['towns', 'tree'] # Order does not matter # 3C d = {} for k,v in words.items(): n = len(k) if n not in d: d[n] = [] d[n].append(v) avg = {} for k,v in d.items(): avg[k] = sum(v)/len(v) # 4A def getHour(time): """ The parameter time is of type string. Return the hour between 0 and 23 of the day represented in time. """ hour = int(time.split(':')[0]) if hour == 12 and time[-1] == 'a': hour = 0 if hour < 12 and time[-1] == 'p': hour += 12 return hour # 4B def getData(filename): """ The parameter filename is of type string. Returns a list of dictionaries based on the csv file specified by filename. """ f = open(filename) freader = csv.reader(f) data = [] for row in freader: d = {} d['giver'] = row[0] d['receiver'] = row[1] d['amount'] = float(row[2]) d['date'] = row[3] d['time'] = row[4] data.append(d) return data # 4C def mostExpensiveHour(data): """ The parameter data is a list of dictionaries as returned by getData. Return the hour of the day that had the most money transferred through it based on the parameter data. """ d = {} for datum in data: hour = getHour(datum['time']) if hour not in d: d[hour] = datum['amount'] else: d[hour] += datum['amount'] return max(d.items(), key=lambda x: (x[1], x[0]))[0]