aboutsummaryrefslogtreecommitdiff
path: root/challenge-035
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-11-23 01:18:37 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-11-23 01:18:37 +0000
commitf18e0d2b4c9e700e5e2c8380e6255d30d2f03176 (patch)
treecd80e050fc9e45b06403e5eb11819cc06ec33172 /challenge-035
parenta2b52efb745d603cff76f102098af3b7a79d5e51 (diff)
downloadperlweeklychallenge-club-f18e0d2b4c9e700e5e2c8380e6255d30d2f03176.tar.gz
perlweeklychallenge-club-f18e0d2b4c9e700e5e2c8380e6255d30d2f03176.tar.bz2
perlweeklychallenge-club-f18e0d2b4c9e700e5e2c8380e6255d30d2f03176.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-035')
-rw-r--r--challenge-035/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-035/laurent-rosenfeld/perl5/ch-1.pl43
-rw-r--r--challenge-035/laurent-rosenfeld/perl5/ch-2.pl43
-rw-r--r--challenge-035/laurent-rosenfeld/perl5/morse.dat36
-rw-r--r--challenge-035/laurent-rosenfeld/perl6/ch-1.p632
-rw-r--r--challenge-035/laurent-rosenfeld/perl6/ch-2.p632
-rw-r--r--challenge-035/laurent-rosenfeld/perl6/morse.dat36
7 files changed, 223 insertions, 0 deletions
diff --git a/challenge-035/laurent-rosenfeld/blog.txt b/challenge-035/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..d0c58f1ae6
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2019/11/perl-weekly-challenge-35-binary-encoded-morse-code.html
diff --git a/challenge-035/laurent-rosenfeld/perl5/ch-1.pl b/challenge-035/laurent-rosenfeld/perl5/ch-1.pl
new file mode 100644
index 0000000000..01622bbc81
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/perl5/ch-1.pl
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+use feature qw/say/;
+use Data::Dumper;
+
+my %bin_chars = ( '.' => 1, '_' => 111);
+my $morse_codes_file = "morse.dat";
+open my $IN_MORSE, "<", $morse_codes_file or die "Cannot open $morse_codes_file $!";
+my %bin_morse =
+ map { chomp;
+ my ($key, $val) = split ":", $_ ;
+ my $bin_val = join '0', map $bin_chars{$_}, split //, $val;
+ $key => $bin_val;
+ } <$IN_MORSE>;
+
+my $input = shift // "The quick brown fox jumps over the lazy dog";
+
+sub to_morse {
+ my $input = uc shift; # Morse doesn't have cases
+ $input =~ s/[^A-Z0-9 ]//g; # remove non Morse characters
+ my @morse_words;
+ for my $word (split / /, $input) {
+ push @morse_words, join '000', map { $bin_morse{$_} } split //, $word;
+ }
+ return join '0000000', @morse_words;
+}
+sub from_morse {
+ my $input = shift;
+ my %rev_bin_morse = reverse %bin_morse;
+ my @words;
+ for my $word (split /0{7}/, $input) {
+ push @words, join '', map $rev_bin_morse{$_}, split /000/, $word;
+ }
+ return join " ", @words;
+}
+sub format80c {
+ shift =~ s/(.{80})/$1\n/gr;
+}
+
+say "Input string: $input";
+my $encoded = to_morse $input;
+say "Binary encoded Morse string:\n", format80c $encoded;
+say "Decoded string: ", from_morse $encoded;
diff --git a/challenge-035/laurent-rosenfeld/perl5/ch-2.pl b/challenge-035/laurent-rosenfeld/perl5/ch-2.pl
new file mode 100644
index 0000000000..01622bbc81
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/perl5/ch-2.pl
@@ -0,0 +1,43 @@
+use strict;
+use warnings;
+use feature qw/say/;
+use Data::Dumper;
+
+my %bin_chars = ( '.' => 1, '_' => 111);
+my $morse_codes_file = "morse.dat";
+open my $IN_MORSE, "<", $morse_codes_file or die "Cannot open $morse_codes_file $!";
+my %bin_morse =
+ map { chomp;
+ my ($key, $val) = split ":", $_ ;
+ my $bin_val = join '0', map $bin_chars{$_}, split //, $val;
+ $key => $bin_val;
+ } <$IN_MORSE>;
+
+my $input = shift // "The quick brown fox jumps over the lazy dog";
+
+sub to_morse {
+ my $input = uc shift; # Morse doesn't have cases
+ $input =~ s/[^A-Z0-9 ]//g; # remove non Morse characters
+ my @morse_words;
+ for my $word (split / /, $input) {
+ push @morse_words, join '000', map { $bin_morse{$_} } split //, $word;
+ }
+ return join '0000000', @morse_words;
+}
+sub from_morse {
+ my $input = shift;
+ my %rev_bin_morse = reverse %bin_morse;
+ my @words;
+ for my $word (split /0{7}/, $input) {
+ push @words, join '', map $rev_bin_morse{$_}, split /000/, $word;
+ }
+ return join " ", @words;
+}
+sub format80c {
+ shift =~ s/(.{80})/$1\n/gr;
+}
+
+say "Input string: $input";
+my $encoded = to_morse $input;
+say "Binary encoded Morse string:\n", format80c $encoded;
+say "Decoded string: ", from_morse $encoded;
diff --git a/challenge-035/laurent-rosenfeld/perl5/morse.dat b/challenge-035/laurent-rosenfeld/perl5/morse.dat
new file mode 100644
index 0000000000..e462feddcc
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/perl5/morse.dat
@@ -0,0 +1,36 @@
+0:_____
+1:.____
+2:..___
+3:...__
+4:...._
+5:.....
+6:_....
+7:__...
+8:___..
+9:____.
+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:__..
diff --git a/challenge-035/laurent-rosenfeld/perl6/ch-1.p6 b/challenge-035/laurent-rosenfeld/perl6/ch-1.p6
new file mode 100644
index 0000000000..ce601b3155
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/perl6/ch-1.p6
@@ -0,0 +1,32 @@
+use v6;
+
+sub MAIN ($input = "The quick brown fox jumps over the lazy dog") {
+ my %bin_chars = '.' => 1, '_' => 111;
+ my %*bin-morse = "morse.dat".IO.lines.map({
+ my ($key, $val) = split ":", $_ ;
+ my $bin_val = $val.comb.map({%bin_chars{$_}}).join('0');
+ $key => $bin_val;
+ });
+ say "Input string: $input";
+ my $encoded = to_morse $input;
+ say "Binary encoded Morse string:";
+ my $encoded-copy = $encoded;
+ .Str.say for $encoded-copy ~~ s:g/(. ** 1..80)/$0\n/;
+ say "Decoded string: ", from_morse $encoded;
+}
+sub to_morse ($input is copy) {
+ $input ~~ s:i:g/<-[A..Z0..9\s.,?'!;]>//; # remove non Morse characters
+ my @morse_words;
+ for $input.uc.split(/\s/) -> $word {
+ push @morse_words, join '000', map { %*bin-morse{$_} }, $word.comb;
+ }
+ return join '0000000', @morse_words;
+}
+sub from_morse ($input) {
+ my %rev_bin-morse = reverse %*bin-morse.kv;
+ my @words;
+ for split /0 ** 7/, $input -> $word {
+ push @words, join '', map {%rev_bin-morse{$_}}, split /000/, $word;
+ }
+ return join " ", @words;
+}
diff --git a/challenge-035/laurent-rosenfeld/perl6/ch-2.p6 b/challenge-035/laurent-rosenfeld/perl6/ch-2.p6
new file mode 100644
index 0000000000..ce601b3155
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/perl6/ch-2.p6
@@ -0,0 +1,32 @@
+use v6;
+
+sub MAIN ($input = "The quick brown fox jumps over the lazy dog") {
+ my %bin_chars = '.' => 1, '_' => 111;
+ my %*bin-morse = "morse.dat".IO.lines.map({
+ my ($key, $val) = split ":", $_ ;
+ my $bin_val = $val.comb.map({%bin_chars{$_}}).join('0');
+ $key => $bin_val;
+ });
+ say "Input string: $input";
+ my $encoded = to_morse $input;
+ say "Binary encoded Morse string:";
+ my $encoded-copy = $encoded;
+ .Str.say for $encoded-copy ~~ s:g/(. ** 1..80)/$0\n/;
+ say "Decoded string: ", from_morse $encoded;
+}
+sub to_morse ($input is copy) {
+ $input ~~ s:i:g/<-[A..Z0..9\s.,?'!;]>//; # remove non Morse characters
+ my @morse_words;
+ for $input.uc.split(/\s/) -> $word {
+ push @morse_words, join '000', map { %*bin-morse{$_} }, $word.comb;
+ }
+ return join '0000000', @morse_words;
+}
+sub from_morse ($input) {
+ my %rev_bin-morse = reverse %*bin-morse.kv;
+ my @words;
+ for split /0 ** 7/, $input -> $word {
+ push @words, join '', map {%rev_bin-morse{$_}}, split /000/, $word;
+ }
+ return join " ", @words;
+}
diff --git a/challenge-035/laurent-rosenfeld/perl6/morse.dat b/challenge-035/laurent-rosenfeld/perl6/morse.dat
new file mode 100644
index 0000000000..e462feddcc
--- /dev/null
+++ b/challenge-035/laurent-rosenfeld/perl6/morse.dat
@@ -0,0 +1,36 @@
+0:_____
+1:.____
+2:..___
+3:...__
+4:...._
+5:.....
+6:_....
+7:__...
+8:___..
+9:____.
+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:__..