Hi everyone!
I’m getting an unexpected result when I submit my solution to the Armstrong Numbers exercise. When I run it locally on my machine, it seems to work fine, but some test are still failing and I don’t really understand why:
is(
is_armstrong_number(153),
T, # True
"Three-digit number that is an Armstrong number"
);
# Failed test 'Three-digit number that is an Armstrong number'
# at /mnt/exercism-iteration/t/armstrong-numbers.t line 28.
# +-----+--------+-------+-----+
# | GOT | OP | CHECK | LNs |
# +-----+--------+-------+-----+
# | | TRUE() | TRUE | 28 |
# +-----+--------+-------+-----+
I’m not sure what T is supposed to be here. I assumed it’s a subroutine to cast a value to a boolean since Perl doesn’t have them (though I think recent versions now do)?
The lasts two tests also seem to be problematic as they are very large numbers that get truncated, but I’m not sure about those either.
Does anybody have any tips? Here’s my solution:
sub get_digits ($number)
{
my @digits = ();
my $q = $number;
my $r = 0;
do {
$r = $q % 10;
$q = $q / 10;
push @digits, $r;
} while ($q >= 1);
return \@digits;
}
sub is_armstrong_number ($number)
{
my $digits = get_digits($number);
my $length = scalar @$digits;
my $result = 0;
foreach my $digit ( @$digits ) {
$result += $digit ** $length;
}
return $result == $number;
}
Thanks!