The Inca and a couple of other South American cultures from the Andean region used quipus (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 Quechua language (Peru). A quipu usually consisted of colored, spun, and plied thread or strings made from cotton or camelid 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.

quipu

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 knots, figure-of-eight knots and long knots (consisting of an overhand knot with one or more additional turns).

knot notation

Assignment

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.

decimal to quipu

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:

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:

Example

>>> 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

Resources