import random
import time

BASE = 3


class RatioInt(list):

    def __init__(self, value=[], base=10):
        assert all(
            val < base for val in value), 'Elements must be less than base. Base = %s' % base

        super(RatioInt, self).__init__(reversed(value))
        self.base = base

    def __add__(self, other):
        out = [0 for _ in range(max(len(self), len(other)))]
        i = 0
        c = 0

        while i < len(out):
            temp = self[i] + other[i] + c
            out[i] = (temp % self.base)
            c = temp // self.base

            i += 1

        if c:
            out.append(c)

        return RatioInt(out, self.base)

    def __mod__(self, val):
        '''
        Возвращает val-последних символов.
        '''
        return self[-val:]

    def __getitem__(self, key):
        try:
            return super(RatioInt, self).__getitem__(key)
        except IndexError:
            return 0

    def __str__(self):
        return ''.join(str(elem) for elem in self) + '(%s)' % self.base


def timing(func):
    def _f(*args, **kwargs):
        start = time.time()
        out = func(*args, **kwargs)
        print('It took %2.6ss' % (time.time() - start))
        return out

    return _f


@timing
def test_ariphmetic():
    for _ in range(10 ** 5):
        base = random.randint(2, 10)
        a = RatioInt([random.randint(0, base - 1) for _ in range(3)], base)
        b = RatioInt([random.randint(0, base - 1) for _ in range(3)], base)
        a += b
        # print('%s + %s = %s' % (a, b, a + b))


if __name__ == '__main__':
    test_ariphmetic()
