diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-12 18:36:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-12 18:36:53 +0000 |
| commit | be322b567f75d74d60a2f5f8568ace56da8630ef (patch) | |
| tree | 20b696988b72f159add4faf4f7df9d3419bdcd2a | |
| parent | bdad32c82b247a4ea4f786c8c93a2844f2e5ef75 (diff) | |
| parent | 7b01224f669cc37933dcb1d91df262da8df359d9 (diff) | |
| download | perlweeklychallenge-club-be322b567f75d74d60a2f5f8568ace56da8630ef.tar.gz perlweeklychallenge-club-be322b567f75d74d60a2f5f8568ace56da8630ef.tar.bz2 perlweeklychallenge-club-be322b567f75d74d60a2f5f8568ace56da8630ef.zip | |
Merge pull request #9036 from ntovar/branch-242
Challenge 242, solutions by Nelo Tovar
| -rw-r--r-- | challenge-242/nelo-tovar/README.md | 1 | ||||
| -rw-r--r-- | challenge-242/nelo-tovar/perl/ch-1.pl | 73 | ||||
| -rw-r--r-- | challenge-242/nelo-tovar/perl/ch-2.pl | 85 |
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 ' '; +} |
