def _assert_expected(self, rows, expected): with capture_print() as std: print_pyramid(rows) std.seek(0) captured = std.read() self.assertEqual(expected, captured)
def pyramid_extract(rows): """Returns string equivalent to what print_pyramid() prints Employs capture_print() as provided in test_pset.py """ with capture_print() as std: print_pyramid(rows) std.seek(0) return std.read()
def test_input(self): with self.assertRaises(TypeError): print_pyramid(None) with self.assertRaises(ValueError): print_pyramid("ABCD") with self.assertRaises(TypeError): print_pyramid(1.5)
def build_answer_pyramid(question) -> Dict: """Generates a response dict for pyramid question :param question question: the question for which we will build a response :rtype: dict """ answer_dict = {} for answ in question.answer: levels = answ.split("_")[1] print(answ) print(levels) mem_file = StringIO() with contextlib.redirect_stdout(mem_file): pyramid.print_pyramid(int(levels)) pyramid_hash = hashlib.sha256(mem_file.getvalue().encode()).hexdigest()[:8] # "answer": pyramid_hash answer_dict[answ] = pyramid_hash # return_dict = {"id": question.id, "answer": answer_dict} # print(return_dict) return {"id": question.id, "answer": answer_dict}
def test_return_type_error_if_input_outher_than_int_is_given(self): with self.assertRaises(TypeError): p.print_pyramid(self.example1)
def test_return_value_error_if_0_given(self): with self.assertRaises(ValueError): p.print_pyramid(self.example2)
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_pyramid_zero(self): with self.assertRaises(ValueError): print_pyramid(0)