aboutsummaryrefslogtreecommitdiff
path: root/challenge-035
diff options
context:
space:
mode:
authorYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-02-27 00:38:29 +0800
committerYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-02-27 00:38:29 +0800
commit2640a0d64155ceb076579a0ad96f99a32c1a59aa (patch)
tree733b214ba1bfbe7ea9d3934034618b736f346c49 /challenge-035
parent65a833d89f6ee365237cbcd14d1391d3d703cd1d (diff)
downloadperlweeklychallenge-club-2640a0d64155ceb076579a0ad96f99a32c1a59aa.tar.gz
perlweeklychallenge-club-2640a0d64155ceb076579a0ad96f99a32c1a59aa.tar.bz2
perlweeklychallenge-club-2640a0d64155ceb076579a0ad96f99a32c1a59aa.zip
I know it's been a while
Diffstat (limited to 'challenge-035')
-rw-r--r--challenge-035/yet-ebreo/perl5/ch-1.pl77
-rw-r--r--challenge-035/yet-ebreo/perl5/ch-2.pl68
2 files changed, 145 insertions, 0 deletions
diff --git a/challenge-035/yet-ebreo/perl5/ch-1.pl b/challenge-035/yet-ebreo/perl5/ch-1.pl
new file mode 100644
index 0000000000..4eaa827c8a
--- /dev/null
+++ b/challenge-035/yet-ebreo/perl5/ch-1.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+# Morse Code Char Set
+# dot: 1
+# dash: 111
+# intra-character gap: 0
+# character gap: 000
+# word gap: 0000000
+
+my %m_encode = (
+ 0=>489335,
+ 1=>96119,
+ 2=>22391,
+ 3=>5495,
+ 4=>1367,
+ 5=>341,
+ 6=>1877,
+ 7=>7637,
+ 8=>30581,
+ 9=>122333,
+ A=>23,
+ B=>469,
+ C=>1885,
+ D=>117,
+ E=>1,
+ F=>349,
+ G=>477,
+ H=>85,
+ I=>5,
+ J=>6007,
+ K=>471,
+ L=>373,
+ M=>119,
+ N=>29,
+ O=>1911,
+ P=>1501,
+ Q=>7639,
+ R=>93,
+ S=>21,
+ T=>7,
+ U=>87,
+ V=>343,
+ W=>375,
+ X=>1879,
+ Y=>7543,
+ Z=>1909
+);
+
+my $string_to_encode = $ARGV[0] || "A QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
+
+sub encode {
+ my $string = uc pop;
+ return $string =~
+ #Insert # in between letters using \b
+ s/\B/#/gr =~
+ #Replace space which separate words, with @
+ s/ /@/gr =~
+ #Replace alphanumeric + _ matches with the morsecode from hash table
+ s/\w/sprintf "%b", $m_encode{$&}/gre =~
+ #Replace # with the assigned character gap
+ s/#/000/gr =~
+ #Replace @ with the assigned word gap
+ s/@/0000000/gr ;
+}
+
+my $encoded_string = encode($string_to_encode);
+
+say "Encoded Morse Code: ".$encoded_string;
+
+=begin
+perl .\ch-1.pl "just another perl hacker"
+Encoded Morse Code: 101110111011100010101110001010100011100000001011100011101000111011101110001110001010101000100010111010000000101110111010001000101110100010111010100000001010101000101110001110101110100011101011100010001011101
+=cut \ No newline at end of file
diff --git a/challenge-035/yet-ebreo/perl5/ch-2.pl b/challenge-035/yet-ebreo/perl5/ch-2.pl
new file mode 100644
index 0000000000..d8f3f39e57
--- /dev/null
+++ b/challenge-035/yet-ebreo/perl5/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+my %m_decode = (
+ "489335" =>"0",
+ "96119" =>"1",
+ "22391" =>"2",
+ "5495" =>"3",
+ "1367" =>"4",
+ "341" =>"5",
+ "1877" =>"6",
+ "7637" =>"7",
+ "30581" =>"8",
+ "122333" =>"9",
+ "23" =>"A",
+ "469" =>"B",
+ "1885" =>"C",
+ "117" =>"D",
+ "1" =>"E",
+ "349" =>"F",
+ "477" =>"G",
+ "85" =>"H",
+ "5" =>"I",
+ "6007" =>"J",
+ "471" =>"K",
+ "373" =>"L",
+ "119" =>"M",
+ "29" =>"N",
+ "1911" =>"O",
+ "1501" =>"P",
+ "7639" =>"Q",
+ "93" =>"R",
+ "21" =>"S",
+ "7" =>"T",
+ "87" =>"U",
+ "343" =>"V",
+ "375" =>"W",
+ "1879" =>"X",
+ "7543" =>"Y",
+ "1909" =>"Z"
+);
+
+my $string_to_decode = $ARGV[0] || "10111000000011101110101110001010111000101000111010111010001110101110000000111010101000101110100011101110111000101110111000111010000000101011101000111011101110001110101011100000001011101110111000101011100011101110001011101110100010101000000011101110111000101010111000100010111010000000111000101010100010000000101110101000101110001110111010100011101011101110000000111010100011101110111000111011101";
+
+sub decode {
+ my $string = pop =~
+ #Replace the assigned word gap with @
+ s/0000000/@/gr =~
+ #Replace the assigned char gap with #
+ s/000/#/gr =~
+ #Replace morse code with equivalent char from hash
+ s/[01]+/$m_decode{oct "b$&"}/gre =~
+ #Replace @ with space
+ s/@/ /gr =~
+ #Remove #'s
+ s/#//gr;
+ return $string
+}
+
+say "Decoded String: ".decode($string_to_decode);
+
+=begin
+perl .\ch-2.pl 101110111011100010101110001010100011100000001011100011101000111011101110001110001010101000100010111010000000101110111010001000101110100010111010100000001010101000101110001110101110100011101011100010001011101
+Decoded String: JUST ANOTHER PERL HACKER
+=cut \ No newline at end of file