Peter’s solutions: week 373 — 11 May 2026
THE WEEKLY CHALLENGE
The week of lists
You are given two arrays of strings. Write a script to return true if the two given arrays represent the same strings when concatenated, otherwise return false.
Example 1 Input: @arr1 = ('a', 'bc') @arr2 = ('ab', 'c') Output: true Array 1: 'a' + 'bc' = 'abc' Array 2: 'ab' + 'c' = 'abc' Example 2 Input: @arr1 = ('a', 'b', 'c') @arr2 = ('a', 'bc') Output: true Array 1: 'a' + 'b' + 'c' = 'abc' Array 2: 'a' + 'bc' = 'abc' Example 3 Input: @arr1 = ('a', 'bc') @arr2 = ('a', 'c', 'b') Output: false Array 1: 'a' + 'bc' = 'abc' Array 2: 'a' + 'c' + 'b' = 'acb' Example 4 Input: @arr1 = ('ab', 'c', '') @arr2 = ('', 'a', 'bc') Output: true Array 1: 'ab' + 'c' + '' = 'abc' Array 2: '' + 'a' + 'bc' = 'abc' Example 5 Input: @arr1 = ('p', 'e', 'r', 'l') @arr2 = ('perl') Output: true Array 1: 'p' + 'e' + 'r' + 'l' = 'perl' Array 2: 'perl'
As always, someone will have a one-liner, but I settled initially for two lines. However, in order to get rid of the 'unitiallised' warning where either concatenated atring was empty I had to add a third line.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-05-11 use utf8; # Week 373 - task 1 - Equal list use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; equal_list(['a', 'bc'], ['a', 'bc']); equal_list(['a', 'c', 'b'], ['abc']); equal_list(['ab', 'c'], []); equal_list(['', ''], ['', '', '']); equal_list(['c', 'h', 'al', 'le', 'ng', 'e'], ['challenge']); equal_list(['cha', 'llenge'], ['chall', 'enge']); sub equal_list { my ($s, @joined); # concatenate the string elements @joined = ('', ''); $joined[0] .= $_ for @{$_[0]}; $joined[1] .= $_ for @{$_[1]}; # report say qq[\nInput: ('] . join(q[', '], @{$_[0]}) . q['), ('] . join(q[', '], @{$_[1]}) . q[')]; say qq[Output: ] . ($joined[0] eq $joined[1] ? 'true' : 'false'); }
8 lines of code
Input: ('a', 'bc'), ('a', 'bc') Output: true Input: ('a', 'c', 'b'), ('abc') Output: false Input: ('ab', 'c'), ('') Output: false Input: ('', ''), ('', '', '') Output: true Input: ('c', 'h', 'al', 'le', 'ng', 'e'), ('challenge') Output: true Input: ('cha', 'llenge'), ('chall', 'enge') Output: true
Any content of this website which has been created by Peter Campbell Smith is in the public domain