aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-20 20:15:48 +0100
committerGitHub <noreply@github.com>2023-05-20 20:15:48 +0100
commitd537ac3afaa9c12f40cad1ce15a813c1b4592d86 (patch)
tree5f0e4ebe05d502924acce835ed54d4c0343596a1
parent22cd1b407333e4a2849bff1c339f206281bfd990 (diff)
parent12216a3eeb12eb503cf88c4d6ffc298848076304 (diff)
downloadperlweeklychallenge-club-d537ac3afaa9c12f40cad1ce15a813c1b4592d86.tar.gz
perlweeklychallenge-club-d537ac3afaa9c12f40cad1ce15a813c1b4592d86.tar.bz2
perlweeklychallenge-club-d537ac3afaa9c12f40cad1ce15a813c1b4592d86.zip
Merge pull request #8101 from pauloscustodio/master
Add solution
-rw-r--r--challenge-216/paulo-custodio/Makefile2
-rw-r--r--challenge-216/paulo-custodio/perl/ch-1.pl45
-rw-r--r--challenge-216/paulo-custodio/perl/ch-2.pl82
-rw-r--r--challenge-216/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-216/paulo-custodio/t/test-2.yaml20
-rw-r--r--challenge-217/paulo-custodio/Makefile2
-rw-r--r--challenge-217/paulo-custodio/perl/ch-1.pl37
-rw-r--r--challenge-217/paulo-custodio/perl/ch-2.pl49
-rw-r--r--challenge-217/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-217/paulo-custodio/t/test-2.yaml25
10 files changed, 292 insertions, 0 deletions
diff --git a/challenge-216/paulo-custodio/Makefile b/challenge-216/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-216/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-216/paulo-custodio/perl/ch-1.pl b/challenge-216/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..c4e5d3069d
--- /dev/null
+++ b/challenge-216/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+# Challenge 216
+#
+# Task 1: Registration Number
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of words and a random registration number.
+#
+# Write a script to find all the words in the given list that has every letter
+# in the given registration number.
+#
+# Example 1
+#
+# Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD'
+# Output: ('abcd')
+#
+# The only word that matches every alphabets in the given registration number
+# is 'abcd'.
+#
+# Example 2
+#
+# Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB'
+# Output: ('job', 'bjorg')
+#
+# Example 3
+#
+# Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2'
+# Output: ('crack', 'rac')
+
+use Modern::Perl;
+
+sub has_every_letter {
+ my($word, $reg) = @_;
+ $reg =~ s/\s+//g;
+ $reg =~ s/0/o/g;
+ $reg =~ s/\d+//g;
+ for my $letter (split //, $word) {
+ $reg =~ s/$letter//gi;
+ }
+ return $reg eq '';
+}
+
+my($reg, @words) = @ARGV;
+say join ", ", grep {has_every_letter($_, $reg)} @words;
diff --git a/challenge-216/paulo-custodio/perl/ch-2.pl b/challenge-216/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..290dffedd9
--- /dev/null
+++ b/challenge-216/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/usr/bin/env perl
+
+# Challenge 216
+#
+# Task 2: Word Stickers
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of word stickers and a target word.
+#
+# Write a script to find out how many word stickers is needed to make up the
+# given target word.
+#
+# Example 1:
+#
+# Input: @stickers = ('perl','raku','python'), $word = 'peon'
+# Output: 2
+#
+# We just need 2 stickers i.e. 'perl' and 'python'.
+# 'pe' from 'perl' and
+# 'on' from 'python' to get the target word.
+#
+# Example 2:
+#
+# Input: @stickers = ('love','hate','angry'), $word = 'goat'
+# Output: 3
+#
+# We need 3 stickers i.e. 'angry', 'love' and 'hate'.
+# 'g' from 'angry'
+# 'o' from 'love' and
+# 'at' from 'hate' to get the target word.
+#
+# Example 3:
+#
+# Input: @stickers = ('come','nation','delta'), $word = 'accommodation'
+# Output: 4
+#
+# We just need 2 stickers of 'come' and one each of 'nation' & 'delta'.
+# 'a' from 'delta'
+# 'ccommo' from 2 stickers 'come'
+# 'd' from the same sticker 'delta' and
+# 'ation' from 'nation' to get the target word.
+#
+# Example 4:
+#
+# Input: @stickers = ('come','country','delta'), $word = 'accommodation'
+# Output: 0
+#
+# as there's no "i" in the inputs.
+
+use Modern::Perl;
+
+sub max_match {
+ my($word, @stickers) = @_;
+ for my $len (reverse 1 .. length($word)) {
+ for my $s (0 .. length($word)-$len) {
+ my $match = substr($word, $s, $len);
+
+ for my $i (0 .. $#stickers) {
+ if ($stickers[$i] =~ /$match/) {
+ return ($match, $i);
+ }
+ }
+ }
+ }
+ return ("", -1);
+}
+
+sub count_stickers {
+ my($word, @stickers) = @_;
+ @stickers = (@stickers) x length($word);
+ my %used;
+ while ($word ne '') {
+ my($match, $sticker) = max_match($word, @stickers);
+ return 0 if $match eq "";
+ $used{$sticker} = 1;
+ $word =~ s/$match//;
+ $stickers[$sticker] =~ s/$match//;
+ }
+ return scalar keys %used;
+}
+
+say count_stickers(@ARGV);
diff --git a/challenge-216/paulo-custodio/t/test-1.yaml b/challenge-216/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..dcaf2567aa
--- /dev/null
+++ b/challenge-216/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: "'AB1 2CD' abc abcd bcd"
+ input:
+ output: abcd
+- setup:
+ cleanup:
+ args: "'007 JB' job james bjorg"
+ input:
+ output: job, bjorg
+- setup:
+ cleanup:
+ args: "'C7 RA2' crack road rac"
+ input:
+ output: crack, rac
diff --git a/challenge-216/paulo-custodio/t/test-2.yaml b/challenge-216/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..daa51a1a87
--- /dev/null
+++ b/challenge-216/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: peon perl raku python
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: goat love hate angry
+ input:
+ output: 3
+- setup:
+ cleanup:
+ args: accommodation come nation delta
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: accommodation come country delta
+ input:
+ output: 0
diff --git a/challenge-217/paulo-custodio/Makefile b/challenge-217/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-217/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-217/paulo-custodio/perl/ch-1.pl b/challenge-217/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..e3d1c555f9
--- /dev/null
+++ b/challenge-217/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+# Challenge 217
+#
+# Task 1: Sorted Matrix
+# Submitted by: Mohammad S Anwar
+#
+# You are given a n x n matrix where n >= 2.
+#
+# Write a script to find 3rd smallest element in the sorted matrix.
+# Example 1
+#
+# Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3])
+# Output: 1
+#
+# The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5.
+# The 3rd smallest of the sorted list is 1.
+#
+# Example 2
+#
+# Input: @matrix = ([2, 1], [4, 5])
+# Output: 4
+#
+# The sorted list of the given matrix: 1, 2, 4, 5.
+# The 3rd smallest of the sorted list is 4.
+#
+# Example 3
+#
+# Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1])
+# Output: 0
+#
+# The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3.
+# The 3rd smallest of the sorted list is 0.
+
+use Modern::Perl;
+
+say((sort {$a<=>$b} split ' ', "@ARGV" =~ s/\D/ /gr)[2]);
diff --git a/challenge-217/paulo-custodio/perl/ch-2.pl b/challenge-217/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..dadcc0ef29
--- /dev/null
+++ b/challenge-217/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+# Challenge 217
+#
+# Task 2: Max Number
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of positive integers.
+#
+# Write a script to concatenate the integers to form the highest possible value.
+# Example 1:
+#
+# Input: @list = (1, 23)
+# Output: 231
+#
+# Example 2:
+#
+# Input: @list = (10, 3, 2)
+# Output: 3210
+#
+# Example 3:
+#
+# Input: @list = (31, 2, 4, 10)
+# Output: 431210
+#
+# Example 4:
+#
+# Input: @list = (5, 11, 4, 1, 2)
+# Output: 542111
+#
+# Example 5:
+#
+# Input: @list = (1, 10)
+# Output: 110
+
+use Modern::Perl;
+
+sub by_largest {
+ my($a, $b) = @_;
+
+ if (substr($a,0,1) ne substr($b,0,1)) {
+ return $b cmp $a;
+ }
+ else {
+ return length($a) <=> length($b);
+ }
+}
+
+say join '', sort {by_largest($a,$b)} @ARGV;
diff --git a/challenge-217/paulo-custodio/t/test-1.yaml b/challenge-217/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..1795b47ab2
--- /dev/null
+++ b/challenge-217/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: "'([3, 1, 2], [5, 2, 4], [0, 1, 3])'"
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: "'([2, 1], [4, 5])'"
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: "'([1, 0, 3], [0, 0, 0], [1, 2, 1])'"
+ input:
+ output: 0
diff --git a/challenge-217/paulo-custodio/t/test-2.yaml b/challenge-217/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..2ebfc063cb
--- /dev/null
+++ b/challenge-217/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 1 23
+ input:
+ output: 231
+- setup:
+ cleanup:
+ args: 10 3 2
+ input:
+ output: 3210
+- setup:
+ cleanup:
+ args: 31 2 4 10
+ input:
+ output: 431210
+- setup:
+ cleanup:
+ args: 5 11 4 1 2
+ input:
+ output: 542111
+- setup:
+ cleanup:
+ args: 1 10
+ input:
+ output: 110