aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-27 15:43:46 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-27 15:43:46 +0000
commit334029e5f043365b08eefda94c620376d58ae611 (patch)
tree2fd7c493914fc23fd64c4b4491327a3ac3706a42
parent61cb5f1f04d6541803bd51a3cefe6c3978fa46b8 (diff)
downloadperlweeklychallenge-club-334029e5f043365b08eefda94c620376d58ae611.tar.gz
perlweeklychallenge-club-334029e5f043365b08eefda94c620376d58ae611.tar.bz2
perlweeklychallenge-club-334029e5f043365b08eefda94c620376d58ae611.zip
Add Perl solution to challenge 035
-rw-r--r--challenge-035/paulo-custodio/Makefile2
-rw-r--r--challenge-035/paulo-custodio/README1
-rw-r--r--challenge-035/paulo-custodio/perl/ch-1.pl38
-rw-r--r--challenge-035/paulo-custodio/perl/ch-2.pl30
-rw-r--r--challenge-035/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-035/paulo-custodio/t/test-2.yaml5
6 files changed, 81 insertions, 0 deletions
diff --git a/challenge-035/paulo-custodio/Makefile b/challenge-035/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-035/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-035/paulo-custodio/README b/challenge-035/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-035/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-035/paulo-custodio/perl/ch-1.pl b/challenge-035/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..1346f11cbf
--- /dev/null
+++ b/challenge-035/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Challenge 035
+#
+# TASK #1
+# Contributed by Paul Johnson
+# Write a program to encode text into binary encoded morse code.
+# Pay attention to any changes which might need to be made to the text to
+# make it valid morse code.
+#
+# Morse code consists of dots, dashes and gaps. It can be encoded in binary
+# in the following fashion:
+#
+# dot: 1
+# dash: 111
+# intra-character gap: 0
+# character gap: 000
+# word gap: 0000000
+# An intra-character gap is inserted between the dots and dashes in a character.
+
+use Modern::Perl;
+use Text::Morse;
+
+sub encode_bin_morse {
+ my($text) = @_;
+ my $morse = Text::Morse->new;
+ my $encoded = $morse->Encode($text); # ' ' between chars, '\n' between words
+ for ($encoded) {
+ s/\./10/g; # . = 1 + 0 gap
+ s/\-/1110/g; # - = 111 + 0 gap
+ s/ /00/g; # inter-word = 000 - 0 gap
+ s/\n/0000/g; # word gap = 0000000 - 00 - 0
+ s/0+$// # remove trailing zeros
+ }
+ return $encoded;
+}
+
+say encode_bin_morse("@ARGV");
diff --git a/challenge-035/paulo-custodio/perl/ch-2.pl b/challenge-035/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..1450b46fed
--- /dev/null
+++ b/challenge-035/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+# Challenge 035
+#
+# TASK #2
+# Contributed by Paul Johnson
+# Write a program to decode binary morse code.
+# Consider how it might be possible to recover from badly formed morse code.
+#
+# a) by splitting the morse code on gaps
+# b) without looking further than one digit ahead
+
+use Modern::Perl;
+use Text::Morse;
+
+sub decode_bin_morse {
+ my($encoded) = @_;
+ for ($encoded) {
+ s/0+$//; # remove trailing zeros
+ s/0000000/\n/g; # word gap
+ s/000/ /g; # character gap
+ s/1110?/-/g; # -
+ s/10?/./g; # .
+ }
+ my $morse = Text::Morse->new;
+ my $text = $morse->Decode($encoded);
+ return $text;
+}
+
+say decode_bin_morse("@ARGV");
diff --git a/challenge-035/paulo-custodio/t/test-1.yaml b/challenge-035/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..a26ea529b6
--- /dev/null
+++ b/challenge-035/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: MORSE CODE
+ input:
+ output: 11101110001110111011100010111010001010100010000000111010111010001110111011100011101010001
diff --git a/challenge-035/paulo-custodio/t/test-2.yaml b/challenge-035/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..9955ded934
--- /dev/null
+++ b/challenge-035/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 11101110001110111011100010111010001010100010000000111010111010001110111011100011101010001
+ input:
+ output: MORSE CODE