diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-25 10:13:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-25 10:13:39 +0100 |
| commit | a5d98d2f9d85a2742f783e591f1386d0729d5595 (patch) | |
| tree | 2bd54f1edb33aa2321836cd204da1f097abb36e1 | |
| parent | 67e9a1ac432929a34a918c5478fa33df6fb8b480 (diff) | |
| parent | a9a0c9dc3071261876a38af9eba2bfc0dfa3c345 (diff) | |
| download | perlweeklychallenge-club-a5d98d2f9d85a2742f783e591f1386d0729d5595.tar.gz perlweeklychallenge-club-a5d98d2f9d85a2742f783e591f1386d0729d5595.tar.bz2 perlweeklychallenge-club-a5d98d2f9d85a2742f783e591f1386d0729d5595.zip | |
Merge pull request #10688 from pauloscustodio/master
Add Perl solution to challenge 248
| -rw-r--r-- | challenge-248/paulo-custodio/perl/ch-2.pl | 80 | ||||
| -rw-r--r-- | challenge-248/paulo-custodio/t/test-2.yaml | 19 | ||||
| -rw-r--r-- | challenge-253/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-253/paulo-custodio/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-253/paulo-custodio/perl/ch-2.pl | 65 | ||||
| -rw-r--r-- | challenge-253/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-253/paulo-custodio/t/test-2.yaml | 10 |
7 files changed, 212 insertions, 0 deletions
diff --git a/challenge-248/paulo-custodio/perl/ch-2.pl b/challenge-248/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..79369dfc3b --- /dev/null +++ b/challenge-248/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl + +# Challenge 248 +# +# Task 2: Submatrix Sum +# Submitted by: Jorg Sommrey +# You are given a NxM matrix A of integers. +# +# Write a script to construct a (N-1)x(M-1) matrix B having elements that are the sum over the 2x2 submatrices of A, +# +# b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1] +# +# Example 1 +# Input: $a = [ +# [1, 2, 3, 4], +# [5, 6, 7, 8], +# [9, 10, 11, 12] +# ] +# +# Output: $b = [ +# [14, 18, 22], +# [30, 34, 38] +# ] +# Example 2 +# Input: $a = [ +# [1, 0, 0, 0], +# [0, 1, 0, 0], +# [0, 0, 1, 0], +# [0, 0, 0, 1] +# ] +# +# Output: $b = [ +# [2, 1, 0], +# [1, 2, 1], +# [0, 1, 2] +# ] + +use Modern::Perl; + +my $matrix = parse_matrix("@ARGV"); +my $sums = sum_submatrices($matrix); +print_matrix($sums); + +sub parse_matrix { + my($text) = @_; + my @lines = split(/\]\s*,/, $text); + my $matrix = []; + for (@lines) { + s/^\D+//; + my @nums = split /\D+/, $_; + push @$matrix, \@nums; + } + return $matrix; +} + +sub sum_submatrices { + my($matrix) = @_; + my $sums = []; + my $rows = scalar(@$matrix); + my $cols = scalar(@{$matrix->[0]}); + for my $r (0 .. $rows-2) { + for my $c (0 .. $cols-2) { + $sums->[$r][$c] = 0; + $sums->[$r][$c] += $matrix->[$r][$c]; + $sums->[$r][$c] += $matrix->[$r][$c+1]; + $sums->[$r][$c] += $matrix->[$r+1][$c]; + $sums->[$r][$c] += $matrix->[$r+1][$c+1]; + } + } + return $sums; +} + +sub print_matrix { + my($matrix) = @_; + say "["; + for (@$matrix) { + say " [", join(", ", @$_), "],"; + } + say "]"; +} diff --git a/challenge-248/paulo-custodio/t/test-2.yaml b/challenge-248/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ca5e38be19 --- /dev/null +++ b/challenge-248/paulo-custodio/t/test-2.yaml @@ -0,0 +1,19 @@ +- setup: + cleanup: + args: '[ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]' + input: + output: | + |[ + | [14, 18, 22], + | [30, 34, 38], + |] +- setup: + cleanup: + args: '[ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ]' + input: + output: | + |[ + | [2, 1, 0], + | [1, 2, 1], + | [0, 1, 2], + |] diff --git a/challenge-253/paulo-custodio/Makefile b/challenge-253/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-253/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-253/paulo-custodio/perl/ch-1.pl b/challenge-253/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..50830257d2 --- /dev/null +++ b/challenge-253/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Challenge 253 +# +# Task 1: Split Strings +# Submitted by: Mohammad S Anwar +# You are given an array of strings and a character separator. +# +# Write a script to return all words separated by the given character +# excluding empty string. +# +# Example 1 +# Input: @words = ("one.two.three","four.five","six") +# $separator = "." +# Output: "one","two","three","four","five","six" +# Example 2 +# Input: @words = ("$perl$$", "$$raku$") +# $separator = "$" +# Output: "perl","raku" + +use Modern::Perl; + +my($sep, @words) = @ARGV; +my $words = join($sep, @words); +@words = grep {$_ ne ''} split /\Q$sep/, $words; +say "@words"; diff --git a/challenge-253/paulo-custodio/perl/ch-2.pl b/challenge-253/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..de3cea2947 --- /dev/null +++ b/challenge-253/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl + +# Challenge 253 +# +# Task 2: Weakest Row +# Submitted by: Mohammad S Anwar +# You are given an m x n binary matrix i.e. only 0 and 1 where 1 always appear before 0. +# +# A row i is weaker than a row j if one of the following is true: +# +# a) The number of 1s in row i is less than the number of 1s in row j. +# b) Both rows have the same number of 1 and i < j. +# Write a script to return the order of rows from weakest to strongest. +# +# Example 1 +# Input: $matrix = [ +# [1, 1, 0, 0, 0], +# [1, 1, 1, 1, 0], +# [1, 0, 0, 0, 0], +# [1, 1, 0, 0, 0], +# [1, 1, 1, 1, 1] +# ] +# Output: (2, 0, 3, 1, 4) +# +# The number of 1s in each row is: +# - Row 0: 2 +# - Row 1: 4 +# - Row 2: 1 +# - Row 3: 2 +# - Row 4: 5 +# Example 2 +# Input: $matrix = [ +# [1, 0, 0, 0], +# [1, 1, 1, 1], +# [1, 0, 0, 0], +# [1, 0, 0, 0] +# ] +# Output: (0, 2, 3, 1) +# +# The number of 1s in each row is: +# - Row 0: 1 +# - Row 1: 4 +# - Row 2: 1 +# - Row 3: 1 + +use Modern::Perl; +use List::Util 'sum'; + +my $matrix = parse_matrix("@ARGV"); +say join " ", + map {$_->[0]} + sort {$a->[1] == $b->[1] ? $a->[0] <=> $b->[0] : $a->[1] <=> $b->[1]} + map {[$_, sum(@{$matrix->[$_]})]} 0 .. $#$matrix; + +sub parse_matrix { + my($text) = @_; + my @lines = split(/\]\s*,/, $text); + my $matrix = []; + for (@lines) { + s/^\D+//; + my @nums = split /\D+/, $_; + push @$matrix, \@nums; + } + return $matrix; +} diff --git a/challenge-253/paulo-custodio/t/test-1.yaml b/challenge-253/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..fc06e98f61 --- /dev/null +++ b/challenge-253/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: . one.two.three four.five six + input: + output: one two three four five six +- setup: + cleanup: + args: "'$' '$perl$$' '$$raku$'" + input: + output: perl raku diff --git a/challenge-253/paulo-custodio/t/test-2.yaml b/challenge-253/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..0509d83514 --- /dev/null +++ b/challenge-253/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: '[ [1, 1, 0, 0, 0], [1, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1] ]' + input: + output: 2 0 3 1 4 +- setup: + cleanup: + args: '[ [1, 0, 0, 0], [1, 1, 1, 1], [1, 0, 0, 0], [1, 0, 0, 0] ]' + input: + output: 0 2 3 1 |
