The Jupiter-C1 was an American research and development vehicle developed from the Jupiter-A2. The Jupiter-C was used for three sub-orbital spaceflights3 in 1956 and 1957 to test re-entry nosecones4 that were later to be deployed on the more advanced PGM-19 Jupiter5 mobile missile. A member of the Redstone rocket family6, Jupiter-C was designed by the U.S. Army Ballistic Missile Agency7 (ABMA), under the direction of Wernher von Braun8.

Jupiter-C
Jupiter-C on the launch pad at Cape Canaveral.

The Jupiter-C was part of the IRBM9 project, and the sequence of manufacture of the rockets was considered a military secret. So the designation painted on the sides of the rocket was not a serial number in clear text, but employed a simple transformation cypher that the staff would be sure not to forget. The key was taken from the name of the design and test base: Huntsville, Alabama10.

transformation cypher
Transformation cypher used for encoding serial numbers on the Jupiter-C rockets.

By discarding all duplicate letters and appending the letter X, a ten-letter key was obtained: HUNTSVILEX. This resulted in a transformation cypher where the letter H was used for the digit 1, the letter U for the digit 2, …, the letter E for the digit 9 and the letter X for the digit 0. For example, the Jupiter-C modified to launch Explorer 111 had the encoded serial number UE painted on the side, indicating it was serial number 29 (U → 2, E → 9). The next version of the Jupiter-C had NX painted on its side, the encoded version of the serial number 30 (N → 3, X → 0).

Assignment

A serial number is a natural number (int) and a key is a string (str) that only contains letters (both uppercase and lowercase letters are allowed).

Consider a transformation cypher for serial numbers that works with a given key. First, the key is reduced by only retaining the first occurrence of each letter, where no distinction is made between uppercase and lowercase letters. This way, the key TRICHINOPHOBIA is for example reduced to TRICHNOPBA. This reduced key must contain ten letters, where the first letter corresponds to the digit 1, the second letter to the digit 2, …, the penultimate letter to the digit 9 and the last letter to the digit 0. Your task:

None of these functions may make a distinction between uppercase and lowercase letters in the keys and encoded serial numbers that are passed as arguments.

If the functions encode, decode or next take a key whose reduced version does not exist of ten letters, an AssertionError must be raised with the message invalid key.

Example

>>> reduce('HUNTSVILLEX')
'HUNTSVILEX'
>>> reduce('TRICHINOPHOBIA')
'TRICHNOPBA'

>>> encode(29, 'HUNTSVILLEX')
'UE'
>>> encode(63, 'TRICHINOPHOBIA')
'NI'

>>> decode('UE', 'HUNTSVILLEX')
29
>>> decode('NI', 'TRICHINOPHOBIA')
63

>>> next('UE', 'HUNTSVILLEX')
'NX'
>>> next('NI', 'TRICHINOPHOBIA')
'NC'

Resources