Code 391 is a technique for constructing bar codes. It was developed in 1974 by Dr. David Allais and Ray Stevens for the company Intermec. In code 39, every character is represented by five vertical bars, that are separated from each other by four vertical white bars. Three of these nine bars are wide and the others are narrow. Bar codes of consecutive characters are always separated by a narrow white bar. Here's how the word GEOLOGY is displayed as a code 39 bar code.
Code 39 bar codes are represented in ASCII format using four symbols that represent the four combinations of narrow/wide black/white bars.
bar | symbol |
---|---|
wide-black | B |
narrow-black | S |
wide-white | b |
narrow-white | s |
As such, the sequence BsSsSbSsB is the ASCII-representation of the letter A in code 39.
An encryption key is a dictionary (dict) that maps characters (str) onto their ASCII-representation in code 39 (str). Note that each character has a unique representation in code 39 (represented as a 9-character string). Because code 39 makes no distinction between uppercase and lowercase letters, encryption keys never use lowercase letters as their keys.
Your task:
Write a function reverse that takes a dictionary (dict). The function must return a new dictionary (dict) containing the same key/value-pairs as the given dictionary, but with the roles of keys and values reversed. The function may assume that all values in the given dictionary are unique and immutable.
Write a function code39 that takes two arguments: i) a sentence (str) and ii) an encryption key (dict). The function must return the ASCII-representation (str) of the given sentence in code 39, based on the given encryption key. When converting to code 39, the function must make no distinction between uppercase and lowercase letters. In addition, the function may assume that each character in the given sentence occurs as a key in the given encryption key.
Keep in mind that the conversion of consecutive characters of the sentence must always be separated by a narrow white bar (represented by the letter s).
Write a function decode39 that takes two arguments: i) the ASCII-representation (str) of a sentence in code 39 and ii) the encryption key (dict) used for the conversion to code 39. The function must return the original sentence, where all letters of the sentence are represented as uppercase letters.
>>> key = {
... 'U': 'SbSbSbSsS', 'Z': 'SsBsSbBsS', 'P': 'BbSsSsBsS', 'R': 'SsSbBsBsS',
... 'H': 'SsBbBsSsS', 'W': 'SbBsSsBsS', 'D': 'SbBsSsSsB', 'K': 'BsSsSsBbS',
... '-': 'SsSbBsSsB', 'M': 'SbSsSbSbS', 'O': 'SsSbSsBsB', '7': 'SsSbSbSbS',
... '+': 'BsSsBbSsS', '1': 'SsSsBbBsS', ' ': 'SsBsBbSsS', '.': 'SsBbSsBsS',
... '/': 'SsSsBsBbS', 'V': 'SbSsBsBsS', 'X': 'SbSbSsSbS', 'C': 'SbSsBsSsB',
... 'Y': 'BsSsSbBsS', 'G': 'BsBsSsSbS', '4': 'SsBbSsSsB', 'Q': 'SsBsSbSsB',
... 'J': 'SsSsSsBbB', 'F': 'BsSsSbSsB', 'A': 'SsBsSsSbB', '6': 'BsSsSsSbB',
... '2': 'BsBsSbSsS', '$': 'SsSsSbBsB', '0': 'BsSbBsSsS', 'N': 'SsBsBsSbS',
... 'I': 'BsSbSsSsB', '9': 'BbSsBsSsS', 'L': 'BsSbSsBsS', ',': 'SsSsBsSbB',
... '5': 'BsSsBsSbS', 'B': 'BbBsSsSsS', '%': 'SsBsSsBbS', 'S': 'BsBbSsSsS',
... '3': 'SbBsBsSsS', 'T': 'BbSsSsSsB', '*': 'SsSsBbSsB', 'E': 'SbSsSsBsB'
... }
>>> reverse(key)
{'SbSbSbSsS': 'U', 'SsBsSbBsS': 'Z', 'BbSsSsBsS': 'P', 'SsSbBsBsS': 'R', 'SsBbBsSsS': 'H', 'SbBsSsBsS': 'W', 'SbBsSsSsB': 'D', 'BsSsSsBbS': 'K', 'SsSbBsSsB': '-', 'SbSsSbSbS': 'M', 'SsSbSsBsB': 'O', 'SsSbSbSbS': '7', 'BsSsBbSsS': '+', 'SsSsBbBsS': '1', 'SsBsBbSsS': ' ', 'SsBbSsBsS': '.', 'SsSsBsBbS': '/', 'SbSsBsBsS': 'V', 'SbSbSsSbS': 'X', 'SbSsBsSsB': 'C', 'BsSsSbBsS': 'Y', 'BsBsSsSbS': 'G', 'SsBbSsSsB': '4', 'SsBsSbSsB': 'Q', 'SsSsSsBbB': 'J', 'BsSsSbSsB': 'F', 'SsBsSsSbB': 'A', 'BsSsSsSbB': '6', 'BsBsSbSsS': '2', 'SsSsSbBsB': '$', 'BsSbBsSsS': '0', 'SsBsBsSbS': 'N', 'BsSbSsSsB': 'I', 'BbSsBsSsS': '9', 'BsSbSsBsS': 'L', 'SsSsBsSbB': ',', 'BsSsBsSbS': '5', 'BbBsSsSsS': 'B', 'SsBsSsBbS': '%', 'BsBbSsSsS': 'S', 'SbBsBsSsS': '3', 'BbSsSsSsB': 'T', 'SsSsBbSsB': '*', 'SbSsSsBsB': 'E'}
>>> encoded = code39('Sulfur, so good.', key)
>>> encoded
'BsBbSsSsSsSbSbSbSsSsBsSbSsBsSsBsSsSbSsBsSbSbSbSsSsSsSbBsBsSsSsSsBsSbBsSsBsBbSsSsBsBbSsSsSsSsSbSsBsBsSsBsBbSsSsBsBsSsSbSsSsSbSsBsBsSsSbSsBsBsSbBsSsSsBsSsBbSsBsS'
>>> decode39(encoded, key)
'SULFUR, SO GOOD.'