forked from mayankgb2/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse_code
More file actions
60 lines (60 loc) · 1.7 KB
/
morse_code
File metadata and controls
60 lines (60 loc) · 1.7 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
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 2 11:21:31 2018
@author: Satyajit
"""
# Dictionary representing the morse code chart
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':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'
}
def encryption(message):
my_cipher = ''
for myletter in message:
if myletter != ' ':
my_cipher += MORSE_CODE_DICT[myletter] + ' '
else:
my_cipher += ' '
return my_cipher
# This function is used to decrypt
# Morse code to English
def decryption(message):
message += ' '
decipher = ''
mycitext = ''
for myletter in message:
# checks for space
if (myletter != ' '):
i = 0
mycitext += myletter
else:
i += 1
if i == 2 :
decipher += ' '
else:
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
.values()).index(mycitext)]
mycitext = ''
return decipher
def main():
my_message = "PYTHON-PROGRAM"
output = encryption(my_message.upper())
print (output)
my_message = ".--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- -- "
output = decryption(my_message)
print (output)
# Executes the main function
if __name__ == '__main__':
main()