Robbie Hatley's Perl Solutions To The Weekly Challenge #209

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

The Weekly Challenge

This week (2023-03-19 through 2023-03-25) is weekly challenge #209.

Task 1 is as follows:

You are given an array of binary bits that ends with 0. 
Valid sequences in the bit string are:
[0] -decodes-to-> "a"
[1, 0] -> "b"
[1, 1] -> "c"
Write a script to print 1 if the last character is an “a”, 
otherwise print 0.

This is perhaps the more technically-challenging of the two tasks this we, as it pretty much requires usage of a Finite State Machine. But for me that's easy, as I'm good at making finite state machines. So I did so:

Robbie's Solution to TWC 209-1

Task 2 was this:

Task 2: Merge Account
Submitted by: Mohammad S Anwar
You are given an array of accounts i.e. name with list of email addresses. 
Write a script to merge the accounts where possible. The accounts can only 
be merged if they have at least one email address in common.
Example 1: 
   Input:  [['A', 'a1@a.com', 'a2@a.com'],
            ['B', 'b1@b.com'],
            ['A', 'a3@a.com', 'a1@a.com']]
   Output: [['A', 'a1@a.com', 'a2@a.com', 'a3@a.com'], 
            ['B', 'b1@b.com']]
Example 2: 
   Input:  [['A', 'a1@a.com', 'a2@a.com'], 
            ['B', 'b1@b.com'], 
            ['A', 'a3@a.com'], 
            ['B', 'b2@b.com', 'b1@b.com']]
   Output: [['A', 'a1@a.com', 'a2@a.com'], 
            ['A', 'a3@a.com'], 
            ['B', 'b1@b.com', 'b2@b.com']]

That's conceptually simple ("merge some accounts") but does heavily involve "arrays of arrays of arrays", which Perl arguably does somewhat more clumsily than C++ (which would maybe be the better language for this task). But I persevered and came up with a solution in Perl anyway:

Robbie's Solution to TWC 209-2

That's it for 209; see you on 210.

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #215

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239