Task1
We parse complex numbers and apply the given formula:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub parse_complex{
my ($s) = @_;
if ($s =~ /^\s*(-?\d+)\s*\+\s*\(?\s*(-?\d+)\s*\)?\s*i\s*$/){
return ($1, $2);
}
die "Invalid complex number: '$s'\n"
}
sub format_complex{
my ($re,$im) = @_;
my $str = $im < 0 ? "($im)" : "$im";
"$re+${str}i"
}
sub complex_number{
my ($n1,$n2) = @_;
my ($a,$b) = parse_complex($n1);
my ($c,$d) = parse_complex($n2);
my $real = $a * $c - $b * $d;
my $imag = $a * $d + $b * $c;
format_complex($real,$imag)
}
is complex_number('1+1i','1+1i'),'0+2i','Example 1';
is complex_number('1+(-1)i','1+(-1)i'),'0+(-2)i','Example 2';
is complex_number('0+3i','0+4i'),'-12+0i','Example 3';
is complex_number('3+4i','3+(-4)i'),'25+0i','Example 4';
is complex_number('50+(-20)i','-10+30i'),'100+1700i','Example 5';
done_testing();
Task2
We use backtracking to find all the solutions:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub word_squares{
my ($words) = @_;
return [] unless @$words;
my ($n,@res,%used) = length $words->[0];
my $solve;
$solve = sub{
my $k = @res;
return 1 if $k == $n;
my $prefix = join '',map{substr($res[$_],$k,1)} 0..$k-1;
foreach my $i(0..$#$words){
next if $used{$i};
next unless substr($words->[$i],0,$k) eq $prefix;
$used{$i} = 1;
push @res,$words->[$i];
return 1 if $solve->();
pop @res;
delete $used{$i}
}
0
};
my $found = $solve->();
undef $solve;
$found ? \@res : [];
}
is_deeply word_squares(['MA','AM','MY','ME']),['MA','AM'],'Example 1';
is_deeply word_squares(['SUN','USE','NET','SEE','TEN']),
['SUN','USE','NET'],'Example 2';
is_deeply word_squares(['BOAT','TEAL','AREA','TSAR','OGRE']),
['BOAT','OGRE','AREA','TEAL'],'Example 3';
is_deeply word_squares(['CARD','AREA','REAR','DART']),
['CARD','AREA','REAR','DART'],'Example 4';
is_deeply word_squares(['HEART','TREND','EMBER','RESIN','ABUSE']),
['HEART','EMBER','ABUSE','RESIN','TREND'],'Example 5';
done_testing();