diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-20 13:36:48 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-20 13:36:48 +0100 |
| commit | 6ea7c0a2ebb198c4d2149ddbdbf5e1f4cb4fa627 (patch) | |
| tree | 5edd5525aa2c08b669528a9c6ce285966e5c3004 | |
| parent | d5b566e5ccf3e946c53d5c92c90b43af27303699 (diff) | |
| parent | 190862678a9c8f26d213d656c1eb50b8f372653d (diff) | |
| download | perlweeklychallenge-club-6ea7c0a2ebb198c4d2149ddbdbf5e1f4cb4fa627.tar.gz perlweeklychallenge-club-6ea7c0a2ebb198c4d2149ddbdbf5e1f4cb4fa627.tar.bz2 perlweeklychallenge-club-6ea7c0a2ebb198c4d2149ddbdbf5e1f4cb4fa627.zip | |
Merge pull request #5968 from pauloscustodio/master
Add Perl solutions
53 files changed, 1245 insertions, 0 deletions
diff --git a/challenge-067/paulo-custodio/Makefile b/challenge-067/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-067/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-067/paulo-custodio/README b/challenge-067/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-067/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-067/paulo-custodio/perl/ch-1.pl b/challenge-067/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..2a0587b5df --- /dev/null +++ b/challenge-067/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +# Challenge 067 +# +# TASK #1 Number Combinations +# Submitted by: Mohammad S Anwar +# +# You are given two integers $m and $n. Write a script print all possible +# combinations of $n numbers from the list 1 2 3
$m. +# +# Every combination should be sorted i.e. [2,3] is valid combination but [3,2] is not. +# +# Example: +# Input: $m = 5, $n = 2 +# +# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] + +use Modern::Perl; +use Math::Combinatorics 'combine'; + +my($m, $n) = @ARGV; +my @m = (1..$m); +my %out; +for (combine($n, @m)) { + $out{"[".join(",", sort @$_)."]"} = 1; +} +say "[ ", join(", ", sort keys %out), " ]"; diff --git a/challenge-067/paulo-custodio/perl/ch-2.pl b/challenge-067/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..bd12104b1f --- /dev/null +++ b/challenge-067/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +# Challenge 067 +# +# TASK #2 Letter Phone +# Submitted by: Mohammad S Anwar +# +# You are given a digit string $S. Write a script to print all possible letter +# combinations that the given digit string could represent. +# +# Letter Phone +# +# +# Example: +# Input: $S = '35' +# +# Output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"]. + +use Modern::Perl; +use Iterator::Array::Jagged; + +my %digits = ( + 1 => ['_', ',', '@'], + 2 => [qw( a b c )], + 3 => [qw( d e f )], + 4 => [qw( g h i )], + 5 => [qw( j k l )], + 6 => [qw( m n o )], + 7 => [qw( p q r s )], + 8 => [qw( t u v )], + 9 => [qw( w x y z )], + '*' => [' '], +); + +my $s = shift||""; +my @out = letter_phone($s); +say "[", join(", ", map {'"'.$_.'"'} @out), "]"; + +sub letter_phone { + my($s) = @_; + my @s = map {$digits{$_}} split(//, $s); + my $it = Iterator::Array::Jagged->new(data => \@s); + my @out; + while (my @set = $it->next) { + push @out, join("", @set); + } + return sort @out; +} diff --git a/challenge-067/paulo-custodio/t/test-1.yaml b/challenge-067/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..4ed336268c --- /dev/null +++ b/challenge-067/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 5 2 + input: + output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] diff --git a/challenge-067/paulo-custodio/t/test-2.yaml b/challenge-067/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..699383fa69 --- /dev/null +++ b/challenge-067/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 35 + input: + output: ["dj", "dk", "dl", "ej", "ek", "el", "fj", "fk", "fl"] diff --git a/challenge-068/paulo-custodio/Makefile b/challenge-068/paulo-custodio/Makefile new file mode 100644 index 0000000000..ba9a2bf399 --- /dev/null +++ b/challenge-068/paulo-custodio/Makefile @@ -0,0 +1,3 @@ +all: + perl perl/ch-1.pl + perl perl/ch-2.pl diff --git a/challenge-068/paulo-custodio/README b/challenge-068/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-068/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-068/paulo-custodio/perl/ch-1.pl b/challenge-068/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..e7279cec76 --- /dev/null +++ b/challenge-068/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl + +# Challenge 068 +# +# TASK #1 Zero Matrix +# Submitted by: Mohammad S Anwar +# You are given a matrix of size M x N having only 0s and 1s. +# +# Write a script to set the entire row and column to 0 if an element is 0. +# +# Example 1 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 1, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [1, 0, 1] +# Example 2 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 0, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [0, 0, 0] + +use Modern::Perl; +use Clone 'clone'; +use Test::More; + +is_deeply [zero_matrix([1,0,1], + [1,1,1], + [1,1,1])], + [[0,0,0], + [1,0,1], + [1,0,1]]; + +is_deeply [zero_matrix([1,0,1], + [1,1,1], + [1,0,1])], + [[0,0,0], + [1,0,1], + [0,0,0]]; + +done_testing; + + +sub zero_matrix { + my(@m) = @_; + my $orig = clone(\@m); + for my $r (0 .. @m-1) { + for my $c (0 .. @{$m[0]}-1) { + if (!$orig->[$r][$c]) { + @m = zero_row($r, @m); + @m = zero_col($c, @m); + } + } + } + return @m; +} + +sub zero_row { + my($r, @m) = @_; + for my $c (0 .. @{$m[0]}-1) { + $m[$r][$c] = 0; + } + return @m; +} + +sub zero_col { + my($c, @m) = @_; + for my $r (0 .. @m-1) { + $m[$r][$c] = 0; + } + return @m; +} diff --git a/challenge-068/paulo-custodio/perl/ch-2.pl b/challenge-068/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b4730ddce8 --- /dev/null +++ b/challenge-068/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# Challenge 068 +# +# TASK #2 Reorder List +# Submitted by: Mohammad S Anwar +# You are given a singly linked list $L as below: +# +# L0 ? L1 ?
? Ln-1 ? Ln +# Write a script to reorder list as below: +# +# L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? +# You are ONLY allowed to do this in-place without altering the nodes values. +# +# Example +# Input: 1 ? 2 ? 3 ? 4 +# Output: 1 ? 4 ? 2 ? 3 + +use Modern::Perl; +use Test::More; + +is_deeply reorder_list([1,[2,[3,[4]]]]), [1,[4,[2,[3]]]]; +done_testing; + +sub reorder_list { + my($l) = @_; + # get second element + my $tail = $l->[1]; + # get and remove last element + my $p = $tail; + my $last; + while ($p->[1]) { + $last = $p; + $p = $p->[1]; + } + my $eln = pop(@{$last}); + + return [$l->[0], [$eln->[0], $tail]]; +} diff --git a/challenge-069/paulo-custodio/Makefile b/challenge-069/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-069/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-069/paulo-custodio/README b/challenge-069/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-069/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-069/paulo-custodio/perl/ch-1.pl b/challenge-069/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..1bbbf03f2e --- /dev/null +++ b/challenge-069/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# Challenge 069 +# +# TASK #1 Strobogrammatic Number +# Submitted by: Mohammad S Anwar +# A strobogrammatic number is a number that looks the same when looked at upside +# down. +# +# You are given two positive numbers $A and $B such that 1 <= $A <= $B <= 10^15. +# +# Write a script to print all strobogrammatic numbers between the given two +# numbers. +# +# Example +# Input: $A = 50, $B = 100 +# Output: 69, 88, 96 + +use Modern::Perl; + +my($A, $B) = @ARGV; +my @out; +for my $n ($A .. $B) { + push @out, $n if is_strobogrammatic($n); +} +say join(", ", @out); + +sub is_strobogrammatic { + my($n) = @_; + $n =~ /^[0689]+$/ or return 0; + (my $inv = $n) =~ tr/69/96/; + return 1 if reverse($inv)==$n; + return 0; +} diff --git a/challenge-069/paulo-custodio/perl/ch-2.pl b/challenge-069/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..699d9f7240 --- /dev/null +++ b/challenge-069/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +# Challenge 069 +# +# TASK #2 0/1 String +# Submitted by: Mohammad S Anwar +# A 0/1 string is a string in which every character is either 0 or 1. +# +# Write a script to perform switch and reverse to generate S30 as described +# below: +# +# switch: +# +# Every 0 becomes 1 and every 1 becomes 0. For example, 101 becomes 010. +# +# reverse: +# +# The string is reversed. For example, "001 becomes 100. +# UPDATE (2020-07-13 17:00:00): +# It was brought to my notice that generating S1000 string would be nearly +# impossible. So I have decided to lower it down to S30. Please follow the rule +# as below: +# +# S0 = +# S1 = 0 +# S2 = 001 +# S3 = 0010011 +# +# SN = SN-1 + 0 + switch(reverse(SN-1)) + +# Note: modified to S20, as S30 was taking forever + +use Modern::Perl; + +my $N = 20; + +sub bits_switch { + my($s) = @_; + $s =~ tr/01/10/; + return $s; +} + +sub bits_reverse { + my($s) = @_; + return join('', reverse split('', $s)); +} + +my $prev = ""; +my $s; +for (1..$N) { + $s = $prev."0".bits_switch(bits_reverse($prev)); + $prev = $s; +} +say $s; diff --git a/challenge-069/paulo-custodio/t/test-1.yaml b/challenge-069/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..d0b5752a4c --- /dev/null +++ b/challenge-069/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 1 1000 + input: + output: 8, 69, 88, 96, 609, 689, 808, 888, 906, 986 diff --git a/challenge-069/paulo-custodio/t/test-2.yaml b/challenge-069/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..0f3bfa090d --- /dev/null +++ b/challenge-069/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 0010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011100100110001101100010011100110111001001100011011100100111001101110010011000110110001001110011011000100110001101110010011100110110001001100011011000100111001101110010011000110111001001110011011100100110001101100010011100110110001001100011011100100111001101110010011000110110001001110011011100100110001101110010011100110110001001100011011000100111001101100010011000110111001001110011011000100110001101100010011100110111001001100011011100100111001101100010011000110110001001110011011000100110001101110010011100110111001001100011011000100111001101110010011000110111001001110011011000100110001101100010011100110110001001100011011100100111001101100010011000110110001001110011011100100110001101110010011100110111001001100011011000100111001101100010011000110111001001110011011 |
