solution_025.py

#!/usr/bin/python3 
# ==================================================================
# Morse code translator (enter text, output Morse code)
# ==================================================================
#
# Each dit or dah within an encoded character is followed by a
# period of signal absence, called a space, equal to the dit
# duration. The letters of a word are separated by a space of
# duration equal to three dits, and words are separated by a
# space equal to seven dits.
#
# ==================================================================
#
# Question for the student:
#    What is the flaw in this solution (code)?
#
# Answer:
#   (O4( RK! !ZKMO!7 WY?!O MYNO RK! ZK1!O! YP NSPPO?OX( VOXQ(R7
#   RY3 NY 5Y1 MYX2O? LO(3OOX (ROW8
#
# To decode the answer use the program developed for
#   project: "Send and receive secret messages" (shift 10)
#   plaintext alphabet for shift cipher:
#     ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
#      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
#      'U', 'V', 'W', 'X', 'Y', 'Z', '.', '?', '!', '(',
#      '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
#      ')']
#
# ==================================================================

import sys
import user_interface as ui


abc = { 'A' : '.-',
        'B' : '-..',
        'C' : '-.-.',
        'D' : '-..',
        'E' : '.',
        'F' : '..-.',
        'G' : '--.',
        'H' : '....',
        'I' : '..',
        'J' : '.--',
        'K' : '-.-',
        'L' : '.-..',
        'M' : '--',
        'N' : '-.',
        'O' : '---',
        'p' : '.--.',
        'Q' : '--.-',
        'R' : '.-.',
        'S' : '...',
        'T' : '-',
        'U' : '..-',
        'V' : '...-',
        'W' : '.--',
        'X' : '-..-',
        'Y' : '-.--',
        'Z' : '--..',
        '1' : '.----',
        '2' : '..---',
        '3' : '...--',
        '4' : '....-',
        '5' : '.....',
        '6' : '-....',
        '7' : '--...',
        '8' : '---..',
        '9' : '----.',
        '0' : '-----',
        '.' : '.-.-.-',
        ',' : '--..--',
        '?' : '..--..',
        '!' : '-.-.--',
        ':' : '---...',
        ';' : '-.-.-.',
        '/' : '-..-.',
        '=' : '-...-',
        '+' : '.-.-.',
        '-' : '-....-' }

xabc = { 'A' : 'dot,dash',
         'B' : 'dash,dot,dot',
         'C' : 'dash,dot,dash,dot',
         'D' : 'dash,dot,dot',
         'E' : 'dot',
         'F' : 'dot,dot,dash,dot',
         'G' : 'dash,dash,dot',
         'H' : 'dot,dot,dot,dot',
         'I' : 'dot,dot',
         'J' : 'dot,dash,dash',
         'K' : 'dash,dot,dash',
         'L' : 'dot,dash,dot,dot',
         'M' : 'dash,dash',
         'N' : 'dash,dot',
         'O' : 'dash,dash,dash',
         'p' : 'dot,dash,dash,dot',
         'Q' : 'dash,dash,dot,dash',
         'R' : 'dot,dash,dot',
         'S' : 'dot,dot,dot',
         'T' : 'dash',
         'U' : 'dot,dot,dash',
         'V' : 'dot,dot,dot,dash',
         'W' : 'dot,dash,dash',
         'X' : 'dash,dot,dot,dash',
         'Y' : 'dash,dot,dash,dash',
         'Z' : 'dash,dash,dot,dot',
         '1' : 'dot,dash,dash,dash,dash',
         '2' : 'dot,dot,dash,dash,dash',
         '3' : 'dot,dot,dot,dash,dash',
         '4' : 'dot,dot,dot,dot,dash',
         '5' : 'dot,dot,dot,dot,dot',
         '6' : 'dash,dot,dot,dot,dot',
         '7' : 'dash,dash,dot,dot,dot',
         '8' : 'dash,dash,dash,dot,dot',
         '9' : 'dash,dash,dash,dash,dot',
         '0' : 'dash,dash,dash,dash,dash',
         '.' : 'dot,dash,dot,dash,dot,dash',
         ',' : 'dash,dash,dot,dot,dash,dash',
         '?' : 'dot,dot,dash,dash,dot,dot',
         '!' : 'dash,dot,dash,dot,dash,dash',
         ':' : 'dash,dash,dash,dot,dot,dot',
         ';' : 'dash,dot,dash,dot,dash,dot',
         '/' : 'dash,dot,dot,dash,dot',
         '=' : 'dash,dot,dot,dot,dash',
         '+' : 'dot,dash,dot,dash,dot',
         '-' : 'dash,dot,dot,dot,dot,dash' }


# -------------------------------------------------------------------
# ---- directory entry exists?
# -------------------------------------------------------------------

def checkKey(dict,k):
    if k in dict:
        return True
    return False


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

if __name__ == '__main__':

    def verifyList(abc):

        print()
        print('verify abc dictionary only contains . or -')

        err = 0

        for k,v in abc.items():
            lst = list(v)
            for x in lst:
                if x == '.' or x == '-':
                   continue
            print(f'error in key="{k}" value="{v}"')
            err += 1

        print()
        print(f'{err} dot/dash errors found')


    while(True):

        # ---- ask the user for input

        print()
        s = ui.get_user_input('Enter text: ')

        if not s:              # empty string?
            break        

        # ---- process each character after converting to uppercase

        for c in list(s.upper()):

            if not checkKey(abc,c):
                print()
                print(f'key {c} not in dictionary - end program')
                print()
                sys.exit()

            print(abc[c],end='')
            print(' ',end='')
        print()


    print()