aboutsummaryrefslogtreecommitdiff
path: root/challenge-035
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2019-11-24 18:16:04 -0500
committerJaldhar H. Vyas <jaldhar@braincells.com>2019-11-24 18:16:04 -0500
commit13b8d97fe2292ab38ed32b9ed27b95a476490fbb (patch)
treeba2d8fadc2c8ecfbc51a6f98781f6bfd8052ca9f /challenge-035
parent4b5f8423cee1b75c862ce9c50cc22da4196432c4 (diff)
downloadperlweeklychallenge-club-13b8d97fe2292ab38ed32b9ed27b95a476490fbb.tar.gz
perlweeklychallenge-club-13b8d97fe2292ab38ed32b9ed27b95a476490fbb.tar.bz2
perlweeklychallenge-club-13b8d97fe2292ab38ed32b9ed27b95a476490fbb.zip
Challenge 35 by Jaldhar H. Vyas.
Diffstat (limited to 'challenge-035')
-rw-r--r--challenge-035/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-035/jaldhar-h-vyas/perl/ch-1.pl65
-rwxr-xr-xchallenge-035/jaldhar-h-vyas/perl/ch-2.pl66
-rwxr-xr-xchallenge-035/jaldhar-h-vyas/raku/ch-1.p664
-rwxr-xr-xchallenge-035/jaldhar-h-vyas/raku/ch-2.p664
5 files changed, 260 insertions, 0 deletions
diff --git a/challenge-035/jaldhar-h-vyas/blog.txt b/challenge-035/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..6293ab2d3d
--- /dev/null
+++ b/challenge-035/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2019/11/perl_weekly_challenge_week_35.html
diff --git a/challenge-035/jaldhar-h-vyas/perl/ch-1.pl b/challenge-035/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..a2eeaa7f7e
--- /dev/null
+++ b/challenge-035/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+use constant CHARACTER_GAP => '000';
+use constant WORD_GAP => '000000';
+
+my %to_morse = (
+ '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',
+ '1' => '10111011101110111',
+ '2' => '101011101110111',
+ '3' => '1010101110111',
+ '4' => '10101010111',
+ '5' => '101010101',
+ '6' => '11101010101',
+ '7' => '1110111010101',
+ '8' => '111011101110101',
+ '9' => '11101110111011101',
+ '0' => '1110111011101110111',
+);
+
+sub morse_encode {
+ my ($message) = @_;
+
+ my @words = split /\W/, $message;
+
+ for my $word (@words) {
+ my @chars = split q{}, $word;
+ for my $c (@chars) {
+ if (exists $to_morse{uc $c}) {
+ $c = $to_morse{uc $c};
+ }
+ }
+ $word = join CHARACTER_GAP, @chars;
+ }
+
+ return join WORD_GAP, @words;
+}
+
+say morse_encode(join q{ }, @ARGV);
diff --git a/challenge-035/jaldhar-h-vyas/perl/ch-2.pl b/challenge-035/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..108deaa7fa
--- /dev/null
+++ b/challenge-035/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+use constant CHARACTER_GAP => '000';
+use constant WORD_GAP => '000000';
+
+my %from_morse = (
+ '10111' => 'A',
+ '111010101' => 'B',
+ '11101011101' => 'C',
+ '1110101' => 'D',
+ '1' => 'E',
+ '101011101' => 'F',
+ '111011101' => 'G',
+ '1010101' => 'H',
+ '101' => 'I',
+ '1011101110111' => 'J',
+ '111010111' => 'K',
+ '101110101' => 'L',
+ '1110111' => 'M',
+ '11101' => 'N',
+ '11101110111' => 'O',
+ '10111011101' => 'P',
+ '1110111010111' => 'Q',
+ '1011101' => 'R',
+ '10101' => 'S',
+ '111' => 'T',
+ '1010111' => 'U',
+ '101010111' => 'V',
+ '101110111' => 'W',
+ '11101010111' => 'X',
+ '1110101110111' => 'Y',
+ '11101110101' => 'Z',
+ '10111011101110111' => '1',
+ '101011101110111' => '2',
+ '1010101110111' => '3',
+ '10101010111' => '4',
+ '101010101' => '5',
+ '11101010101' => '6',
+ '1110111010101' => '7',
+ '111011101110101' => '8',
+ '11101110111011101' => '9',
+ '1110111011101110111' => '0',
+);
+
+sub morse_decode {
+ my ($message) = @_;
+
+
+ my @words = split WORD_GAP, $message;
+
+ for my $word (@words) {
+ my @chars = split CHARACTER_GAP, $word;
+ for my $c (@chars) {
+ if (exists $from_morse{$c}) {
+ $c = $from_morse{$c};
+ }
+ }
+ $word = join q{}, @chars;
+ }
+
+ return join q{ }, @words;
+}
+
+say morse_decode(join q{}, @ARGV);
diff --git a/challenge-035/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-035/jaldhar-h-vyas/raku/ch-1.p6
new file mode 100755
index 0000000000..48b196c0f1
--- /dev/null
+++ b/challenge-035/jaldhar-h-vyas/raku/ch-1.p6
@@ -0,0 +1,64 @@
+#!/usr/bin/perl6
+
+constant $CHARACTER_GAP = '000';
+constant $WORD_GAP = '000000';
+
+my %to_morse = (
+ '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',
+ '1' => '10111011101110111',
+ '2' => '101011101110111',
+ '3' => '1010101110111',
+ '4' => '10101010111',
+ '5' => '101010101',
+ '6' => '11101010101',
+ '7' => '1110111010101',
+ '8' => '111011101110101',
+ '9' => '11101110111011101',
+ '0' => '1110111011101110111',
+);
+
+sub morse_encode(Str $message) {
+
+ my @words = split /\W/, $message;
+
+ for @words <-> $word {
+ my @chars = $word.comb;
+ for @chars <-> $c {
+ if %to_morse{uc $c}:exists {
+ $c = %to_morse{uc $c};
+ }
+ }
+ $word = @chars.join($CHARACTER_GAP);
+ }
+
+ return @words.join($WORD_GAP);
+}
+
+sub MAIN(*@ARGS) {
+ say morse_encode(@*ARGS.join(q{ }));
+}
diff --git a/challenge-035/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-035/jaldhar-h-vyas/raku/ch-2.p6
new file mode 100755
index 0000000000..d5c518e174
--- /dev/null
+++ b/challenge-035/jaldhar-h-vyas/raku/ch-2.p6
@@ -0,0 +1,64 @@
+#!/usr/bin/perl6
+
+constant $CHARACTER_GAP = '000';
+constant $WORD_GAP = '000000';
+
+my %from_morse = (
+ '10111' => 'A',
+ '111010101' => 'B',
+ '11101011101' => 'C',
+ '1110101' => 'D',
+ '1' => 'E',
+ '101011101' => 'F',
+ '111011101' => 'G',
+ '1010101' => 'H',
+ '101' => 'I',
+ '1011101110111' => 'J',
+ '111010111' => 'K',
+ '101110101' => 'L',
+ '1110111' => 'M',
+ '11101' => 'N',
+ '11101110111' => 'O',
+ '10111011101' => 'P',
+ '1110111010111' => 'Q',
+ '1011101' => 'R',
+ '10101' => 'S',
+ '111' => 'T',
+ '1010111' => 'U',
+ '101010111' => 'V',
+ '101110111' => 'W',
+ '11101010111' => 'X',
+ '1110101110111' => 'Y',
+ '11101110101' => 'Z',
+ '10111011101110111' => '1',
+ '101011101110111' => '2',
+ '1010101110111' => '3',
+ '10101010111' => '4',
+ '101010101' => '5',
+ '11101010101' => '6',
+ '1110111010101' => '7',
+ '111011101110101' => '8',
+ '11101110111011101' => '9',
+ '1110111011101110111' => '0',
+);
+
+sub morse_decode(Str $message) {
+
+ my @words = $message.split($WORD_GAP);
+
+ for @words <-> $word {
+ my @chars = $word.split($CHARACTER_GAP);
+ for @chars <-> $c {
+ if %from_morse{$c}:exists {
+ $c = %from_morse{$c};
+ }
+ }
+ $word = @chars.join;
+ }
+
+ return @words.join(q{ });
+}
+
+sub MAIN(*@ARGS) {
+ say morse_decode(@*ARGS.join());
+}