Perl Weekly Challenge 239.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 239.
Task 1: Same String
Submitted by: Mohammad S Anwar
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"
I input each array as a space separated string. The trivial solution would be then to remove the spaces and compare the results, but as it seems like cheating, I first split them into two arrays, then join them and compare. The result is a simple one-liner.
Example 1:
perl -E '
@x=split " ", shift; @y=split " ", shift; say "(@x), (@y) -> ", (join "", @x) eq (join "",@y)?"True":"False"
' "ab c" "a bc"
Results:
(ab c), (a bc) -> True
Example 2:
perl -E '
@x=split " ", shift; @y=split " ", shift; say "(@x), (@y) -> ", (join "", @x) eq (join "",@y)?"True":"False"
' "ab c" "ac b"
Results:
(ab c), (ac b) -> False
Example 3:
perl -E '
@x=split " ", shift; @y=split " ", shift; say "(@x), (@y) -> ", (join "", @x) eq (join "",@y)?"True":"False"
' "ab cd e" "abcde"
Results:
(ab cd e), (abcde) -> True
The full code is similar.
1 # Perl weekly challenge 239
2 # Task 1: Same String
3 #
4 # See https://wlmb.github.io/2023/10/15/PWC239/#task-1-same-string
5 use v5.36;
6 die <<~"FIN" unless @ARGV==2;
7 Usage: $0 AR1 AR2
8 to check if the space separated lists of strings ARn concatenate
9 to the same string.
10 FIN
11 my @ar1=split " ", shift;
12 my @ar2=split " ", shift;
13 say "(@ar1), (@ar2) -> ", (join "", @ar1) eq (join "",@ar2)?"True":"False"
Examples:
./ch-1.pl "ab c" "a bc"
./ch-1.pl "ab c" "ac b"
./ch-1.pl "ab cd e" "abcde"
Results:
(ab c), (a bc) -> True
(ab c), (ac b) -> False
(ab cd e), (abcde) -> True
Task 2: Consistent Strings
Submitted by: Mohammad S Anwar
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.
To solve this task I make a hash of allowed characters and use it to
check all characters of all strings. I assume the first input in @ARGV
is the allowed string and the rest are the strings to test. I assume
upper and lower case letters differ. With all
from List::Util
the code fits a one-liner.
Example 1:
perl -MList::Util=all -E '
map{$x{$_}++} split "", $x=shift; @o=grep{all {$x{$_}} split ""}@ARGV; say "$x: @ARGV -> ",0+@o;
' ab ad bd aaab baa badab
Results:
ab: ad bd aaab baa badab -> 2
Example 2:
perl -MList::Util=all -E '
map{$x{$_}++} split "", $x=shift; @o=grep{all {$x{$_}} split ""}@ARGV; say "$x: @ARGV -> ",0+@o;
' abc a b c ab ac bc abc
Results:
abc: a b c ab ac bc abc -> 7
Example 3:
perl -MList::Util=all -E '
map{$x{$_}++} split "", $x=shift; @o=grep{all {$x{$_}} split ""}@ARGV; say "$x: @ARGV -> ",0+@o;
' cad cc acd b ba bac bad ac d
Results:
cad: cc acd b ba bac bad ac d -> 4
The full code is similar:
1 # Perl weekly challenge 239
2 # Task 2: Consistent Strings
3 #
4 # See https://wlmb.github.io/2023/10/15/PWC239/#task-2-consistent-strings
5 use v5.36;
6 use List::Util qw(all);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 A S1 [S2...]
9 Count how many of the strings Sn are consistent with the allowed characters in string A.
10 FIN
11 my $allowed=shift;
12 my %allowed;
13 map {$allowed{$_}++} split "", $allowed;
14 my @output = grep{all {$allowed{$_}} split ""} @ARGV;
15 say "Allowed: $allowed. Strings: @ARGV -> ",0+@output;
Examples:
./ch-2.pl ab ad bd aaab baa badab
./ch-2.pl abc a b c ab ac bc abc
./ch-2.pl cad cc acd b ba bac bad ac d
Results:
Allowed: ab. Strings: ad bd aaab baa badab -> 2
Allowed: abc. Strings: a b c ab ac bc abc -> 7
Allowed: cad. Strings: cc acd b ba bac bad ac d -> 4