In 1984, German painter and artist Ralf Krempel obtained a patent for encoding messages as color codes. Each character is encoded as a unique sequence of one or more colors. According to the following table, uppercase letter C is encoded as color code yellow-blue, lowercase letter o as color code blue, and lowercase letter s as color code red-yellow.

color codes

To encode a plaintext, join the color codes for its characters, separated by a black color. Using the above color codes, plaintext Cosmopolitan is encoded into this ciphertext.

ciphertext (uppercase letters and asterisks)

A modern version of the Krempel code represents each color in the ciphertext by an emoji of that color. As there are multiple emojis of the same color, each color can be represented by different emojis.

emoji

If we represent each color in the ciphertext for Cosmopolitan with a randomly chosen emoji of that color, we may get this ciphertext.

ciphertext (emojis)

As each color is represented by a randomly chosen emoji of that color, this could be an alternative ciphertext for Cosmopolitan.

ciphertext (emojis)

We simply reverse the process to decode a ciphertext. If the ciphertext is represented by emojis, we first determine the color for each emoji. Then we determine the corresponding character for each color code — with color codes separated from each other by a black color. The plaintext is obtained by concatenating these characters.

Assignment

To encode and decode messages according to a Krempel code, we represent each color in the ciphertext by a unique uppercase letter (str). The color black is always represented by an asterisk (*; str).

The color codes for a Krempel code are defined in a text file. Each line of the file consists of a character that can occur in a plaintext, followed by one or more uppercase letters that represent the color code for the character in a ciphertext. For example, these are the first few lines of the file with color codes for the Krempel code we used in the introduction (krempel.txt):

AYG
aGB
BGR
CYB
iGP
JRR
KY
LYP
lP
mRB
nYR
OG
…

Using these color codes, the ciphertext (str) for Cosmopolitan is

YB*B*RY*RB*B*YY*B*P*GP*GY*GB*YR

We can also represent a color using an emoji (str) from the UTF-8 character encoding, where emojis are a single character. Multiple emojis could represent the same color, unlike uppercase letters and the asterisk that always represent a color in a unique way.

The colors of emojis for a Krempel code are defined in a text file. Each line of the file consists of an emoji and the uppercase letter or an asterisk (*) for the color of that emoji. For example, these are the first few lines of the file with emojis for the Krempel code we used in the introduction (emoji.txt):

🍎R
πŸ’§B
🌟Y
πŸŒ‚P
πŸ«‘G
πŸ€G
🎀*
πŸ†P
πŸŒ›Y
🐒G
πŸ₯©R
πŸ’œP
…

Using these emojis, the ciphertext (str) for Cosmopolitan can be represented as (first example from introduction)

πŸŒ›πŸ’¦πŸ¦›πŸ’§πŸŽ€πŸ„πŸŒ™πŸŽ€πŸ„πŸ“˜πŸ¦›πŸ’¦πŸŽ€πŸŒ™πŸŒŸπŸ¦›πŸ’¦πŸ–€πŸ‘ΎπŸ–€πŸ«‘πŸŒ‚πŸ¦›πŸ€πŸŒ™πŸ–€πŸ’πŸ“˜πŸŽ€πŸŒ™πŸ‰

or as (second example from introduction)

πŸŒŸπŸ“˜πŸ¦›πŸ’§πŸ¦›πŸ₯©πŸŒ›πŸŽ€πŸ‰πŸ“˜πŸŽ€πŸ“˜πŸ¦›πŸŒŸπŸŒŸπŸ¦›πŸ¬πŸ–€πŸ‘ΎπŸ¦›πŸ«‘πŸ’œπŸ–€πŸ«‘πŸŒŸπŸ–€πŸ’πŸ’¦πŸ–€πŸŒ™πŸ₯©

Your task:

The functions colored_emojis, decode and encode must not modify the dictionaries (dict) passed to them.

Example

In this interactive session, we assume the text files krempel.txt and emoji.txt to be located in the current directory.

>>> character2colors = read_codes('krempel.txt')
>>> character2colors['C']
'YB'
>>> character2colors['o']
'B'
>>> character2colors['s']
'RY'

>>> emoji2color = read_emojis('emoji.txt')
>>> emoji2color['πŸ„']
'R'
>>> emoji2color['πŸ€']
'G'
>>> emoji2color['🐬']
'B'
>>> emoji2color['🎀']
'*'

>>> color2emojis = colored_emojis(emoji2color)
>>> color2emojis['R']
{'πŸ„', 'β›”', '🍎', 'πŸ‰', 'πŸ₯©'}
>>> color2emojis['G']
{'πŸ€', 'πŸ«‘', '🐒', '🌿'}
>>> color2emojis['B']
{'πŸ’§', 'πŸ’¦', 'πŸ“˜', '🐬'}
>>> color2emojis['*']
{'πŸ–€', '🎀', 'πŸ¦›'}

>>> decode('YB*B*RY*RB*B*YY*B*P*GP*GY*GB*YR', character2colors)
'Cosmopolitan'
>>> decode('πŸŒ›πŸ’¦πŸ¦›πŸ’§πŸŽ€πŸ„πŸŒ™πŸŽ€πŸ„πŸ“˜πŸ¦›πŸ’¦πŸŽ€πŸŒ™πŸŒŸπŸ¦›πŸ’¦πŸ–€πŸ‘ΎπŸ–€πŸ«‘πŸŒ‚πŸ¦›πŸ€πŸŒ™πŸ–€πŸ’πŸ“˜πŸŽ€πŸŒ™πŸ‰', character2colors, emoji2color)
'Cosmopolitan'
>>> decode('πŸŒŸπŸ“˜πŸ¦›πŸ’§πŸ¦›πŸ₯©πŸŒ›πŸŽ€πŸ‰πŸ“˜πŸŽ€πŸ“˜πŸ¦›πŸŒŸπŸŒŸπŸ¦›πŸ¬πŸ–€πŸ‘ΎπŸ¦›πŸ«‘πŸ’œπŸ–€πŸ«‘πŸŒŸπŸ–€πŸ’πŸ’¦πŸ–€πŸŒ™πŸ₯©', character2colors, emoji2color)
'Cosmopolitan'

>>> encode('Cosmopolitan', character2colors)
'YB*B*RY*RB*B*YY*B*P*GP*GY*GB*YR'
>>> encode('Cosmopolitan', character2colors, emoji2color)
'πŸŒ›πŸ’¦πŸ¦›πŸ’§πŸŽ€πŸ„πŸŒ™πŸŽ€πŸ„πŸ“˜πŸ¦›πŸ’¦πŸŽ€πŸŒ™πŸŒŸπŸ¦›πŸ’¦πŸ–€πŸ‘ΎπŸ–€πŸ«‘πŸŒ‚πŸ¦›πŸ€πŸŒ™πŸ–€πŸ’πŸ“˜πŸŽ€πŸŒ™πŸ‰'
>>> encode('Cosmopolitan', character2colors, emoji2color)
'πŸŒŸπŸ“˜πŸ¦›πŸ’§πŸ¦›πŸ₯©πŸŒ›πŸŽ€πŸ‰πŸ“˜πŸŽ€πŸ“˜πŸ¦›πŸŒŸπŸŒŸπŸ¦›πŸ¬πŸ–€πŸ‘ΎπŸ¦›πŸ«‘πŸ’œπŸ–€πŸ«‘πŸŒŸπŸ–€πŸ’πŸ’¦πŸ–€πŸŒ™πŸ₯©'