aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2023-10-16 21:00:56 +0200
committerpme <hauptadler@gmail.com>2023-10-16 21:00:56 +0200
commitd0fb609a80c9cee62d5269f41388d92ea8f19f15 (patch)
treef9c328f6bc1f9bb55d0349d03697112456c2f044
parentafac53ab966abe7e14039640d054f82eb323097c (diff)
downloadperlweeklychallenge-club-d0fb609a80c9cee62d5269f41388d92ea8f19f15.tar.gz
perlweeklychallenge-club-d0fb609a80c9cee62d5269f41388d92ea8f19f15.tar.bz2
perlweeklychallenge-club-d0fb609a80c9cee62d5269f41388d92ea8f19f15.zip
challenge-239
-rwxr-xr-xchallenge-239/peter-meszaros/perl/ch-1.pl62
-rwxr-xr-xchallenge-239/peter-meszaros/perl/ch-2.pl69
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-239/peter-meszaros/perl/ch-1.pl b/challenge-239/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..7985f31740
--- /dev/null
+++ b/challenge-239/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+#
+# You are given two arrays of strings.
+#
+# Write a script to find out if the word created by concatenating the array
+# elements is the same.
+# Example 1
+#
+# Input: @arr1 = ("ab", "c")
+# @arr2 = ("a", "bc")
+# Output: true
+#
+# Using @arr1, word1 => "ab" . "c" => "abc"
+# Using @arr2, word2 => "a" . "bc" => "abc"
+#
+# Example 2
+#
+# Input: @arr1 = ("ab", "c")
+# @arr2 = ("ac", "b")
+# Output: false
+#
+# Using @arr1, word1 => "ab" . "c" => "abc"
+# Using @arr2, word2 => "ac" . "b" => "acb"
+#
+# Example 3
+#
+# Input: @arr1 = ("ab", "cd", "e")
+# @arr2 = ("abcde")
+# Output: true
+#
+# Using @arr1, word1 => "ab" . "cd" . "e" => "abcde"
+# Using @arr2, word2 => "abcde"
+#
+
+use strict;
+use warnings;
+use Test::More;
+use Data::Dumper;
+
+my $cases = [
+ [["ab", "c"], ["a", "bc"]],
+ [["ab", "c"], ["ac", "b"]],
+ [["ab", "cd", "e"], ["abcde"]],
+];
+
+sub same_string
+{
+ my ($arr1, $arr2) = $_[0]->@*;
+
+ my $w1 = join('', @$arr1);
+ my $w2 = join('', @$arr2);
+ return $w1 eq $w2 ? 1 : 0;
+}
+
+is(same_string($cases->[0]), 1, '[["ab", "c"], ["a", "bc"]]');
+is(same_string($cases->[1]), 0, '[["ab", "c"], ["ac", "b"]]');
+is(same_string($cases->[2]), 1, '[["ab", "cd", "e"], ["abcde"]]');
+done_testing();
+
+exit 0;
+
+
diff --git a/challenge-239/peter-meszaros/perl/ch-2.pl b/challenge-239/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..0e2b1c54c7
--- /dev/null
+++ b/challenge-239/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/env perl
+#
+# You are given an array of strings and allowed string having distinct
+# characters.
+#
+# A string is consistent if all characters in the string appear in the
+# string allowed.
+#
+#
+# Write a script to return the number of consistent strings in the given array.
+# Example 1
+#
+# Input: @str = ("ad", "bd", "aaab", "baa", "badab")
+# $allowed = "ab"
+# Output: 2
+#
+# Strings "aaab" and "baa" are consistent since they only contain characters
+# 'a' and 'b'.
+#
+# Example 2
+#
+# Input: @str = ("a", "b", "c", "ab", "ac", "bc", "abc")
+# $allowed = "abc"
+# Output: 7
+#
+# Example 3
+#
+# Input: @str = ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d")
+# $allowed = "cad"
+# Output: 4
+#
+# Strings "cc", "acd", "ac", and "d" are consistent.
+#
+
+use strict;
+use warnings;
+use Test::More;
+use Data::Dumper;
+
+my $cases = [
+ [["ad", "bd", "aaab", "baa", "badab"], "ab"],
+ [["a", "b", "c", "ab", "ac", "bc", "abc"], "abc"],
+ [["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"], "cad"],
+];
+
+sub consistent_strings
+{
+ my ($strs, $allowed) = $_[0]->@*;
+
+ my %allowed = map { $_ => 1} split('', $allowed);
+
+ my $cnt = 0;
+ WORD: for my $w (@$strs) {
+ my @w = split('', $w);
+ for my $c (@w) {
+ next WORD unless exists $allowed{$c};
+ }
+ ++$cnt;
+ }
+
+ return $cnt;
+}
+
+is(consistent_strings($cases->[0]), 2, '[["ad", "bd", "aaab", "baa", "badab"], "ab"]');
+is(consistent_strings($cases->[1]), 7, '[["a", "b", "c", "ab", "ac", "bc", "abc"], "abc"]');
+is(consistent_strings($cases->[2]), 4, '[["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"], "cad"]');
+done_testing();
+
+exit 0;