aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-08-24 19:09:39 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-08-24 19:09:39 +0100
commita9a0c9dc3071261876a38af9eba2bfc0dfa3c345 (patch)
tree5a0df3bdee4b8857e5ea3a4a7edf3d20b2030edd
parent944a7674b54f7e035cbde8733a6ba0dfcb77ac86 (diff)
downloadperlweeklychallenge-club-a9a0c9dc3071261876a38af9eba2bfc0dfa3c345.tar.gz
perlweeklychallenge-club-a9a0c9dc3071261876a38af9eba2bfc0dfa3c345.tar.bz2
perlweeklychallenge-club-a9a0c9dc3071261876a38af9eba2bfc0dfa3c345.zip
Add Perl solution to challenge 253
-rw-r--r--challenge-253/paulo-custodio/Makefile2
-rw-r--r--challenge-253/paulo-custodio/perl/ch-1.pl26
-rw-r--r--challenge-253/paulo-custodio/perl/ch-2.pl65
-rw-r--r--challenge-253/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-253/paulo-custodio/t/test-2.yaml10
5 files changed, 113 insertions, 0 deletions
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