aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-07 14:39:30 +0000
committerGitHub <noreply@github.com>2023-11-07 14:39:30 +0000
commite83db97b5bc19d8f26345af7bf2be1204433e730 (patch)
treefeb31de9bf03bfcc9b9b3a2d34bcccfcdb9f2a0e
parent5c464fd842b4e53d342602f2cb710c75aa14a435 (diff)
parente1f41774deab9b65fe994538698bdd4d973846f7 (diff)
downloadperlweeklychallenge-club-e83db97b5bc19d8f26345af7bf2be1204433e730.tar.gz
perlweeklychallenge-club-e83db97b5bc19d8f26345af7bf2be1204433e730.tar.bz2
perlweeklychallenge-club-e83db97b5bc19d8f26345af7bf2be1204433e730.zip
Merge pull request #9017 from pauloscustodio/master
Add Perl solution
-rw-r--r--challenge-242/paulo-custodio/Makefile2
-rw-r--r--challenge-242/paulo-custodio/perl/ch-1.pl63
-rw-r--r--challenge-242/paulo-custodio/perl/ch-2.pl65
-rw-r--r--challenge-242/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-242/paulo-custodio/t/test-2.yaml10
5 files changed, 150 insertions, 0 deletions
diff --git a/challenge-242/paulo-custodio/Makefile b/challenge-242/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-242/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-242/paulo-custodio/perl/ch-1.pl b/challenge-242/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..148f427abd
--- /dev/null
+++ b/challenge-242/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+
+# Challenge 242
+#
+# Task 1: Missing Members
+# Submitted by: Mohammad S Anwar
+# 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 Modern::Perl;
+use List::Util 'uniq';
+
+@ARGV==2 or die "usage: $0 '(a,b,c)' '(d,e,f)'\n";
+my @arr1 = parse(shift);
+my @arr2 = parse(shift);
+
+my @not_in1 = not_in(\@arr1, \@arr2);
+my @not_in2 = not_in(\@arr2, \@arr1);
+
+say output(\@not_in1, \@not_in2);
+
+
+sub parse {
+ my($text) = @_;
+ my @arr = split ' ', $text =~ s/\D/ /gr;
+ return @arr;
+}
+
+sub output {
+ my($arr1, $arr2) = @_;
+ my @elems;
+ for ($arr1, $arr2) {
+ if (@$_) {
+ push @elems, "[".join(", ", @$_)."]"
+ }
+ }
+ return "(".join(", ", @elems).")";
+}
+
+sub not_in {
+ my($arr1, $arr2) = @_;
+ my %arr2;
+ $arr2{$_}=1 for @$arr2;
+ my @not_in;
+ push @not_in, $_ for uniq grep {!$arr2{$_}} @$arr1;
+ return @not_in;
+}
diff --git a/challenge-242/paulo-custodio/perl/ch-2.pl b/challenge-242/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..530091d6b5
--- /dev/null
+++ b/challenge-242/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+
+# Challenge 242
+#
+# Task 1: Missing Members
+# Submitted by: Mohammad S Anwar
+# 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 Modern::Perl;
+
+@ARGV==1 or die "usage: $0 '([a, b, c], [d, e, f], [g, h, i])'\n";
+my @m = parse(shift);
+
+# a) Reverse each row
+$_ = [reverse @$_] for @m;
+
+# b) Invert each member
+for (@m) {
+ for (@$_) {
+ $_ = 1-$_;
+ }
+}
+
+
+say output(@m);
+
+
+sub parse {
+ my($text) = @_;
+ my @m;
+ my @rows = split /\]\s*,\s*\[/, $text;
+ for (@rows) {
+ s/\D/ /g;
+ my @cols = split ' ', $_;
+ push @m, \@cols;
+ }
+ return @m;
+}
+
+sub output {
+ my(@m) = @_;
+ my @elems;
+ for (@m) {
+ push @elems, "[".join(", ", @$_)."]"
+ }
+ return "(".join(", ", @elems).")";
+}
+
diff --git a/challenge-242/paulo-custodio/t/test-1.yaml b/challenge-242/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..930832046a
--- /dev/null
+++ b/challenge-242/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: '"(1, 2, 3)" "(2, 4, 6)"'
+ input:
+ output: ([1, 3], [4, 6])
+- setup:
+ cleanup:
+ args: '"(1, 2, 3, 3)" "(1, 1, 2, 2)"'
+ input:
+ output: ([3])
diff --git a/challenge-242/paulo-custodio/t/test-2.yaml b/challenge-242/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..08a0b6cd29
--- /dev/null
+++ b/challenge-242/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: '"([1, 1, 0], [1, 0, 1], [0, 0, 0])"'
+ input:
+ output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
+- setup:
+ cleanup:
+ args: '"([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])"'
+ input:
+ output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])