Morse Code Implementation | function to encode a alphabet as Morse code ; refer to the Morse table image attached in the article ; for space ; character by character print Morse code ; Driver Code
def morseEncode ( x ) : NEW_LINE INDENT if x is ' a ' : NEW_LINE INDENT return " . - " NEW_LINE DEDENT elif x is ' b ' : NEW_LINE INDENT return " - . . . " NEW_LINE DEDENT elif x is ' c ' : NEW_LINE INDENT return " - . - . " NEW_LINE DEDENT elif x is ' d ' : NEW_LINE INDENT return " - . . " NEW_LINE DEDENT elif x is ' e ' : NEW_LINE INDENT return " . " NEW_LINE DEDENT elif x is ' f ' : NEW_LINE INDENT return " . . - . " NEW_LINE DEDENT elif x is ' g ' : NEW_LINE INDENT return " - - . " NEW_LINE DEDENT elif x is ' h ' : NEW_LINE INDENT return " . . . . " NEW_LINE DEDENT elif x is ' i ' : NEW_LINE INDENT return " . . " NEW_LINE DEDENT elif x is ' j ' : NEW_LINE INDENT return " . - - - " NEW_LINE DEDENT elif x is ' k ' : NEW_LINE INDENT return " - . - " NEW_LINE DEDENT elif x is ' l ' : NEW_LINE INDENT return " . - . . " NEW_LINE DEDENT elif x is ' m ' : NEW_LINE INDENT return " - - " NEW_LINE DEDENT elif x is ' n ' : NEW_LINE INDENT return " - . " NEW_LINE DEDENT elif x is ' o ' : NEW_LINE INDENT return " - - - " NEW_LINE DEDENT elif x is ' p ' : NEW_LINE INDENT return " . - - . " NEW_LINE DEDENT elif x is ' q ' : NEW_LINE INDENT return " - - . - " NEW_LINE DEDENT elif x is ' r ' : NEW_LINE INDENT return " . - . " NEW_LINE DEDENT elif x is ' s ' : NEW_LINE INDENT return " . . . " NEW_LINE DEDENT elif x is ' t ' : NEW_LINE INDENT return " - " NEW_LINE DEDENT elif x is ' u ' : NEW_LINE INDENT return " . . - " NEW_LINE DEDENT elif x is ' v ' : NEW_LINE INDENT return " . . . - " NEW_LINE DEDENT elif x is ' w ' : NEW_LINE INDENT return " . - - " NEW_LINE DEDENT elif x is ' x ' : NEW_LINE INDENT return " - . . - " NEW_LINE DEDENT elif x is ' y ' : NEW_LINE INDENT return " - . - - " NEW_LINE DEDENT elif x is ' z ' : NEW_LINE INDENT return " - - . . " NEW_LINE DEDENT elif x is '1' : NEW_LINE INDENT return " . - - - - " ; NEW_LINE DEDENT elif x is '2' : NEW_LINE INDENT return " . . - - - " ; NEW_LINE DEDENT elif x is '3' : NEW_LINE INDENT return " . . . - - " ; NEW_LINE DEDENT elif x is '4' : NEW_LINE INDENT return " . . . . - " ; NEW_LINE DEDENT elif x is '5' : NEW_LINE INDENT return " . . . . . " ; NEW_LINE DEDENT elif x is '6' : NEW_LINE INDENT return " - . . . . " ; NEW_LINE DEDENT elif x is '7' : NEW_LINE INDENT return " - - . . . " ; NEW_LINE DEDENT elif x is '8' : NEW_LINE INDENT return " - - - . . " ; NEW_LINE DEDENT elif x is '9' : NEW_LINE INDENT return " - - - - . " ; NEW_LINE DEDENT elif x is '0' : NEW_LINE INDENT return " - - - - - " ; NEW_LINE DEDENT DEDENT def morseCode ( s ) : NEW_LINE INDENT for character in s : NEW_LINE INDENT print ( morseEncode ( character ) , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE morseCode ( s ) NEW_LINE DEDENT