aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-02-26 18:27:28 +0000
committerGitHub <noreply@github.com>2020-02-26 18:27:28 +0000
commit2d7574823cefd1e7a11072776b784fa88fe0fb52 (patch)
treec0a212d8299d2d93f4de77102f027a95ee24596f
parente2664a3f4c9d187c2d5f8559048b9af0f2e66550 (diff)
parent2640a0d64155ceb076579a0ad96f99a32c1a59aa (diff)
downloadperlweeklychallenge-club-2d7574823cefd1e7a11072776b784fa88fe0fb52.tar.gz
perlweeklychallenge-club-2d7574823cefd1e7a11072776b784fa88fe0fb52.tar.bz2
perlweeklychallenge-club-2d7574823cefd1e7a11072776b784fa88fe0fb52.zip
Merge pull request #1315 from Doomtrain14/master
Hello
-rw-r--r--challenge-034/yet-ebreo/perl5/ch-1.pl28
-rw-r--r--challenge-034/yet-ebreo/perl5/ch-2.pl41
-rw-r--r--challenge-035/yet-ebreo/perl5/ch-1.pl77
-rw-r--r--challenge-035/yet-ebreo/perl5/ch-2.pl68
4 files changed, 214 insertions, 0 deletions
diff --git a/challenge-034/yet-ebreo/perl5/ch-1.pl b/challenge-034/yet-ebreo/perl5/ch-1.pl
new file mode 100644
index 0000000000..b9aabf6867
--- /dev/null
+++ b/challenge-034/yet-ebreo/perl5/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+my @array = qw(a quick brown fox jumps over the lazy dog);
+
+#Using index
+say $array[4];
+
+#Using multiple indices
+my @sliced_array = @array[2,3];
+say @sliced_array;
+
+#Using range
+@sliced_array = @array[2..6];
+
+say @sliced_array;
+
+my $n = 0;
+my %hash = map { $_ => $n++ } @array;
+
+#Using one key
+say $hash{'jumps'};
+
+#Using array as keys
+say @hash{qw(jumps dog a)}; \ No newline at end of file
diff --git a/challenge-034/yet-ebreo/perl5/ch-2.pl b/challenge-034/yet-ebreo/perl5/ch-2.pl
new file mode 100644
index 0000000000..b0c253f846
--- /dev/null
+++ b/challenge-034/yet-ebreo/perl5/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+my %table = (
+ add => \&add,
+ subtract => \&subtract,
+ multiply => \&multiply,
+ divide => \&divide
+);
+
+sub divide {
+ my ($m,$n) = @_;
+ say "Error: Divide by 0" if !$n;
+
+ return $m/$n;
+}
+
+sub add {
+ my ($m,$n) = @_;
+ return $m+$n;
+}
+
+sub subtract {
+ my ($m,$n) = @_;
+ return $m-$n;
+}
+
+sub multiply {
+ my ($m,$n) = @_;
+ return $m*$n;
+}
+
+my $commands = "add multiply subtract divide";
+my @operands = (43,6);
+for my $cmd ($commands=~/\w+/g) {
+ say "[$cmd]";
+ say $table{$cmd}->(@operands);
+} \ No newline at end of file
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