mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-20 03:31:26 +08:00
subset_generation (#326)
* subset_generation generate all possible subset of size n of a given array of size r * Rename subset_generation to subset_generation.py * Update subset_generation.py I made all changes I could . What I mean is I removed all the empty space ....... There some comment extra if you feel removing those comments please do so yourself pls provide spacing as it should be * Create morse_Code_implementation.py * Any more changes pls let me know
This commit is contained in:

committed by
Anup Kumar Panwar

parent
974088d872
commit
7f4f565e62
82
ciphers/morse_Code_implementation.py
Normal file
82
ciphers/morse_Code_implementation.py
Normal file
@ -0,0 +1,82 @@
|
||||
# Python program to implement Morse Code Translator
|
||||
|
||||
|
||||
# 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 encrypt(message):
|
||||
cipher = ''
|
||||
for letter in message:
|
||||
if letter != ' ':
|
||||
|
||||
|
||||
cipher += MORSE_CODE_DICT[letter] + ' '
|
||||
else:
|
||||
|
||||
cipher += ' '
|
||||
|
||||
return cipher
|
||||
|
||||
|
||||
def decrypt(message):
|
||||
|
||||
message += ' '
|
||||
|
||||
decipher = ''
|
||||
citext = ''
|
||||
for letter in message:
|
||||
|
||||
if (letter != ' '):
|
||||
|
||||
|
||||
i = 0
|
||||
|
||||
|
||||
citext += letter
|
||||
|
||||
else:
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
if i == 2 :
|
||||
|
||||
|
||||
decipher += ' '
|
||||
else:
|
||||
|
||||
|
||||
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
|
||||
.values()).index(citext)]
|
||||
citext = ''
|
||||
|
||||
return decipher
|
||||
|
||||
|
||||
def main():
|
||||
message = "Morse code here"
|
||||
result = encrypt(message.upper())
|
||||
print (result)
|
||||
|
||||
message = result
|
||||
result = decrypt(message)
|
||||
print (result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user