aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorntovar <tovar.nelo@gmail.com>2023-11-10 18:30:59 -0500
committerntovar <tovar.nelo@gmail.com>2023-11-10 18:30:59 -0500
commit7b01224f669cc37933dcb1d91df262da8df359d9 (patch)
tree0489cd5111b6ed310bbcf2e5a25296c72daee3c4
parent09f1e6fdb27cf9a44ca982e3c5a8f3f27369de80 (diff)
downloadperlweeklychallenge-club-7b01224f669cc37933dcb1d91df262da8df359d9.tar.gz
perlweeklychallenge-club-7b01224f669cc37933dcb1d91df262da8df359d9.tar.bz2
perlweeklychallenge-club-7b01224f669cc37933dcb1d91df262da8df359d9.zip
Challenge 242, solutions by Nelo Tovar
-rw-r--r--challenge-242/nelo-tovar/README.md1
-rw-r--r--challenge-242/nelo-tovar/perl/ch-1.pl73
-rw-r--r--challenge-242/nelo-tovar/perl/ch-2.pl85
3 files changed, 159 insertions, 0 deletions
diff --git a/challenge-242/nelo-tovar/README.md b/challenge-242/nelo-tovar/README.md
new file mode 100644
index 0000000000..845b45e758
--- /dev/null
+++ b/challenge-242/nelo-tovar/README.md
@@ -0,0 +1 @@
+Solution by Nelo Tovar
diff --git a/challenge-242/nelo-tovar/perl/ch-1.pl b/challenge-242/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..382a77f5cd
--- /dev/null
+++ b/challenge-242/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,73 @@
+#!/usr/bin/env perl
+
+# Task 1: Missing Members
+#
+# You are given two arrays 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).
+
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ [ 1, 2, 3 ],
+ [ 2, 4, 6 ],
+ ],
+ [ [ 1, 2, 3, 3 ],
+ [ 1, 1, 2, 2 ],
+ ],
+);
+
+sub missing_members {
+ my $a1 = shift;
+ my $a2 = shift;
+
+ my @missing1;
+ my @missing2;
+
+
+ no warnings;
+ foreach my $element (@$a1) {
+ next if ( $element ~~ @$a2);
+ push @missing1, $element unless ($element ~~ @missing1);
+ }
+
+ foreach my $element (@$a2) {
+ next if ( $element ~~ @$a1);
+ push @missing2, $element unless ($element ~~ @missing2);
+ }
+ use warnings;
+
+ return [[@missing1], [@missing2]];
+}
+
+for my $e (@examples) {
+ my @arr1 = $e->[0]->@*;
+ my @arr2 = $e->[1]->@*;
+
+ my $mm = missing_members(\@arr1, \@arr2);
+ say 'Input : @arr1 = ', dump(\@arr1);
+ say ' @arr2 = ', dump(\@arr2);
+ say 'Output : ', dump($mm);
+ say ' ';
+}
diff --git a/challenge-242/nelo-tovar/perl/ch-2.pl b/challenge-242/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..ffab352fdb
--- /dev/null
+++ b/challenge-242/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,85 @@
+#!/usr/bin/env perl
+
+# Task 2: Flip Matrix
+#
+# 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])
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+
+ [ [ 1, 1, 0 ],
+ [ 1, 0, 1 ],
+ [ 0, 0, 0 ]
+ ],
+ [ [ 1, 1, 0, 0 ],
+ [ 1, 0, 0, 1 ],
+ [ 0, 1, 1, 1 ],
+ [ 1, 0, 1, 0 ]
+ ],
+);
+
+sub reverse_matrix_rows {
+ my $m1 = shift;
+ my @reverse;
+
+ foreach my $x ($m1->@*) {
+ push @reverse, [reverse $x->@*];
+ }
+
+ return \@reverse
+}
+
+sub invertir {
+ my $m1 = shift;
+ my @invert;
+
+ foreach my $x ($m1->@*) {
+ push @invert, [map {$_ == 0 ? 1 : 0} $x->@*];
+ }
+
+ return \@invert
+}
+
+for my $m (@examples) {
+ my @matrix = $m->@*;
+
+ my $reverse = reverse_matrix_rows \@matrix;
+ my $invert = invertir $reverse;
+
+ say 'Input : @matrix = ', dump(@matrix);
+ say 'Output : ', dump(@$invert);
+ say ' ';
+}