def get_answers(questions: List[QuizSubmissionQuestion]) -> List[Dict]: """Creates answers for Canvas quiz questions""" answer_0 = { "fib_100000": last_8(optimized_fibonacci(100000)), "summable_5_7_11_100000": last_8(SummableSequence(5, 7, 11)(100000)), "summable_0_1_100000": last_8(SummableSequence(0, 1)(100000)), "fib_234202": last_8(optimized_fibonacci(234202)), "summable_8_9_99_141515": last_8(SummableSequence(8, 9, 99)(141515)), "summable_5_98_7_35_2_603": last_8(SummableSequence(5, 98, 7, 35, 2)(603)), } answer_1 = { "pyramid_24": hashlib.sha256(pyramid_extract(24).encode()).hexdigest()[:8], "pyramid_53": hashlib.sha256(pyramid_extract(53).encode()).hexdigest()[:8], } answer_2 = 8610 answers = [answer_0, answer_1, answer_2] results = [] for i in range(len(questions)): results.append({"id": questions[i].id, "answer": answers[i]}) return results
def test_summable(self): ss = SummableSequence(0, 1) for n in range(0, 50, 5): with timeout(message="Timeout running f({})".format(n)): self.assertEqual(ss(n), optimized_fibonacci(n)) # test on new seq new_seq = SummableSequence(5, 7, 11) for n, expected in [ # Check progressively more complex values, see if time out (0, 5), (1, 7), (2, 11), (3, 23), (4, 41), (5, 75), (20, 703209), (21, 1293403), (22, 2378939), (23, 4375551), (24, 8047893), (25, 14802383), (26, 27225827), (27, 50076103), (28, 92104313), (29, 169406243), ]: with timeout(message="Timeout running f({})".format(n)): self.assertEqual(expected, new_seq(n))
def test_summable_input(self): with self.assertRaises(TypeError): SummableSequence(None) with self.assertRaises(ValueError): SummableSequence("ABCD") with self.assertRaises(ValueError): SummableSequence(-1) with self.assertRaises(TypeError): SummableSequence(1.5)
def test_summable(self): for n, expected in self.test_scenario: with timeout(message="Timeout running f({})".format(n)): ss = SummableSequence(0, 1) self.assertEqual(ss(n), expected) for n, expected in [(0, 5), (1, 7), (2, 11), (3, 23), (4, 41)]: with timeout(message="Timeout running f({})".format(n)): ss = SummableSequence(5, 7, 11) self.assertEqual(ss(n), expected)
def test_summable_1(self): new_seq = SummableSequence(5, 7) actual_value = new_seq(10) n = 2 with timeout(message="Timeout running f({})".format(n)): expected_value = 55 self.assertEqual(expected_value, actual_value)
def test_summable(self): ss = SummableSequence(0, 1) for n in range(0, 50, 5): with timeout(message="Timeout running f({})".format(n)): raise NotImplementedError( "You should implement this and other SummableSequence tests!" )
def test_summable(self): ss = SummableSequence(0, 1, 2) for n, expected in [ (0, 0), (1, 1), (2, 2), (3, 3), (4, 6), (5, 11), (6, 20), (7, 37), (8, 68), ]: with timeout(message="Timeout running f({})".format(n)): self.assertEqual(ss(n), expected) ss = SummableSequence(0, 1, 2)
def test_summable(self): ss = SummableSequence((0, 1), n) for n, expected in fib_values: with timeout(message="Timeout running f({})".format(n)): self.assertEqual(expected, ss(n)) for n in range(0, 50, 5): with timeout(message="Timeout running f({})".format(n)): self.assertEqual(optimized_fibonacci(n), ss(n))
def test_summable(self): for n, expected in self.test_scenario: with timeout(message="Timeout running f({})".format(n)): ss = SummableSequence(0, 1) self.assertEqual(ss(n), expected) for n, expected in [ # Check progressively more complex values, see if time out (0, 5), (1, 7), (2, 11), (3, 23), (4, 41), ]: with timeout(message="Timeout running f({})".format(n)): ss = SummableSequence(5, 7, 11) self.assertEqual(ss(n), expected)
def test_summable(self): ss = SummableSequence(0, 1) for n in range(0, 50, 5): with timeout(message="Timeout running f({})".format(n)): # compare summable result to fibonacci result test = ss(n) truth = optimized_fibonacci(n) assert test == truth
def test_summable(self): ss = SummableSequence(0, 1) expected = [ # Check progressively more complex values, see if time out [0, 0], [5, 5], [10, 55] ] index = 0 for n in range(0, 10, 5): with timeout(message="Timeout running f({})".format(n)): actual_value = ss(n) expected_value = expected[index][1] self.assertEqual(expected_value, actual_value) index = index + 1
def test_summable(self): ss = SummableSequence(0, 1) for n, expected in [ # Check progressively more complex values, see if time out (6, 8), (10, 55), (15, 610), (20, 6765), (30, 832040), (40, 102334155), (100, 354224848179261915075), ]: with timeout(message="Timeout running f({})".format(n)): self.assertEqual(expected, ss(n))
def test_summable(self): ss = SummableSequence(0, 1, 1) for n, expected in [ # Check progressively more complex values, see if time out (0, 2), (5, 8), (10, 89), (15, 987), (20, 10946), (25, 121393), (30, 1346269), (35, 14930352), (40, 165580141), (45, 1836311903), ]: with timeout(message="Timeout running f({})".format(n)): self.assertEqual(expected, ss(n))
def test_summable(self): for n in range(0, 50, 5): for e, expected, in [ (0, 1), (5, 5), (10, 55), (15, 610), (20, 6765), (25, 75025), (30, 832040), (35, 9227465), (40, 102334155), (45, 1134903170), (50, 12586269025), ]: with timeout(message="Timeout running f({})".format(n)): ss = SummableSequence(0, 1) self.assertEqual(expected, ss(e))
def test_summable_smallest_2_back(self): new_seq = SummableSequence(0, 1) actual_value = new_seq(0) self.assertEqual(0, actual_value)
def test_summable(self): ss = SummableSequence(0, 1) for n in range(0, 50, 5): with timeout(message="Timeout running f({})".format(n)): self.assertEqual(ss(n), optimized_fibonacci(n))
def get_answers(questions: List[QuizSubmissionQuestion]) -> List[Dict]: """Creates answers for Canvas quiz questions""" # Formulate your answers - see docs for QuizSubmission.answer_submission_questions below # It should be a list of dicts, one per q, each with an 'id' and 'answer' field # The format of the 'answer' field depends on the question type # You are responsible for collating questions with the functions to call - do not hard code # submission list submission = [] # answering fib/summable questions answers = {} # optimised fibonacci questions answers["fib_100000"] = last_8(optimized_fibonacci(100000)) answers["fib_234202"] = last_8(optimized_fibonacci(234202)) # summable questions seq = SummableSequence(0, 1) answers["summable_0_1_100000"] = last_8(seq(100000)) seq = SummableSequence(5, 7, 11) answers["summable_5_7_11_100000"] = last_8(seq(100000)) seq = SummableSequence(5, 98, 7, 35, 2) answers["summable_5_98_7_35_2_603"] = last_8(seq(603)) seq = SummableSequence(8, 9, 99) answers["summable_8_9_99_141515"] = last_8(seq(141515)) tmp = {"id": questions[0].id, "answer": answers} submission.append(tmp) # answering pyramid questions answers = {} # anwser for pyramid_24 with capture_print() as std: print_pyramid(24) std.seek(0) output = std.read() output = hashlib.sha256(output.encode()).hexdigest() answers["pyramid_24"] = output[:8] # anwser for pyramid_53 with capture_print() as std: print_pyramid(53) std.seek(0) output = std.read() output = hashlib.sha256(output.encode()).hexdigest() answers["pyramid_53"] = output[:8] tmp = {"id": questions[1].id, "answer": answers} submission.append(tmp) # answering time question tmp = {"id": questions[2].id, "answer": 3268} submission.append(tmp) # eg {"id": questions[0].id, "answer": {key: some_func(key) for key in questions[0].answer.keys()}} return submission
def test_summable(self): ss = SummableSequence(0, 1) for n in range(0, 50, 5): with timeout(message="Timeout running f({})".format(n)): self.assertEqual(ss(5), 16) self.assertEqual(ss(10), 512)
def test_summable2(self): ss2 = SummableSequence(5, 7, 11) for n, expected in [(0, 0), (1, 5), (2, 7), (3, 11), (4, 41), (5, 75)]: self.assertEqual(expected, ss2(n))