Task1
We find the second largest number after filtering alphabetic characters:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(uniq);
use Data::Show;
sub second_largest_digit{
(sort {$b <=> $a} uniq $_[0] =~ /\d/g)[1] // -1
}
show second_largest_digit('aaaaa77777');
show second_largest_digit('abcde');
show second_largest_digit('9zero8eight7seven9');
show second_largest_digit('xyz9876543210');
show second_largest_digit('4abc4def2ghi8jkl2');
Task2
We calculate the sum of each character and see if the sum of first two is equal the third string:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(reduce);
sub f{
reduce {$a * 10 - (ord($b) - ord('a'))} 0, split '',$_[0];
}
sub sum_of_words{
f($_[0]) + f($_[1]) == f($_[2])
}
show sum_of_words('acb','cba','cdb');
show sum_of_words('aab','aac','ad');
show sum_of_words('bc','je','jg');
show sum_of_words('a','aaaa','aa');
show sum_of_words('c','d','h');
show sum_of_words('gfi','hbf','bdhd');