aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-221/paulo-custodio/Makefile2
-rw-r--r--challenge-221/paulo-custodio/perl/ch-1.pl48
-rw-r--r--challenge-221/paulo-custodio/t/test-1.yaml10
3 files changed, 60 insertions, 0 deletions
diff --git a/challenge-221/paulo-custodio/Makefile b/challenge-221/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-221/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-221/paulo-custodio/perl/ch-1.pl b/challenge-221/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..6419ef21f8
--- /dev/null
+++ b/challenge-221/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+# Challenge 221
+#
+# Task 1: Good Strings
+# Submitted by: Mohammad S Anwar
+#
+# You are given a list of @words and a string $chars.
+#
+# A string is good if it can be formed by characters from $chars, each
+# character can be used only once.
+#
+#
+# Write a script to return the sum of lengths of all good strings in words.
+#
+# Example 1
+#
+# Input: @words = ("cat", "bt", "hat", "tree")
+# $chars = "atach"
+# Output: 6
+#
+# The good strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.
+#
+# Example 2
+#
+# Input: @words = ("hello", "world", "challenge")
+# $chars = "welldonehopper"
+# Output: 10
+#
+# The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.
+
+use Modern::Perl;
+
+my @words = @ARGV;
+my $chars = pop(@words);
+my $result = 0;
+for my $word (@words) {
+ $result += length($word) if is_good_string($word, $chars);
+}
+say $result;
+
+sub is_good_string {
+ my($word, $chars) = @_;
+ for my $ch (split //, $chars) {
+ $word =~ s/$ch//i;
+ }
+ return $word eq '';
+}
diff --git a/challenge-221/paulo-custodio/t/test-1.yaml b/challenge-221/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..21517bd98b
--- /dev/null
+++ b/challenge-221/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: cat bt hat tree atach
+ input:
+ output: 6
+- setup:
+ cleanup:
+ args: hello world challenge welldonehopper
+ input:
+ output: 10