The Weekly Challenge 035

Morse Code Binary Encoding and Decoding

Solutions on GitHub

Task 1: Morse Code Binary Encoding

Write a script to encode text into binary encoded Morse code based on standard timing rules (dot = 1, dash = 111, intra-character gap = 0, inter-character gap = 000, word gap = 0000000).

Example Output

Input: "HELLO WORLD" Output: 1010100011101110111010111000101110100010111010001110101000101011100010111010001110100011101011100010101110001011101000101110100010111010001110100011101011100010111010001110100011101011100010101110001011101

Logic

Represent characters as standard Morse signals. Convert each dot (.) to 10 and each dash (-) to 1110, using 000 between characters, and 0000000 between words. Join the bits together and clean up trailing zeroes.

Perl Solution

ch-1.pl

sub encode_morse { my $text = shift; my $encoded = ''; foreach my $char ( split //, uc $text ) { if ( exists $morse_code{$char} ) { my $morse = $morse_code{$char}; my @bits = (); foreach my $symbol ( split //, $morse ) { if ( $symbol eq '.' ) { push @bits, '1', '0'; } elsif ( $symbol eq '-' ) { push @bits, '1' x 3, '0'; } } $encoded .= join( '0', @bits ) . '0'; } } $encoded =~ s/0$//; return $encoded; }

Python Solution

ch-1.py

def encode_morse(text): encoded = "" for char in text.upper(): if char in morse_code: morse = morse_code[char] bits = [] for symbol in morse: if symbol == ".": bits.extend(["1", "0"]) elif symbol == "-": bits.extend(["1" * 3, "0"]) encoded += "0".join(bits) + "0" encoded = encoded.rstrip("0") return encoded

Task 2: Morse Code Binary Decoding

Write a script to decode the binary Morse code generated in Task 1 back into standard text.

Example Output

Input: "1010100011101110111010111000101110..." Output: "HELLO WORLD"

Logic

Split the binary stream by 7 zeroes (0000000) to extract words. Then split each word by 3 zeroes (000) to extract characters. Parse the remaining binary bits back to dots/dashes and look up the plaintext letters.

Perl Solution

ch-2.pl

sub decode_morse { my $encoded = shift; my @words = ( $encoded =~ /0{7}/g ); my @letters = map { [ ( $_ =~ /0{3}/g ) ] } @words; my $plaintext = ''; for my $word (@letters) { for my $letter (@$word) { if ( exists $morse_code{$letter} ) { $plaintext .= $morse_code{$letter}; } else { for my $i ( 1 .. 3 ) { my $lookahead = substr( $letter, $i ); if ( exists $morse_code{$lookahead} ) { $plaintext .= $morse_code{$lookahead}; last; } } $plaintext .= ' ' unless length $plaintext && substr( $plaintext, -1 ) eq ' '; } } $plaintext .= ' '; } $plaintext =~ s/\s+$//; return $plaintext; }

Python Solution

ch-2.py

def decode_morse(encoded): words = re.findall("0{7}", encoded + "0000000") letters = [re.findall("0{3}", word + "000") for word in words] plaintext = "" for word in letters: for letter in word: if letter in morse_code: plaintext += morse_code[letter] else: for i in range(1, 4): lookahead = letter[i:] if lookahead in morse_code: plaintext += morse_code[lookahead] break else: plaintext += " " return plaintext