コード例 #1
0
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
コード例 #2
0
    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))
コード例 #3
0
 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))
コード例 #4
0
ファイル: test_pset.py プロジェクト: JarvisEQ/pset0
    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
コード例 #5
0
 def test_fibonacci_integer(self):
     with self.assertRaises(TypeError):
         optimized_fibonacci(None)
     with self.assertRaises(ValueError):
         optimized_fibonacci("ABCD")
     with self.assertRaises(TypeError):
         optimized_fibonacci(1.5)
コード例 #6
0
ファイル: test_pset.py プロジェクト: AjayKayMaster/p0
 def test_fibonnacci(self):
     for n, expected in [
             # Check progressively more complex values, see if time out
         (0, 0),
         (1, 1),
         (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, optimized_fibonacci(n))
コード例 #7
0
def build_answer_sequence(question) -> Dict:
    """Generates a response dict for fib & sequence question

    :param question question: the question for which we will build a response
    :rtype: dict
    """
    answer_dict = {}
    for answ in question.answer:
        components = answ.split("_")
        op_type = components.pop(0)
        loops = int(components.pop())
        starting_seq = tuple(map(int, components))

        if op_type == "summable":
            new_seq = fibonacci.SummableSequence(*starting_seq)
            # print("new_seq(100000)[-8:]:", fibonacci.last_8(new_seq(loops)))  # 60500327
            answer_dict[answ] = fibonacci.last_8(new_seq(loops))
        elif op_type == "fib":
            # print(fibonacci.last_8(fibonacci.optimized_fibonacci(loops)))
            answer_dict[answ] = fibonacci.last_8(fibonacci.optimized_fibonacci(loops))
    # return_dict = {"id": question.id, "answer": answer_dict}
    # print(return_dict)
    return {"id": question.id, "answer": answer_dict}
コード例 #8
0
 def test_fibonnacci(self):
     for n, expected in fib_values:  # Check progressively more complex values, see if time out
         with timeout(message="Timeout running f({})".format(n)):
             self.assertEqual(expected, optimized_fibonacci(n))
コード例 #9
0
 def test_fibonnacci(self):
     for n, expected in self.test_scenario:
         with timeout(message="Timeout running f({})".format(n)):
             self.assertEqual(optimized_fibonacci(n), expected)
コード例 #10
0
ファイル: submit.py プロジェクト: JarvisEQ/pset0
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
コード例 #11
0
 def test_fibonacci_index(self):
     with self.assertRaises(ValueError):
         optimized_fibonacci(-1)
コード例 #12
0
 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))