-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorse_Code.py
More file actions
49 lines (41 loc) · 1.79 KB
/
Copy pathMorse_Code.py
File metadata and controls
49 lines (41 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
MORSE_CODE_DICT = { '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':'-----',', ':'--..--', '.':'.-.-.-',
'?':'..--..','/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-' }
decodedword = ''
def encode(string , encodedword):
if (string == ' '):
encodedword = encodedword + ' '
else:
encoded = MORSE_CODE_DICT[string]
encodedword = encodedword + encoded + ' '
return encodedword
def decode(encodedword):
for key , value in MORSE_CODE_DICT.items():
if(value == encodedword):
return (str(decodedword) + str(key))
if __name__ == '__main__':
message = input("Enter The Sentence To Be Converted To Morse Code : ")
encodedword = ''
for i in message :
encodedword = encode(i , encodedword)
print(encodedword)
encodedmess = input("Enter The Encoded Message To Be Decoded : ")
encodedwords = encodedmess.split(' ')
for i in encodedwords:
encodedletters = i.split(' ')
for j in encodedletters:
decodedword = decode(j)
decodedword = str(decodedword) + ' '
print(decodedword)