def test_rand(self):
        from random import randint
        randlong = lambda: sum(
            randint(1, 10000000000000000) * 10**randint(1, 42)
            for i29741 in range(randint(29, 42))) + randint(0, 999)
        for i3192471204 in range(9):
            a = randlong()
            b = 0
            self.assertEqual(last_digit(a, b), 1, "x ** 0 must return 1")

        for i3192471204 in range(42):
            a, b = randlong(), randlong()
            self.assertEqual(last_digit(a, b), (a % 10)**(b % 4 + 4) % 10,
                             "Testing %d and %d" % (a, b))
        def _random_test(value_min, value_max, repeat=20):
            MOD = 100

            def clamp(number):
                return number % MOD + MOD if number > MOD else number

            def powmod(base, exp):
                base, exp = clamp(base), clamp(exp)
                return clamp(base**exp)

            def _solution(lst):
                lst = lst[:] + [1]
                while len(lst) > 1:
                    exp, base = lst.pop(), lst.pop()
                    lst.append(powmod(base, exp))
                return lst[0] % 10

            for _ in range(repeat):
                test_input = []
                for i in range(10):
                    self.assertEqual(
                        len(test_input) != i, False,
                        'Do not mutate the input array!')
                    test_input.append(randint(value_min, value_max))
                    test_input1 = test_input[:]
                    test_input2 = test_input[:]
                    self.assertEqual(
                        last_digit(test_input1), _solution(test_input2),
                        'Wrong solution for [{}]'.format(', '.join(
                            str(n) for n in test_input)))
 def test(self):
     self.assertEqual(last_digit(4, 1), 4)
     self.assertEqual(last_digit(4, 2), 6)
     self.assertEqual(last_digit(9, 7), 9)
     self.assertEqual(last_digit(10, 1000000000), 0)
     self.assertEqual(
         last_digit(
             38710248912497124917933333333284108412048102948908149081409204712406,
             226628148126342643123641923461846128214626), 6)
     self.assertEqual(
         last_digit(
             3715290469715693021198967285016729344580685479654510946723,
             68819615221552997273737174557165657483427362207517952651), 7)
 def test(self):
     test_data = [([], 1), ([0, 0], 1), ([0, 0, 0], 0), ([1, 2], 1),
                  ([12, 18], 4), ([8, 21], 8), ([3, 3, 1], 7),
                  ([3, 3, 2], 3), ([3, 5, 3], 3), ([3, 4, 5], 1),
                  ([4, 3, 6], 4), ([7, 6, 1], 9), ([7, 6, 2], 1),
                  ([7, 6, 21], 1), ([7, 11, 2], 7), ([12, 30, 21], 6),
                  ([2, 0, 1], 1), ([2, 2, 2, 0], 4), ([2, 2, 101, 2], 6),
                  ([4, 0], 1), ([3, 0, 0], 3), ([2, 2, 1], 4),
                  ([2, 2, 1, 2], 4), ([3, 3, 0, 0], 7), ([3, 4, 0], 3),
                  ([3, 2, 1, 4, 4], 9), ([5, 0], 1), ([2, 3, 2], 2),
                  ([0, 0, 0, 0, 0, 0, 0], 0), ([82242, 254719, 736371], 8),
                  ([937640, 767456, 981242], 0),
                  ([123232, 694022, 140249], 6),
                  ([499942, 898102, 846073], 6), ([837142, 918895,
                                                   51096], 2),
                  ([625703, 43898, 614961, 448629], 1),
                  ([2147483647, 2147483647, 2147483647, 2147483647], 3)]
     for test_input, test_output in test_data:
         self.assertEqual(last_digit(test_input), test_output)
Exemplo n.º 5
0
def evaluate():
    data = request.get_json()
    logging.info("data sent for evaluation {}".format(data))
    n = int(data.get("n"))
    p = int(data.get("p"))
    if (n < 0):
        n = n * -1
    elif (n == 0):
        result = [0, 1, 0]
        return jsonify(result=result)
    print(p)
    digits = p * math.log10(n)
    remainder1 = digits % 1
    print(remainder1)
    remainder2 = remainder1 % 1
    first = 10**remainder2
    first_digit = first // 1
    last_dig = last_digit(n, p)
    digits = digits + 1

    result = [int(first_digit), int(digits), int(last_dig)]
    logging.info("My result :{}".format(result))
    return jsonify(result=result)