The Inca and a couple of other South American cultures from the Andean region used quipus 1(also known as khipus or talking knots): recording devices consisting of knotted strings. The word khipu — meaning "knot" or "to knot" — comes from the Cusco-Colloa Quechua2 language (Peru). A quipu usually consisted of colored, spun, and plied thread or strings made from cotton or camelid3 fiber. For the Inca, the system aided in collecting data and keeping records, ranging from monitoring tax obligations, properly collecting census records, calendrical information, and military organization. Only a relatively small number have survived. A quipu could have only a few or up to 2,000 cords.
Quipus certainly have not yet revealed all of their secrets, but it is clear now that most recorded information consists of numbers in a decimal system. Marcia and Robert Ascher — after having analyzed several hundred quipus — succeeded in deciphering the code that allows to read the numbers.
One cord can record one or more numbers. These numbers are represented as a sequence of knot clusters to be read from the main cord: the cord to which all the other cords are attached (at the top of the figure below). Each cluster of knots is a digit, and there are three main types of knots: simple overhand knots4, figure-of-eight knots5 and long knots (consisting of an overhand knot with one or more additional turns).
For their analysis of quipus the Aschers introduced a simple notation for a cluster of knots (known as the Aschers' notation). They discovered that a natural number $$c_{n}c_{n - 1}c_{n - 2}\ldots c_{2}c_{1}c_{0}$$ — with $$c_i (i = 0, 1, \ldots, n)$$ the digits $$0, 1, \ldots, 9$$ in the decimal representation of the number — is recorded in the following way using knots. For all digits except the last one, a zero is represented by the absence of a knot in the appropriate position (represented as X) and all other digits are represented by a cluster of $$c_i$$ overhand knots (represented as $$c_i$$s). For the last digit a zero is represented by a figure-of-eight knot with an extra twist (represented as EE), a one by a figure-of-eight knot (represented as E; a long knot with a single turn is the same as an overhand knot and would not discriminate the last digit) and the other digits by a long knot with $$c_i$$ turns (represented as $$c_i$$L). The representations of the individual clusters of knots are separated by spaces in the Aschers' notation.
Because the ones digit (the last digit of a natural number) is represented in a distinctive way, it is clear where each number ends. One strand on a quipu can therefore contain several numbers. This is summarized in the diagram below.
For example, if 3s represents three overhand knots, 7L represents a long knot with seven turns, E represents a figure-of-eight knot and X represents a space:
the number 327 is represented by 3s 2s 7L
the number 2461 is represented by 2s 4s 6s E
the number 107 followed by the number 51 is represented by 1s X 7L 5s E
This reading can be confirmed by a fortunate fact: quipus regularly contain sums in a systematic way. For instance, a cord may contain the sum of the next $$n$$ cords, and this relationship is repeated throughout the quipu. Sometimes there are sums of sums as well. Such a relationship would be very improbable if the knots were incorrectly read. Your task:
Write a function quipu that takes a number $$n \in \mathbb{N}$$ (int). The function must return a string containing the knots representation of $$n$$ on a quipu in the Aschers' notation.
Write a function decimal2quipu that takes a sequence (list) of natural numbers (int). The function must return a string containing the knots representation of the given numbers on a quipu in the Aschers' notation.
Write a function quipu2decimal that takes a string
containing the knots representation of one or more natural numbers on
a quipu in the Aschers' notation. The function must return the
sequence (list) of numbers (int). In case
the argument passed to the function is not a string containing a valid
knots representation of one or more natural numbers on a quipu
in the Ascher's notation, an AssertionError must be
raised with the message invalid knots.
>>> quipu(327)
'3s 2s 7L'
>>> quipu(690)
'6s 9s EE'
>>> quipu(2461)
'2s 4s 6s E'
>>> quipu(107)
'1s X 7L'
>>> quipu(51)
'5s E'
>>> decimal2quipu([327, 690, 2461])
'3s 2s 7L 6s 9s EE 2s 4s 6s E'
>>> decimal2quipu([107, 51])
'1s X 7L 5s E'
>>> decimal2quipu([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
'EE E 2L 3L 4L 5L 6L 7L 8L 9L 1s EE 1s E 1s 2L'
>>> quipu2decimal('7s 3s E 6s 9s EE 8s X 4L')
[731, 690, 804]
>>> quipu2decimal('1s X 7L 5s E')
[107, 51]
>>> quipu2decimal('EE E 2L 3L 4L 5L 6L 7L 8L 9L 1s EE 1s E 1s 2L')
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> quipu2decimal('5s 17s 4L')
Traceback (most recent call last):
AssertionError: invalid knots
>>> quipu2decimal('2s X E 4s 9s')
Traceback (most recent call last):
AssertionError: invalid knots
>>> quipu2decimal('4s 1L')
Traceback (most recent call last):
AssertionError: invalid knots