Peter’s blog ✴ Week 378 ✴ 15 June 2026
THE WEEKLY CHALLENGE
Second and sum
You are given three strings consisting of lower case English letters ‘a’ to ‘j’ only. The letter value of a = 0, b = 1, c = 3, etc. Write a script to find whether the sum of first two strings is the third string.
Example 1 Input: $str1 = 'acb', $str2 = 'cba', $str3 = 'cdb' Output: true $str1 = 'acb' = 021 $str2 = 'cba' = 210 $str3 = 'cdb' = 231 $str1 + $str2 = $str3 Example 2 Input: $str1 = 'aab', $str2 = 'aac', $str3 = 'ad' Output: true $str1 = 'aab' = 001 $str2 = 'aac' = 002 $str3 = 'ad' = 03 Example 3 Input: $str1 = 'bc', $str2 = 'je', $str3 = 'jg' Output: false $str1 = 'bc' = 12 $str2 = 'je' = 94 $str3 = 'jg' = 96 Example 4 Input: $str1 = 'a', $str2 = 'aaaa', $str3 = 'aa' Output: true $str1 = 'a' = 0 $str2 = 'aaaa' = 0000 $str3 = 'aa' = 00 Example 5 Input: $str1 = 'c', $str2 = 'd', $str3 = 'h' Output: false $str1 = 'c' = 2 $str2 = 'd' = 3 $str3 = 'h' = 7 Example 6 Input: $str1 = 'gfi', $str2 = 'hbf', $str3 = 'bdhd' Output: true $str1 = 'gfi' = 658 $str2 = 'hbf' = 715 $str3 = 'bdhd' = 1373
My solution converts all 3 strings to numbers using
sub number. It then checks that the first two add up to the third.
That works.
I did consider:
'abc + def == ghi',
tr|a-z|0-9| to translate that into
'012 + 345 == 678',
eval to evaluate that as true or false.But that doesn't always work. Can you figure out why? A clue: the example I have given above is one that doesn't work.
It's because the Perl compiler regards an integer with a leading '0' to be octal, so it treats '012' as decimal 10.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-06-15 use utf8; # Week 378 - task 2 - Sum of words use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; sum_of_words('abc', 'def', 'dfh'); sum_of_words('cde', 'fgh', 'aaaaa'); sum_of_words('deaf', 'bedfig', 'begjjb'); sum_of_words('aab', 'aaac', 'aaaaad'); sum_of_words('jjj', 'aaa', 'jjj'); sum_of_words('a', 'jjj', 'h'); sub sum_of_words { my (@string, @number); # initialise @string = @_; say qq[\nInput: ('$string[0]', '$string[1]', '$string[2]')]; # check for truth $number[$_] = number($string[$_]) for 0 .. 2; say qq[Output: ] . ($number[2] == ($number[0] + $number[1]) ? 'true' : 'false'); # 'proof' say qq[ $number[0] + $number[1] = ] . ($number[0] + $number[1]) . qq[ versus $number[2]]; } sub number { # converts string to integer (a -> 0 ... j -> 9) my $n = 0; $n = $n * 10 + ord($_) - ord('a') for split(//, $_[0]); return $n; }
13 lines of code
Input: ('abc', 'def', 'dfh') Output1: true Output2: false 12 + 345 = 357 versus 357 Input: ('cde', 'fgh', 'aaaaa') Output1: false Output2: false 234 + 567 = 801 versus 0 Input: ('deaf', 'bedfig', 'begjjb') Output1: true Output2: true 3405 + 143586 = 146991 versus 146991 Input: ('aab', 'aaac', 'aaaaad') Output1: true Output2: true 1 + 2 = 3 versus 3 Input: ('jjj', 'aaa', 'jjj') Output1: true Output2: true 999 + 0 = 999 versus 999 Input: ('a', 'jjj', 'h') Output1: false Output2: false 0 + 999 = 999 versus 7
Any content of this website which has been created by Peter Campbell Smith is in the public domain