Robbie Hatley's Solutions To The Weekly Challenge #239

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2023-10-15 through 2023-10-21) is weekly challenge #239.

Task 239-1 is as follows:

Task 1: Same String
Submitted by: Mohammad S Anwar
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"

This is just a matter of joining with "join" and comparing with "eq":

Robbie Hatley's Solution to The Weekly Challenge 239-1

Task 239-2 is as follows:

Task 2: Consistent Strings
Submitted by: Mohammad S Anwar
Given an array of strings and a "string of allowed characters"
(consisting of distinct characters), write a script to determine
how many strings in the array are "consistent" in the sense that
they consist only of characters which appear in the "string of
allowed characters".

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.

I could do this by breaking the strings to arrays, but that seems awkward. Or, I could use "substr", but that seems even MORE awkward. I think I'll use regular expressions instead. Maybe not as fast, but more elegant:

Robbie Hatley's Solution to The Weekly Challenge 239-2

That's it for 239; see you on 240!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #215

Robbie Hatley's Solutions To The Weekly Challenge #221