0% found this document useful (0 votes)
23 views

Python - Morse Code

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Python - Morse Code

Uploaded by

Chanchal jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python_MorseCode.

ipynb - Colab 06/08/24, 3:23 PM

      !    ψ 
##
## Morse
Morse Code
Code ##
##
Morse Code

1 MORSE_CODE = {
2 'A': '.-',
3 'B': '-...',
4 'C': '-.-.',
5 'D': '-..',
6 'E': '.',
7 'F': '..-.',
8 'G': '--.',
9 'H': '....',
10 'I': '..',
11 'J': '.---',
12 'K': '-.-',
13 'L': '.-..',
14 'M': '--',
15 'N': '-.',
16 'O': '---',
17 'P': '.--.',
18 'Q': '--.-',
19 'R': '.-.',
20 'S': '...',
21 'T': '-',
22 'U': '..-',
23 'V': '...-',
24 'W': '.--',
25 'X': '-..-',
26 'Y': '-.--',
27 'Z': '--..',
28 '1': '.----',
29 '2': '..---',
30 '3': '...--',
31 '4': '....-',
32 '5': '.....',
33 '6': '-....',
34 '7': '--...',
35 '8': '---..',
36 '9': '----.',
37 '0': '-----',
38 '&': '.-...',
39 '@': '.--.-.',
40 ':': '---...',
41 ',': '--..--',
https://colab.research.google.com/drive/10VzajFp6K0rzuqJhqs3mR18ZV-qHwXlI#scrollTo=IMF9-wQ4G17K Page 1 of 4
Python_MorseCode.ipynb - Colab 06/08/24, 3:23 PM

41 ',': '--..--',
42 '.': '.-.-.-',
43 ''': '.----.',
44 ''': '.-..-.',
45 '?': '..--..',
46 '/': '-..-.',
47 '=': '-...-',
48 '+': '.-.-.',
49 '-': '-....-',
50 '(': '-.--.',
51 ')': '-.--.-',
52 '!': '-.-.--',
53 }
54
55
56 def encrypt(message):
57 return ' '.join(
58 [
59 MORSE_CODE[char.upper()] if char != ' ' else '/'
60 for char in message
61 ]
62 )
63
64 sList = []
65
66 def decrypt(dMessage):
67 decryptM = ''
68
69 sList = dMessage.split(' ')
70
71 for i in sList:
72 if i == '/':
73 decryptM += ' '
74 else:
75 for key, value in MORSE_CODE.items():
76 if value == i:
77 decryptM += key
78
79 return decryptM
80
81 print(decrypt('.--. -.-- - .... --- -. / ...-- .-.-.- ----. '))

PYTHON 3.9

https://colab.research.google.com/drive/10VzajFp6K0rzuqJhqs3mR18ZV-qHwXlI#scrollTo=IMF9-wQ4G17K Page 2 of 4
Python_MorseCode.ipynb - Colab 06/08/24, 3:23 PM

1 dMessage = '.--. -.-- - .... --- -. / ...-- .-.-.- ----. '


2 sList = dMessage.split(' ')
3 print(sList)

['.--.', '-.--', '-', '....', '---', '-.', '/', '...--', '.-.-.-', '----.', ''

1 MORSE_CODE = {
2 'A': '.-',
3 'B': '-...',
4 'C': '-.-.',
5 'D': '-..',
6 'E': '.'
7 }
8
9 sList = []
10 decryptM = ''
11
12 def decrypt(dMessage):
13 sList = dMessage.split(' ')
14
15 for i in sList:
16 if i != ' ':
17 for key, value in MORSE_CODE.items():
18 if value == ".-":
19 decryptM += key
20 elif i == '/':
21 decryptM += ' '
22
23 return decryptM

1 # MorseCode.encrypt('HOLD!')
2 # '.... --- .-.. -.. -.-.--'
3
4 # MorseCode.decrypt('.... --- .-.. -.. -.-.--')
5 # 'HOLD!'

https://colab.research.google.com/drive/10VzajFp6K0rzuqJhqs3mR18ZV-qHwXlI#scrollTo=IMF9-wQ4G17K Page 3 of 4
Python_MorseCode.ipynb - Colab 06/08/24, 3:23 PM

https://colab.research.google.com/drive/10VzajFp6K0rzuqJhqs3mR18ZV-qHwXlI#scrollTo=IMF9-wQ4G17K Page 4 of 4

You might also like