aboutsummaryrefslogtreecommitdiff
path: root/challenge-242
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-11-12 18:51:18 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-11-12 18:51:18 +0000
commit54093dc3c1faec9e7b8db1e9e2c91d72753eae8c (patch)
treec15082ccde27213f69f2e8c7f68dcf57c55789fa /challenge-242
parentbe322b567f75d74d60a2f5f8568ace56da8630ef (diff)
downloadperlweeklychallenge-club-54093dc3c1faec9e7b8db1e9e2c91d72753eae8c.tar.gz
perlweeklychallenge-club-54093dc3c1faec9e7b8db1e9e2c91d72753eae8c.tar.bz2
perlweeklychallenge-club-54093dc3c1faec9e7b8db1e9e2c91d72753eae8c.zip
- Added solutions by Nelo Tovar.
- Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-242')
-rwxr-xr-xchallenge-242/wanderdoc/perl/ch-1.pl56
-rwxr-xr-xchallenge-242/wanderdoc/perl/ch-2.pl46
2 files changed, 102 insertions, 0 deletions
diff --git a/challenge-242/wanderdoc/perl/ch-1.pl b/challenge-242/wanderdoc/perl/ch-1.pl
new file mode 100755
index 0000000000..2adf7a42b6
--- /dev/null
+++ b/challenge-242/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given two array of integers. Write a script to find out the missing members in each other arrays.
+Example 1 Input: @arr1 = (1, 2, 3) @arr2 = (2, 4, 6) Output: ([1, 3], [4, 6])
+(1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6).
+(2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3).
+Example 2 Input: @arr1 = (1, 2, 3, 3) @arr2 = (1, 1, 2, 2) Output: ([3])
+(1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one.
+(1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3).
+
+=cut
+
+use Test2::V0;
+
+sub missing_members
+{
+ my ($aref_1, $aref_2, $flag_unique, $flag_nonempty) = @_;
+ my (@missing_1_from_2, @missing_2_from_1);
+ my ($vec_1, $vec_2);
+ vec($vec_1, $_, 1) = 1 for @{$aref_1};
+ vec($vec_2, $_, 1) = 1 for @{$aref_2};
+ @missing_1_from_2 = grep { vec($vec_2, $_, 1) != 1 } @{$aref_1};
+ @missing_2_from_1 = grep { vec($vec_1, $_, 1) != 1 } @{$aref_2};
+ if ( defined $flag_unique )
+ {
+ _unique($_) for ( \@missing_1_from_2, \@missing_2_from_1 );
+ }
+ if ( defined $flag_nonempty )
+ {
+ return _nonempty([@missing_1_from_2], [@missing_2_from_1]);
+ }
+ else
+ {
+ return ([@missing_1_from_2], [@missing_2_from_1]);
+ }
+}
+
+sub _unique
+{
+ my %seen;
+ @{$_[0]} = grep {! $seen{$_}++ } @{$_[0]};
+}
+
+
+sub _nonempty
+{
+ return grep { scalar(@$_) > 0 } @_;
+}
+
+
+is([missing_members([1, 2, 3], [2, 4, 6], 1)], [[1, 3], [4, 6]], 'Example 1');
+is([missing_members([1, 2, 3, 3], [1, 1, 2, 2], 'y', 'y')], [[3]], 'Example 2');
+done_testing; \ No newline at end of file
diff --git a/challenge-242/wanderdoc/perl/ch-2.pl b/challenge-242/wanderdoc/perl/ch-2.pl
new file mode 100755
index 0000000000..26981bdb82
--- /dev/null
+++ b/challenge-242/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given n x n binary matrix. Write a script to flip the given matrix as below.
+1 1 0
+0 1 1
+0 0 1
+a) Reverse each row
+0 1 1
+1 1 0
+1 0 0
+b) Invert each member
+1 0 0
+0 0 1
+0 1 1
+
+Example 1
+Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0])
+Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
+Example 2
+Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])
+Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
+=cut
+
+use Test2::V0;
+
+
+sub flip_matrix
+{
+ my $mtr = $_[0];
+ for my $row ( @{$mtr} )
+ {
+ @{$row} = reverse @{$row};
+ @{$row} = map { abs($_ -= 1) } @{$row};
+ }
+ return $mtr;
+}
+
+
+is(flip_matrix( [[1, 1, 0], [1, 0, 1], [0, 0, 0]] ),
+ [[1, 0, 0], [0, 1, 0], [1, 1, 1]], 'Example 1');
+is(flip_matrix( [[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]] ),
+ [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], 'Example 2');
+done_testing(); \ No newline at end of file