return helper1(A[1:], B[1:], a) # dot_product(A, B) returns the sum of the products of the number in 2 lists # with the same index # dot_product: (listof Float) (listof Float) -> Float # requires: len(A) == len(B) # Examples: dot_product([-1.0, 2.0], [2.0, 1.0]) => 0.0 def dot_product(A, B): return helper1(A, B, 0) # Tests: check.within('Q1aT1', dot_product([0.0], [0.0]), 0.0, 0.001) check.within('Q1aT2', dot_product([0.5, 2.0], [1.0, 0.0]), 0.5, 0.001) # helper2(L, b) returns a list of poitive integers # helper2: (listof Int) (listof Int) -> (listof Nat) # effects: non_positive digits are eliminated # Examples: helper2([1, 2, 6], []) => [1, 2, 6] def helper2(L, b): if L == []: return b elif L[0] > 0: b = b + [L[0]] return helper2(L[1:], b) else:
return 0.0 return A[0]*B[0] + dot_product(A[1:], B[1:]) ''' def dot_product(A, B): return dot_product_acc(A, B, 0.0) # Tests: # Test1: A and B == [] check.expect('Q1aT1', dot_product([],[]), 0.0) # Test2: Regular Tests check.expect('Q1aT2', dot_product([-1.0, 2.0],[2.0,1.0]), 0.0) check.expect('Q1aT3', dot_product([1.0,2.0,3.0], [1.0,1.0,1.0]), 6.0) check.expect('Q1aT4', dot_product([1.0,6.0,9.0,10.0],[0.0,1.0,2.0,3.0]), 54.0) # Test3: check.expect('Q1aT5', dot_product([-1.5, 2.5],[2.0,1.0]), -0.5) check.within('Q1aT6', dot_product([1.5,2.0,3.0], [1.0,1.8,-1.0]), 0.00001, 2.1) check.expect('Q1aT7', dot_product([1.1,2.0],[2.0,-2.1]), -2.0) # Question 1 - Part b # keep_positive_acc(lst, list_is_required) returns an list of positive Int, # list_is_required and gets rid of the other negative element in L # keep_positive_acc: (listof Int) (listof Int) -> (listof Nat) # Examples: # keep_positive_acc([1, 2],[]) => [1, 2] # keep_positive_acc([3, -1, 2, 0, 1],[]) => [3, 2, 1] def keep_positive_acc(lst, list_is_required): if lst == []: return list_is_required else: if lst[0] > 0: list_is_required.append(lst[0])
self.x *= k self.y *= k # Test(q3b): __add__ check.expect('T1(3b1)', Vector(2, 3) + Vector(4, -1), Vector(6, 2)) check.expect('T2(3b1)', Vector(0, 0) + Vector(0, 0), Vector(0, 0)) check.expect('T3(3b1)', Vector(1, 0) + Vector(1, -1), Vector(2, -1)) check.expect('T4(3b1)', Vector(-3, -3) + Vector(-9, -9), Vector(-12, -12)) # Test(q3b): __mul__ check.expect('T1(3b2)', Vector(2, 3) * Vector(4, -1), 5) check.expect('T2(3b2)', Vector(0, 0) * Vector(0, 0), 0) check.expect('T3(3b2)', Vector(1, 0) * Vector(1, -1), 1) check.expect('T4(3b2)', Vector(-3, -3) * Vector(-9, -9), 54) # Test(q3b): self.length() check.within('T1(3b3)', Vector(2, 3).length(), 3.6055, 0.0001) check.within('T2(3b3)', Vector(4, -1).length(), 4.1231, 0.0001) check.within('T3(3b3)', Vector(0, 0).length(), 0.0, 0.0001) check.within('T4(3b3)', Vector(1, 0).length(), 1.0, 0.0001) check.within('T5(3b3)', Vector(1, -1).length(), 1.41421, 0.0001) check.within('T6(3b3)', Vector(-3, -3).length(), 4.24264, 0.0001) check.within('T7(3b3)', Vector(-3, 5).length(), 5.83095, 0.0001) # Test(q3b): self.scale(k) u = Vector(2, 3) check.expect('T1(3b4)', u.scale(-2), None) check.expect('T1(3b4)(x)', u.x, -4) check.expect('T1(3b4)(y)', u.y, -6) a = Vector(-1, 3) check.expect('T2(3b4)', a.scale(-1), None)
## Tests for B check.expect("Test1", Vector(1, 1) + Vector(2, 1), Vector(3, 2)) check.expect("Test2", Vector(0, 0) + Vector(2, 1), Vector(2, 1)) check.expect("Test3", Vector(1, 2) + Vector(0, 0), Vector(1, 2)) check.expect("Test4", Vector(0, 0) + Vector(0, 0), Vector(0, 0)) check.expect("Test5", Vector(-3, -3) + Vector(2, 1), Vector(-1, -2)) check.expect("Test1", Vector(1, 1) * Vector(2, 1), 3) check.expect("Test2", Vector(0, 0) * Vector(2, 1), 0) check.expect("Test3", Vector(1, 2) * Vector(0, 0), 0) check.expect("Test4", Vector(0, 0) * Vector(0, 0), 0) check.expect("Test5", Vector(-3, -3) * Vector(2, 1), -9) check.within("Test1", Vector(1, 2).length(), 2.23, 0.1) check.within("Test2", Vector(-2, 4).length(), 4.47, 0.1) check.within("Test3", Vector(-3, -1).length(), 3.16, 0.1) check.within("Test4", Vector(2, 2).length(), 2.83, 0.1) c = Vector(1, 2) check.expect("Test1", c.scale(2), None) check.expect("Test1", c, Vector(2, 4)) d = Vector(3, 4) check.expect("Test2", d.scale(0), None) check.expect("Test2", d, Vector(0, 0)) d = Vector(1, 0) check.expect("Test3", d.scale(2), None) check.expect("Test3", d, Vector(2, 0))
return cost * (1 + GST) if prov_abbr == "SK": return (cost * (1 + GST + SK)) if prov_abbr == "BC": return cost * (1 + GST + BC) if prov_abbr == "MB" or prov_abbr == "ON": return cost * (1 + GST + MB_ON) if (prov_abbr == "NB" or prov_abbr == "NL" or prov_abbr == "NS" or prov_abbr == "PE"): return cost * (1 + GST + NB_NL_NS_PE) else: return cost * (1 + GST) ## Test1: Literature related goods check.within("Q2T1", after_tax(20.0, "ON", "literature"), 21.0, 0.01) ## Test2: Child related goods check.within("Q2T2", after_tax(25.0, "AB", "child"), 26.25, 0.01) ## Test3: Food related check.within("Q2T3", after_tax(12.25, "NS", "food"), 12.25, 0.01) ## Test4: Goods in SK check.within("Q2T4", after_tax(50100, "SK", "car"), 55611.00, 0.01) ## Test5: Goods in BC check.within("Q2T5", after_tax(1000000, "BC", "house"), 1120000.00, 0.01) ## Test6: Goods in MB or ON check.within("Q2T6", after_tax(1379, "ON", "gucci"), 1158.27, 0.01) check.within("Q2T7", after_tax(9.99, "MB", "gym membership"), 11.28, 0.01) ## Test7: Goods in NB, NL, NS, PE check.within("Q2T8", after_tax(14.99, "NB", "movies"), 17.24, 0.01) check.within("Q2T9", after_tax(1.99, "NL", "toothpaste"), 2.29, 0.01) check.within("Q2T10", after_tax(999, "NS", "iphone"), 1148.85, 0.01)
# fib_rec(0) => 0 def fib_rec(n): def fib_acc(new_n,last_fib,prev_fib): if (new_n == n): return last_fib else: return fib_acc((1+new_n),(last_fib+prev_fib),last_fib) if (n == 0): return 0 else: return fib_acc(1,1,0) # Testing fib_rec: check.expect("Test 1", fib_rec(10), 55) check.expect("Test 2", fib_rec(65), 17167680177565) # fib_approx(n) consumes a natural number (n) and produces a float that is approximately the nth position of the fibonnachi sequence # fib_approx: Nat -> Approx # Example: fib_approx(6) => 8.0 # fib_approx(0) => 0 def fib_approx(n): phi = (1+(math.sqrt(5)))/2 fib_final = ((phi ** n) - ((-phi) ** (-n)))/math.sqrt(5) return fib_final # Testing fib_approx check.within("Test 3", fib_approx(10), 55.0, 0.0001) check.within("Test 3", fib_approx(15), 610.0, 0.0001)
import check ## fairy_distance: float float float float -> float ## Purpose: Consumes four floats d, the starting distance between the two ## unicorns (km), v1(km/hr), the velocity of the first unicorn, v2(km/hr), the ## velocity of the second unicorn and f(km/hr), the velocity of the fairy and ## produces the float distance travelled by the fairy when the two unicorns ## meet and stop for tea with the fairy. # Examples: fairy_distance(18.0, 2.5, 3.0, 15.0) => 49.090909 (approx) # fairy_distance(200.0,60.0,80.0,300.0) => 428.5714286 (approx) # fairy_distance(200.0,5.0,5.0,50.0) => 1000.0 # def fairy_distance(d, v1, v2, f): return(d / (v1 + v2))*f ## Testing: print "Testing fairy_distance" # Test 1: Example test check.within("Question 3 Test 1", fairy_distance(18.0,2.5,3.0,15.0), \ 49.09090909090909093, 0.000001) # Test 2: Fast fairy check.within("Question 3 Test 2", fairy_distance(200.0,60.0,80.0,300.0)\ , 428.5714286, 0.000001) # Test 3: Slow unicorns check.expect("Question 3 Test 3", fairy_distance(200.0,5.0,5.0,50.0),1000.0) # Test 4: Small distance, slow fairy check.within("Question 3 Test 4", fairy_distance(5.0,30.0,30.0,1.0)\ , 0.083333333, 0.000001)
windchill = 13.12 + 0.6125*temp - 11.37*wind**0.16 + 0.3965*temp*wind**0.16 vapour_pressure = 6.112* 10**(7.5*temp/(237.7+temp)) *(hum/100) humidex = temp + 5/9*(vapour_pressure -10) if temp >= 15: if humidex - temp >= 1: return humidex else: return temp else: if temp - windchill >= 1: return windchill else: return temp ## Test1: temp higher than 15, humidex minus temp higher than 1 check.within("Q2Test1", feels_like(16, 2, 99), 20.43398, 0.0001) ## Test2: temp higher than 15, humidex minus temp less than 1 check.within("Q2Test2", feels_like(16, 2, 50), 16, 0.0001) ## Test3: temp lower than 15, windchill minus temp higher than 1 check.within("Q2Test3", feels_like(-1, 4, 30), -2.18098, 0.0001) ## Test4: temp lower than 15, windchill minus temp less than 1 check.within("Q2Test4", feels_like(-1, 2, 50), -1, 0.0001) check.within("Q2Test5", feels_like(-9.5,1.9,91), -9.5, 0.0001) **** a02q3.py ***************************************************************** ##=============================================== ## Jiadong Mai (20557203) ## CS 116 Winter 2018 ## Assignment 02, Question 3 ##===============================================
def participation(correct, incorrect, unanswered, threshold, tutorials_attended): overall_percent = 5 total_points = (2 * correct) + incorrect total_questions = (correct + incorrect + unanswered) questions_counted = math.ceil(total_questions * (threshold / 100)) possible_points = 2 * questions_counted if questions_counted <= correct: return 5.0 if questions_counted <= (correct + incorrect): return min((((2 * correct + (questions_counted - correct)) / possible_points) \ * overall_percent) + tutorials_attended * 0.1, 5.0) else: return min((((2 * correct + incorrect) / possible_points) * overall_percent) \ + tutorials_attended * 0.1 ,5.0) # Tests for particitation: check.expect("Q2T1", participation(30, 0, 0, 100, 0), 5.0) check.within("Q2T2", participation(10, 15, 5, 80, 0), 3.541666, 0.0001) check.within("Q2T3", participation(10, 15, 5, 90, 0), 3.240740, 0.0001) check.within("Q2T4", participation(15, 5, 0, 90, 0), 4.583333, 0.0001) check.within("Q2T5", participation(20, 5, 5, 90, 5), 4.666666, 0.0001) check.expect("Q2T6", participation(42, 8, 0, 95, 0), 4.6875) check.expect("Q2T7", participation(42, 8, 0, 95, 3), 4.9875) check.expect("Q2T8", participation(42, 8, 0, 95, 4), 5.0) check.within("Q2T9", participation(22, 3, 4, 90, 0), 4.351851, 0.0001) check.expect("Q2T10", participation(22, 3, 4, 90, 12), 5.0) check.expect("Q2T11", participation(0, 0, 30, 21, 4), 0.4)
elif need - correct >= incorrect: earned = correct * 2 + incorrect * 1 else: earned = correct * 2 + need - correct * 1 return 100 * ((earned) / total) # Constant for tests master1 = ['A', 'A', 'B', 'C', 'A', 'D', 'E', 'A', 'D', 'B'] master2 = ['A', 'A', 'B', 'C', 'A'] student1 = ['A', 'A', 'B', 'C', 'A', 'D', 'E', 'A', 'D', 'B'] student2 = ['A', 'A', 'B', 'C', 'D', 'E', 'A', 'E', 'B', ' '] student3 = ['A', 'B', 'A', ' ', ' ', 'D', ' ', 'A', ' ', 'B'] student4 = ['E', 'E', ' ', ' ', ' '] student5 = ['A', ' ', ' ', 'C', ' '] # Tests1: contain only correct answers check.expect("Q1T1", clicker_grade(master1, student1), 100.0) # Tests2: contain correct and incorrect answers check.within("Q1T2", clicker_grade(master1, student2), 78.57143, 0.00001) # Tests3: contain all types of answers check.within("Q1T3", clicker_grade(master1, student3), 71.42857, 0.00001) # Tests4: contain only incorrect answers check.expect("Q1T4", clicker_grade(['A', 'A'], ['B', 'C']), 50.0) # Tests5: contain incorrect and unanswered answers check.within("Q1T5", clicker_grade(master2, student4), 33.3333, 0.0001) # Tests6: contain only unanswered answers check.expect("Q1T6", clicker_grade(['A', 'C'], [' ', ' ']), 0.0) # Tests7: contain only correct and unanswered answers check.within("Q1T7", clicker_grade(master2, student5), 66.6666, 0.0001)
cost_a = 5.00*30 ## cost of group b if group_b <= 30: cost_b = 8.00*group_b elif group_b > 30: cost_b = 8.00*30 ## cost of group c with tax if group_c <= 30: cost_c = 10.00*group_c*1.13 elif group_c > 30: cost_c = 10.00*30*1.13 ## cost of adults with tax and the total cost depending on the time if time == "Day": adult_cost = (members*12.00*0.6 + (adults-members)*12.00) *1.13 total_cost = cost_a + cost_b + cost_c + adult_cost return total_cost elif time == "After_3": adult_cost = adults*12.00*0.5*1.13 total_cost = (cost_a + cost_b + cost_c)*0.5 + adult_cost return total_cost # Tests check.within("Q2T1", trip_cost(0,0,0,0,0, "Day"), 0, 0.01) check.within("Q2T2", trip_cost(1,1,1,1,1, "Day"), 32.43, 0.01) check.within("Q2T3", trip_cost(1,1,1,1,1, "After_3"), 18.93, 0.01) check.within("Q2T4", trip_cost(40,55,29,33,13,"After_3"), 582.59, 0.01) check.within("Q2T5", trip_cost(20,15,29,35,05,"Day"), 995.18, 0.01) check.within("Q2T6", trip_cost(40,40,40,40,0, "Day"), 1271.40, 0.01) check.within("Q2T7", trip_cost(25,15,20,30,10,"Day"), 823.56, 0.01) check.within("Q2T8", trip_cost(10,10,10,10,5,"After_3"), 189.30, 0.01) check.within("Q2T9", trip_cost(50,50,50,50,25, "After_3"), 703.50, 0.01)
Examples: after_tax (20.0 , "ON", "literature") => 21.0 after_tax (20010.0 , "AB", "car") => 21010.5 ''' cost = cost + 0.05 * cost if item == "food" or item == "literature" or item == "child": return cost elif prov_abbr == "AB" or prov_abbr == "NT" or prov_abbr == "NU" or prov_abbr == "YT": return cost elif prov_abbr == "SK": return cost + 0.06 * cost elif prov_abbr == "BC": return cost + 0.07 * cost elif prov_abbr == "QU": return cost + 0.09975 * cost elif prov_abbr == "NB" or prov_abbr == "NL" or prov_abbr == "NS" or prov_abbr == "PE": return cost + 0.1 * cost elif prov_abbr == "ON" or prov_abbr == "MB": return cost + 0.08 * cost ##Tests check.within("no tax item", after_tax(20.0, "ON", "literature"), 21.0, 0.0001) check.within("Ex: AB tax", after_tax(20010.0, "AB", "car"), 21010.5, 0.0001) check.within("Ex: SK tax", after_tax(40.0, "SK", "Clothes"), 44.52, 0.0001) check.within("Ex: BC tax", after_tax(80.0, "BC", "Leather"), 89.88, 0.0001) check.within("Ex: QU tax", after_tax(5700.0, "QU", "Bike"), 6582.00375, 0.0001) check.within("Ex: NB tax", after_tax(1000.0, "NB", "Metal"), 1155.0, 0.0001) check.within("Ex: MB tax", after_tax(570.0, "MB", "Wood"), 646.38, 0.0001)
# Example: participation(3,2,2,50,12) => 5.0 # participation(4,3,0,100,0) => 3.92857 def participation(correct, incorrect, unanswered, threshold, tutorials_attended): all_qs = correct + incorrect + unanswered num_of_qs = math.ceil((threshold / 100) * all_qs) if incorrect < 0: return participation(correct + incorrect, 0, 0, threshold, tutorials_attended) elif unanswered < 0: return participation(correct, incorrect + unanswered, 0, threshold, tutorials_attended) elif num_of_qs < all_qs: return participation(correct, incorrect, unanswered - (all_qs - num_of_qs), 100, tutorials_attended) else: marks_made = (correct * 2) + incorrect marks_possible = all_qs * 2 points_deserved = ((marks_made / marks_possible) * 5.0) + (tutorials_attended * 0.1) if points_deserved > 5.0: return 5.0 else: return points_deserved # Testing participation: check.expect("Test 1", participation(1, 0, 0, 100, 12), 5.0) check.within("Test 2", participation(1, 3, 5, 100, 12), 2.5888, 0.0001) check.within("Test 3", participation(5, 3, 5, 75, 12), 4.45, 0.0001) check.within("Test 4", participation(0, 0, 5, 100, 12), 1.2, 0.0001) check.within("Test 5", participation(0, 0, 5, 1, 12), 1.2, 0.0001) check.within("Test 6", participation(1, 0, 0, 1, 12), 5.0, 0.0001)
import check import math # feels_like(temp, wind, hum) returns the temperature that you feels like # during the day in Canada, and the humidity and the wind chill factors are # considered # feels_like: Float Float Nat -> Float # requires: wind(wind_speed) >= 0 # 0 <= hum <= 100 # Example: feels_like(-9.5,1.9,91) => -9.5 def feels_like(temp, wind, hum): windchill = 13.12 + 0.6125 * temp - 11.37 * (wind ** 0.16) +0.3965 * temp * (wind ** 0.16) vapour_pressure = 6.112 * (10 ** ((7.5 * temp) / (237.7 + temp))) * (hum / 100) humidex = temp + (5 / 9) * (vapour_pressure - 10) if temp <= humidex + 1 and temp >= 15: feeling = humidex elif temp >= windchill + 1 and temp < 15: feeling = windchill else: feeling = temp return feeling # Tests: check.within("Q2T1", feels_like(-9.5, 1, 91), -9.5, 0.001) check.within("Q2T2", feels_like(20.1, 1.9, 12), 20.1, 0.001) check.within("Q2T3", feels_like(13.5, 100, 13), 8.817, 0.001) check.within("Q2T4", feels_like(13.5, 1, 13), 13.5, 0.001) check.within("Q2T5", feels_like(13.5, 19, 91), 11.750, 0.001)