Solutions to Test 1 Practice CompSci 101 Spring 2013 Feb 13, 2013 Problem 1: 27/15, int, 1 32%11, int, 10 sum(range(6)), int, 15 len("dog") < len("catsup"), boolean, true "1,234,567".split(",") list, ["1", "234", "567"] "platform"[-2], string, "r" "beat"+"nick", string, "beatnick" "snapdragon"[3:8] string, "pdrag" 0.23*10, float 2.3 [8,7,6,7,6,5,4,3,2,1][5:7] list [5,4] 2**4 int 16 Part B: def rorhrer(weight,height): return 1.0*weight*2768/(height**3) Part C: def triangle_area(a,b,c): s = (a+b+c)/2.0 area = math.sqrt(s*(s-a)*(s-b)*(s-c)) return area Problem 2: Part A def divisor_sum(n): return sum([k for k in range(1,n) if n % k == 0]) or s = 0 for k in range(n): if n % k == 0: s = s + k return s PART B: def is_amicable(x,y): return divisor_sum(x) == y and divisor_sum(y) == x PART C: def nearby(city,clist,apart): ret = [] for elt in clist: if distance(elt[1],elt[2],city[1],city[2]) <= apart: ret.append(elt[0]) return ret OR return [elt[0] for elt in clist if distance(elt[1],elt[2],city[1],city[2]) <= apart] ******* Problem 3 Part A The one test that doesn't pass is the third, when "big" is b then the expression b in ["bad","news", "is", "trouble"] evaluates to true so "clean" is returned, the word "bad" is never checked. Move the else: outside the loop, e.g., def censored(bad,headline): words = headline.split() for b in bad: if b in bad: return "censored" return "clean" ****** Part C: index+2 skips the comma and space to get the first name the slice uses index to get everything up to and NOT including the comma Part D: .find() returns -1 when the string isn't found, so -1+2 = 1 and the slice gets "ob jones". The value of the slice "bob jones"[:-1] is everything upto and not include the last character or "bob jone" ****** Problem 4 Part B B. def howManyInRange(values, start, end): agelist = getAges(values) count = 0 for age in agelist: if start <= age <= end: count += 1 return count ALTERNATE SOLUTION: def howManyInRange(values, start, end): agelist = getAges(values) ages = [] for age in agelist: if start <= age <= end: ages.append(age) return len(ages) ALTERNATE SOLUTION: def howManyInRange(values, start, end): values = getAges(values) return sum([1 for num in values if num >= start and num <= end]) ALTERNATE SOLUTION: def howManyInRange(values, start, end): return len([num for num in getAges(values) if num >= start and num <= end]) ******* Problem 5 5. def convertWord(word): if word.lower() == "hello": return "Ahoy" pos = word.find("ar") if (pos > 0): return word[:pos] + 'arrr' + word[pos+2:] if (len(word)> 7) and pos == -1: ans = "" for ch in word: if ch != 'o' and ch != 'u': ans += ch return ans return word ALTERNATE SOLUTION: def convertWord(word): if word == "hello" or word == 'Hello': return "Ahoy" if word.startsWith("ar") return word pos = word.find("ar") if (pos > 0): return word[:pos+2] + 'rr' + word[pos+2:] if (len(word)> 7): word = word.replace("o","") word = word.replace("u","") return word return word ****** Problem 6 Part A def isPerfect(num): sum = 0 for div in range(1,num): if num % div == 0: sum += div return sum == num alternatively, replace the last line/return statement with if sum == num: return True return False ---- Alternatively use a list comprehension def isPerfect(num): return sum([div for div in range(1,num) if num % div == 0]) == num --- PART B def combine(phrase): st = "" for w in phrase: st += w[:firstVowelIndex(w)+2] return st --- ******** Problem 7 Part A def isSquareFree(num): for x in range(1,num): sq = x*x if num % sq == 0: return False return True Part B def wordCount(words,sub): """ return the number of strings in string list words that contain string sub """ c = 0 for w in words: if sub in w: c = c + 1 return c -- or def wordCount(words,sub): return sum([1 for w in words if w.find(sub) >= 0]) ****** Problem 8 Question 1: 'dog', no ie and no ei found Question 2: line 8 since eii isn't 0 and 'c' occurs before the 'ei' Question 3: line 6 for similar reasons Question 4: control falls through to the end since all the if expressions are false so False is returned as no other return statements execute Question 5: Start code with: if iei == 0: return False if eii == 0: return True