aboutsummaryrefslogtreecommitdiff
path: root/challenge-035
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-03-26 13:11:43 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-03-26 13:11:43 +0200
commit2a6bb0ae68aad6f29c807dc4f0423768cc845d9a (patch)
tree443f0decd33a748576a4e759ac2ea121319ebcc6 /challenge-035
parente654b9a8e0b8cb51c472c474d80c9b71affb892a (diff)
downloadperlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.tar.gz
perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.tar.bz2
perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.zip
Challenges 035 038 042 LK Perl Python
Diffstat (limited to 'challenge-035')
-rw-r--r--challenge-035/lubos-kolouch/perl/ch-1.pl57
-rw-r--r--challenge-035/lubos-kolouch/perl/ch-2.pl70
-rw-r--r--challenge-035/lubos-kolouch/python/ch-1.py62
-rw-r--r--challenge-035/lubos-kolouch/python/ch-2.py60
4 files changed, 249 insertions, 0 deletions
diff --git a/challenge-035/lubos-kolouch/perl/ch-1.pl b/challenge-035/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..186fc24c4d
--- /dev/null
+++ b/challenge-035/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,57 @@
+use strict;
+use warnings;
+use Test::More;
+
+# Define Morse code mappings
+my %morse_code = (
+ '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' => '--..',
+ ' ' => '/'
+);
+
+# Encode text into binary encoded Morse code
+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$//; # Remove trailing intra-character gap
+ return $encoded;
+}
diff --git a/challenge-035/lubos-kolouch/perl/ch-2.pl b/challenge-035/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..16fc9af817
--- /dev/null
+++ b/challenge-035/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,70 @@
+use strict;
+use warnings;
+use Test::More;
+
+# Define Morse code mappings
+my %morse_code = (
+ '.-' => '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',
+ '/' => ' '
+);
+
+# Decode binary encoded Morse code
+sub decode_morse {
+ my $encoded = shift;
+
+ # Split encoded text into words and characters
+ my @words = ( $encoded =~ /0{7}/g );
+ my @letters = map { [ ( $_ =~ /0{3}/g ) ] } @words;
+
+ # Convert Morse code to plaintext
+ my $plaintext = '';
+ for my $word (@letters) {
+ for my $letter (@$word) {
+ if ( exists $morse_code{$letter} ) {
+ $plaintext .= $morse_code{$letter};
+ }
+ else {
+ # If the Morse code is badly formed, try to recover by looking one digit ahead
+ for my $i ( 1 .. 3 ) {
+ my $lookahead = substr( $letter, $i );
+ if ( exists $morse_code{$lookahead} ) {
+ $plaintext .= $morse_code{$lookahead};
+ last;
+ }
+ }
+
+# If no valid Morse code sequence can be recovered, treat the character as a space
+ $plaintext .= ' '
+ unless length $plaintext && substr( $plaintext, -1 ) eq ' ';
+ }
+ }
+ $plaintext .= ' ';
+ }
+ $plaintext =~ s/\s+$//; # Remove trailing space
+ return $plaintext;
+}
diff --git a/challenge-035/lubos-kolouch/python/ch-1.py b/challenge-035/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..a9d929615a
--- /dev/null
+++ b/challenge-035/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import unittest
+
+# Define Morse code mappings
+morse_code = {
+ "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": "--..",
+ " ": "/",
+}
+
+
+# Encode text into binary encoded Morse code
+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") # Remove trailing intra-character gap
+ return encoded
+
+
+# Define tests
+class TestMorseEncoding(unittest.TestCase):
+ def test_hello_world(self):
+ encoded_text = encode_morse("HELLO WORLD")
+ self.assertEqual(
+ encoded_text,
+ "1010100011101110111010111000101110100010111010001110101000101011100010111010001110100011101011100010101110001011101000101110100010111010001110100011101011100010111010001110100011101011100010101110001011101",
+ )
diff --git a/challenge-035/lubos-kolouch/python/ch-2.py b/challenge-035/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..619c51d9e8
--- /dev/null
+++ b/challenge-035/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import re
+import unittest
+
+# Define Morse code mappings
+morse_code = {
+ ".-": "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",
+ "/": " ",
+}
+
+
+# Decode binary encoded Morse code
+def decode_morse(encoded):
+ # Split encoded text into words and characters
+ words = re.findall("0{7}", encoded + "0000000")
+ letters = [re.findall("0{3}", word + "000") for word in words]
+ # Convert Morse code to plaintext
+ plaintext = ""
+ for word in letters:
+ for letter in word:
+ if letter in morse_code:
+ plaintext += morse_code[letter]
+ else:
+ # If the Morse code is badly formed, try to recover by looking one digit ahead
+ for i in range(1, 4):
+ lookahead = letter[i:]
+ if lookahead in morse_code:
+ plaintext += morse_code[lookahead]
+ break
+ else:
+ # If no valid Morse code sequence can be recovered, treat the character as a space
+ plaintext += " "
+ return plaintext