diff options
| author | Ryan Thompson <i@ry.ca> | 2020-03-01 04:07:41 -0600 |
|---|---|---|
| committer | Ryan Thompson <i@ry.ca> | 2020-03-01 04:07:41 -0600 |
| commit | ea66028356d5cd528de341f00b30d1f84840c127 (patch) | |
| tree | 3a8cdcba2cb0c50af235def97eca186c5533df81 | |
| parent | 712204bb937f97cc022c5adb9852d22e07a245f8 (diff) | |
| parent | fecba96965f6f1d10e7ad8b3628c6891ab1872af (diff) | |
| download | perlweeklychallenge-club-ea66028356d5cd528de341f00b30d1f84840c127.tar.gz perlweeklychallenge-club-ea66028356d5cd528de341f00b30d1f84840c127.tar.bz2 perlweeklychallenge-club-ea66028356d5cd528de341f00b30d1f84840c127.zip | |
Merge remote-tracking branch 'upstream/master' into rjt_049
80 files changed, 5632 insertions, 2326 deletions
@@ -2,6 +2,10 @@ This is the central repository for the members of [**Perl Weekly Challenge**](https://perlweeklychallenge.org). The members can submit the solution to the challenge each week under version control. +## EZPWC - Easy Perl Weekly Challenges Script + +**Saif Ahmed**, respected member of **Team PWC**, created the tool **[EZPWC](https://github.com/saiftynet/EZPWC)** to help you with contributing to the weekly challenge. We highly recommend you to give it a try. If you have any questions/suggestions, then please raise an issue against the tool. + ## How to contribute? Just submit Pull Request with your solutions. 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 => \÷ +); + +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 diff --git a/challenge-036/yet-ebreo/perl5/ch-1.pl b/challenge-036/yet-ebreo/perl5/ch-1.pl new file mode 100644 index 0000000000..94425694a1 --- /dev/null +++ b/challenge-036/yet-ebreo/perl5/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +if (@ARGV != 1) { + say "Usage:\n\tperl ch-1.pl <VIN>\n\n"; +} else { + my $vin = $ARGV[0]; + if ($vin =~ /[OIQ]/i) { + say "Invalid VIN"; + } else { + if ($vin =~ /^[A-Z0-9]{17}$/i) { + say "VIN is valid"; + } else { + say "Invalid VIN"; + } + } +}
\ No newline at end of file diff --git a/challenge-036/yet-ebreo/perl5/ch-2.pl b/challenge-036/yet-ebreo/perl5/ch-2.pl new file mode 100644 index 0000000000..0f207404be --- /dev/null +++ b/challenge-036/yet-ebreo/perl5/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +my %boxes = ( + R => {w => 1, v => 1}, + B => {w => 1, v => 2}, + G => {w => 2, v => 2}, + Y => {w => 12, v => 4}, + P => {w => 4, v => 10} +); + +if (@ARGV < 2) { + die "Usage:\n\tperl ch-2.pl <max_item> <max_weight>\n\n"; +} +my $max_item = $ARGV[0]; +my $max_weight = $ARGV[1]; + +my @g_keys = keys %boxes; +my @g_wt = map { $boxes{$_}{w} } @g_keys; +my @g_val = map { $boxes{$_}{v} } @g_keys; +my $max = 0; +my $mask; + +for my $bitmask (1..2**@g_keys) { + + my ($c_weight,$c_value,$c_item_cnt) = (0) x 3; + + for my $p (0..~-@g_keys) { + if ($bitmask & 1<<$p) { + + if ((++$c_item_cnt <= $max_item) && ($c_weight + $boxes{$g_keys[$p]}{w} <= $max_weight)) { + $c_value += $boxes{$g_keys[$p]}{v}; + $c_weight += $boxes{$g_keys[$p]}{w}; + } + } + } + if ($c_value > $max) { + $max = $c_value; + $mask = $bitmask; + } +} + +say "Color Value Weight"; +map { ($mask & 1 << $_) && printf ("$g_keys[$_] %02d %02d\n", $boxes{$g_keys[$_]}{v}, $boxes{$g_keys[$_]}{w}) } (0..~-@g_keys); +say "Max Value: $max"; +=begin +perl .\ch-2.pl 2 15 +Color Value Weight +B 02 01 +P 10 04 +Max Value: 12 + +perl .\ch-2.pl 5 15 +Color Value Weight +G 02 02 +B 02 01 +P 10 04 +R 01 01 +Max Value: 15 + +perl .\ch-2.pl 1 15 +Color Value Weight +P 10 04 +Max Value: 10 +=cut
\ No newline at end of file diff --git a/challenge-038/jaldhar-h-vyas/blog.txt b/challenge-038/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..8885c6d42b --- /dev/null +++ b/challenge-038/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2020/02/perl_weekly_challenge_week_38.html diff --git a/challenge-038/jaldhar-h-vyas/perl/ch-1.pl b/challenge-038/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..dd9ebd39b1 --- /dev/null +++ b/challenge-038/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +sub isLeap { + my ($year) = @_; + return $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0); +} + +sub isValidDate { + my ($year, $month, $day) = @_; + + if ($month == 2 && $day > (isLeap($year) ? 29 : 28)) { + return undef; + } + + if ((grep { $month == $_ } ( 4, 6, 9, 11 )) && $day > 30) { + return undef; + } + + return 1; +} + +my $input = shift // q{}; + +if ($input =~ m{ + \A + (?<century> [12]) + (?<year> [0-9]{2}) + (?<month> (0[1-9]) | (1[0-2]) ) + (?<day> (0[1-9]) | ([1-2][0-9]) | (3[01])) + \z +}gmx) { + my $year = (($+{century} == 1) ? 20 : 19) . $+{year}; + if (isValidDate($year, $+{month}, $+{day})) { + say join q{-}, ($year, $+{month}, $+{day}); + exit; + } +} + +say 'Invalid date.';
\ No newline at end of file diff --git a/challenge-038/jaldhar-h-vyas/perl/ch-2.pl b/challenge-038/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..4710c7b080 --- /dev/null +++ b/challenge-038/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,117 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; +use English qw/ -no_match_vars /; + +sub combinations { + my @list = @{$_[0]}; + my $length = $_[1]; + + if ($length <= 1) { + return map [$_], @list; + } + + my @combos; + + for (my $i = 0; $i + $length <= scalar @list; $i++) { + my $val = $list[$i]; + my @rest = @list[$i + 1 .. $#list]; + for my $c (combinations(\@rest, $length - 1)) { + push @combos, [$val, @{$c}] ; + } + } + + return @combos; |
