(Python!!!) Each character is represented by a 5-bit code (for example, 10000 re
ID: 3575199 • Letter: #
Question
(Python!!!)
Each character is represented by a 5-bit code (for example, 10000 represents the letter A1). Baudot code uses two modes — Letter mode and Figure mode — that let the same 5-bit pattern represent multiple characters (for example, 10000 represents A in Letter mode, and 1 in Figure mode).
The simplified plain ASCII-based table of codes that we will use looks like the following:
H 11010
I 01100
Note that not every code has meaning in both Letter and Figure modes. For the purposes of this assignment, you may assume that every code you receive is valid (in other words, you will never see a code of 00101 in Figure mode).
1 Many representations of Baudot code present the bits in the order of keys on the original keyboard: 5, 4, 1, 2, 3. For simplicity, we will order the bits from 1–5. Thus, the original Baudot code 01110 (H) will be represented as 11010 in our system.
Three of the 5-digit codes above are special control codes:
• ER (code 00011) represents an erasure. It adds an asterisk to the output indicating that the preceding character should be ignored.
• FS (code 00010) represents Figure Shift. If the decoder is currently in Letter mode, it shifts to Figure mode. If the decoder is already in Figure mode, then a space is printed.
• LS (code 00001) represents Letter Shift. If the decoder is currently in Figure mode, then it shifts to Letter mode. If the decoder is already in Letter mode, then a space is printed.
The decoder always begins in Letter mode.
For example, consider the following Baudot-encoded message:
10110 00101 01000 00001 00010 10000 11110 10000 00010 00001 00111 C S E SP/LSFS/SP 1 0 1 FS/SPSP/LS
11100 10110 10011 00101 ROCKS
We begin in Letter mode, and translate C, S, and E. 00001 is a space (SP) in Letter mode. 00010 in Letter mode tells us to shift to Figure mode, where we continue translating: 1, 0, 1, and a space (00010). 00001 in Figure mode shifts us back to Letter mode, where we finish the translation: R, O, C, K, S. Our final message is therefore CSE 101 ROCKS.
Write a Python program that reads in a Baudot-encoded message and prints its English translation (print an asterisk when you encounter an ER code). You may assume that the input always consists of a series of 5-digit binary codes separated by single spaces.
Sample Program Execution
(Program output is in italics, while user input is in boldface) Execution 1:
Enter Baudot message: 11110 00111 01100 01111 10011 00001 00100 11100 10100 00111 00001 11100 11101 10000 11011 10101 01100 01111 01000
Decoded message: DRINK YOUR OVALTINE
Execution 2:
Enter Baudot message: 01110 11100 10100 00111 00001 00101 10110 11100 00111 01000 00001 10000 01111 11110 00001 00010 01010 00010 00001 00100 01000 10000 00111 00101 00001 10000 01010 11100
Decoded message: FOUR SCORE AND 7 YEARS AGO
Explanation / Answer
letter = {'10000': 'A', '10001': '-', '10101': 'T', '10100': 'U', '01101': 'W', '01100': 'I', '11111': 'P', '11110': 'D', '11010': 'H', '11011': 'L', '01000': 'E', '01001': 'X', '00111': 'R', '00110': 'B', '00010': 'FS', '00011': 'ER', '10011': 'K', '10010': 'J', '10110': 'C', '10111': 'Q', '01110': 'F', '01111': 'N', '11001': 'Z', '11000': '/', '11100': 'O', '11101': 'V', '01011': 'M', '01010': 'G', '00001': 'SP', '00100': 'Y', '00101': 'S'} figure = {'10000': '1', '10001': '.', '10100': '4', '01101': '?', '11111': '+', '11110': '0', '11011': '=', '01000': '2', '00111': '-', '00110': '8', '00010': 'SP', '00011': 'ER', '10011': '(', '10010': '6', '10110': '9', '10111': '/', '11001': ':', '11100': '5', '11101': "'", '01011': ')', '01010': '7', '00001': 'LS', '00100': '3'} encoded = str(raw_input("Enter Baudot message")).strip().split(" ") out = "" dic = letter type = 'letter' for k in encoded: if dic[k] is 'SP': ch = ' ' elif dic[k] is 'ER': ch = '*' elif dic[k] is 'FS': if type is 'letter': dic = figure type = 'figure' ch = '' else: ch = ' ' elif dic[k] is 'LS': if type is 'figure': dic = letter type = 'letter' ch = '' else: ch = ' ' else: ch = dic[k] out = out + ch print out
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.