# 1 words = ['cream', 'teal', 'eggplant', 'sky blue', 'pumpkin', 'peach', 'brown'] say = 'If you are happy and you know it, clap your hands' a = 5 5 b = words[2] 'eggplant' c = 'H' in say False d = 'blue' in words False e = words[6:7] ['brown'] f = 5 % 2 1 g = len(say.split('h')) 3 h = 35 / 10 3.5 i = words[2][3] 'p' j = words[-1] 'brown' k = 6 // 4 1 l = 'you' in say True m = (4 > 3) and (5 != 6) True n = 10.0 + 3.0 13.0 o = 'cream' in words True p = say[:2] 'If' # 2 print(mystery(food, ā€™eā€™)) beetroot print(mystery(food, ā€™aā€™)) alfalfa sprouts # 3 ## Part A discount(2, 3) Returns: 36 ## Part B discount(1,1) # Any arguments where they are equal Returns: None Should return: 13 ## Part C ### Solution 1 def discount(hats, scarves): if hats > scarves: return scarves*13 + (hats-scarves)*5 if scarves > hats: return hats*13 + (scarves-hats)*10 return hats*4 + scarves*9 # Add this line or hats*13 ### Solution 2 def discount(hats, scarves): if hats > scarves: return scarves*13 + (hats-scarves)*5 else: # made this an else return hats*13 + (scarves-hats)*10 ### Solution 3 def discount(hats, scarves): if hats == scarves: return hats*13 if hats > scarves: return scarves*13 + (hats-scarves)*5 if scarves > hats: return hats*13 + (scarves-hats)*10 # 4 ## Part A ''' There was a typo on the original exam, it should have been (d1**2 + d2**2), but the typo said (d1**2 * d2**2). The version on the website has been fixed ''' return 2 * math.sqrt(d1**2+d2**2) ## Part B s = (a+b+c)/2 return math.sqrt(s*(s-a)*(s-b)*(s-c)) # 5 def createFilteredList(lst, filter): newLst = [] for e in lst: if filter(e): newLst.append(e) return newLst # 6 ## Part A def startsWithH(word): if word[0] == 'h': return word[1:] else: return word ## Part B ### Solution 1 def hasAnL(word): if len(word) <= 2: return word newWord = word[0] for c in word[1:-1]: if c != 'l': newWord += c return newWord + word[-1] ### Solution 2 def hasAnL(word): newWord = '' for i in range(len(word)): if i == 0 or i == len(word)-1 or word[i] != 'l': newWord += word[i] return newWord ### Solution 3 def hasAnL(word): if len(word) <= 2: return word middle = word[1:-1].split('l') return word[0] + ''.join(middle) + word[-1] ## Part C def endsWithP(word): if word[-1] == 'p': # can also use .endswith('p') return word[:-1]+'t' elif word[-2:] == 'ps': return word[:-2]+'ts' else: return word ## Part D def toddlerSpeak(sentence): lst = [] for word in sentence.split(): newWord = startsWithH(word) if newWord == word: newWord = hasAnL(word) if newWord == word: newWord = endsWithP(word) lst.append(newWord) return ' '.join(lst)