Six hundred years ago, the people of the French Polynesian island of Mangareva1 developed a mixed-radix counting system that combined binary and decimal elements to count from 1 to 799.
The counting system does not support the representation of zero. The digits 1 through 9 have their normal decimal value. Digits K (takau), P (paua), T (tataua) and V (varu) have values 10, 20, 40 and 80, respectively, so they increase in a binary progression. A number $$n$$ is represented as \[ n = a_{80}\text{V} + a_{40}\text{T} + a_{20}\text{P} + a_{10}\text{K} + a_1 \] where the factors $$a_i\ (i \in \{1, 10, 20, 40, 80\})$$ are subject to the following conditions
$$a_1, a_{80} \in \{0, 1, 2, \ldots, 9\}$$
$$a_{10}, a_{20}, a_{40} \in \{0, 1\}$$
at least one factor is non-zero
terms having $$a_i = 0$$ are not represented
if $$a_i = 1$$ ($$i = 10, 20, 40$$) then the factor $$a_i$$ is not represented
Thus, the number 73 is represented in Mangarevan as the string TPK3, the number 219 is represented as the string 2VTK9, and the number 799 is represented as the string 9VTPK9. Research2 has shown that this counting system has interesting arithmetic, allowing quick mental calculation of operations such as addition, subtraction, multiplication and division: 1VPK9 + 1 = 1VT and 3VPK3 + 2VTK9 = 6VK2.
Define a class Mangarevan that can be used to represent Mangarevan numbers in Python. New objects of this class must either be initialized with a given integer $$i$$ (with $$1 \leq i \leq 799$$) or a given string $$s$$ that is formatted according to the description given in the introduction of this assignment. Other values passed upon initialisation should result in an AssertionError being raised with the message invalid value.
The conversion of a Mangarevan number to a string — using the built-in functions str() and repr() — always contains the string representation as described in the introduction of this assignment (use the example below to derive the difference in formatting of the results returned by both built-in functions) and the conversion to an integer — using the built-in function int() — returns the decimal value.
Make sure that the binary operators for addition (+), subtraction (-), multiplication (*), integer division (//), modulo (%) and exponentiation (**) each evaluate to a new Mangarevan number if both of their operands are Mangarevan numbers or if one operand is a Mangarevan number and the other is an integer. The result of these operations should have the same decimal value as the result obtained by applying these operators on two integer operands having the same decimal values. If the operations do not evaluate to a valid Mangarevan number, an AssertionError must be raised with the message invalid value.
>>> number = Mangarevan('TPK3')
>>> int(number)
73
>>> str(number)
'TPK3'
>>> number
Mangarevan('TPK3')
>>> number = Mangarevan(219)
>>> int(number)
219
>>> str(number)
'2VTK9'
>>> number
Mangarevan('2VTK9')
>>> Mangarevan(42) + Mangarevan(16)
Mangarevan('TK8')
>>> Mangarevan(42) - Mangarevan(16)
Mangarevan('P6')
>>> Mangarevan(42) * Mangarevan(16)
Mangarevan('8VPK2')
>>> Mangarevan(42) // Mangarevan(16)
Mangarevan('2')
>>> Mangarevan(42) % Mangarevan(16)
Mangarevan('K')
>>> Mangarevan(12) ** Mangarevan(2)
Mangarevan('1VTP4')
>>> Mangarevan(42) + 16
Mangarevan('TK8')
>>> Mangarevan(42) - 16
Mangarevan('P6')
>>> Mangarevan(42) * 16
Mangarevan('8VPK2')
>>> Mangarevan(42) // 16
Mangarevan('2')
>>> Mangarevan(42) % 16
Mangarevan('K')
>>> Mangarevan(12) ** 2
Mangarevan('1VTP4')
>>> 42 + Mangarevan(16)
Mangarevan('TK8')
>>> 42 - Mangarevan(16)
Mangarevan('P6')
>>> 42 * Mangarevan(16)
Mangarevan('8VPK2')
>>> 42 // Mangarevan(16)
Mangarevan('2')
>>> 42 % Mangarevan(16)
Mangarevan('K')
>>> 12 ** Mangarevan(2)
Mangarevan('1VTP4')
>>> Mangarevan('C3P0')
Traceback (most recent call last):
AssertionError: invalid value
>>> Mangarevan(1234)
Traceback (most recent call last):
AssertionError: invalid value
>>> Mangarevan({1, 2, 3})
Traceback (most recent call last):
AssertionError: invalid value
Ball P (2013). Polynesian people used binary numbers 600 years ago. Nature. 3
Bender A (2013). Two accounts of traditional Mangarevan counting… and how to evaluate them. The Journal of the Polynesian Society, 275-287. 4
Bender A, Beller S (2014). Mangarevan invention of binary steps for easier calculation. PNAS 111(4), 1322-1327. 5