-
-
Save ezirmusitua/4cd0cc1b27ae9c26bfc1a0a194f53db2 to your computer and use it in GitHub Desktop.
[Morse encoding] codewar challenge: morse encoding #python #algorithm #challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convert_complement_to_dec(str): | |
if str[0] == '1': | |
reversed_str = '' | |
for bit in str[1:]: | |
reversed_str += '1' if bit == '0' else '0' | |
return -1 * (int('0b' + reversed_str, 2) + 1) | |
return int('0b' + str, 2) | |
def convert_dec_to_complement(val): | |
val_bin = bin(val) | |
if val >= 0: return '0' + '0' * (32 - len(val_bin) + 3) + bin(val)[2:] | |
tmp_reverse = '' | |
tmp = '1' * (32 - len(val_bin) + 3 - 1) + val_bin[3:] | |
for bit in tmp: | |
tmp_reverse += '1' if bit == '0' else '0' | |
tmp_reverse_1 = bin(int('0b' + tmp_reverse, 2) + 1)[2:] | |
return '1' + '0' * (32 - len(tmp_reverse_1) - 1) + tmp_reverse_1 | |
class Morse: | |
alpha={ | |
'A': '10111', | |
'B': '111010101', | |
'C': '11101011101', | |
'D': '1110101', | |
'E': '1', | |
'F': '101011101', | |
'G': '111011101', | |
'H': '1010101', | |
'I': '101', | |
'J': '1011101110111', | |
'K': '111010111', | |
'L': '101110101', | |
'M': '1110111', | |
'N': '11101', | |
'O': '11101110111', | |
'P': '10111011101', | |
'Q': '1110111010111', | |
'R': '1011101', | |
'S': '10101', | |
'T': '111', | |
'U': '1010111', | |
'V': '101010111', | |
'W': '101110111', | |
'X': '11101010111', | |
'Y': '1110101110111', | |
'Z': '11101110101', | |
'0': '1110111011101110111', | |
'1': '10111011101110111', | |
'2': '101011101110111', | |
'3': '1010101110111', | |
'4': '10101010111', | |
'5': '101010101', | |
'6': '11101010101', | |
'7': '1110111010101', | |
'8': '111011101110101', | |
'9': '11101110111011101', | |
'.': '10111010111010111', | |
',': '1110111010101110111', | |
'?': '101011101110101', | |
"'": '1011101110111011101', | |
'!': '1110101110101110111', | |
'/': '1110101011101', | |
'(': '111010111011101', | |
')': '1110101110111010111', | |
'&': '10111010101', | |
':': '11101110111010101', | |
';': '11101011101011101', | |
'=': '1110101010111', | |
'+': '1011101011101', | |
'-': '111010101010111', | |
'_': '10101110111010111', | |
'"': '101110101011101', | |
'$': '10101011101010111', | |
'@': '10111011101011101', | |
' ': '0'} | |
sorted_alpha_tuple = sorted(alpha.items(), key=lambda kvt: len(kvt[1]), reverse=True) | |
@classmethod | |
def find_matching_alpha(cls, binary_str): | |
for alpha_code in Morse.sorted_alpha_tuple: | |
if alpha_code[1] == binary_str[:len(alpha_code[1])]: | |
return alpha_code[0], binary_str[len(alpha_code[1]) + 3:] | |
return None | |
@classmethod | |
def encode(self,message): | |
chars = list(message) | |
binary_str = '' | |
for char in chars: | |
binary_str += Morse.alpha[char] + '000' | |
binary_str += '0' * (32 - (len(binary_str) % 32)) | |
turn = 0 | |
result = list() | |
while True: | |
if (turn + 1) * 32 > len(binary_str): break | |
tmp = convert_complement_to_dec(binary_str[turn * 32:(turn + 1) * 32]) | |
result.append(tmp) | |
turn += 1 | |
return result | |
@classmethod | |
def decode(self, array): | |
binary_str = '' | |
for dec in array: | |
binary_str += convert_dec_to_complement(dec) | |
result = '' | |
while binary_str: | |
tmp, binary_str = Morse.find_matching_alpha(binary_str) | |
result += tmp | |
return result.strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment