* Add support for negative numbers. Add negative cases and tidy (#4245) * Remove extraneous regex matchmaster
parent
6307864f65
commit
b9297e004a
|
@ -1,4 +1,5 @@
|
|||
package DDG::Goodie::Factors;
|
||||
|
||||
# ABSTRACT: Returns the factors of the entered number
|
||||
|
||||
use strict;
|
||||
|
@ -13,18 +14,27 @@ triggers startend => 'factors', 'factors of';
|
|||
|
||||
handle remainder => sub {
|
||||
my $query = $_;
|
||||
return unless $query =~ /^\d+$/;
|
||||
|
||||
my $factors = join ', ', divisors($query);
|
||||
return unless $query =~ /^(-)?(\d+)$/;
|
||||
|
||||
return "Factors of $query: $factors", structured_answer => {
|
||||
# The divisors method cannot handle negative numbers, so find the
|
||||
# query magnitude and call it with that instead. Then if the
|
||||
# query number is negative, find and include negative factors.
|
||||
my $negative = $1;
|
||||
my $query_mag = $2;
|
||||
my @factors = divisors($query_mag);
|
||||
|
||||
unshift @factors, sort { $a <=> $b } map { -$_ } @factors
|
||||
if $negative;
|
||||
|
||||
my $factors_list = join ', ', @factors;
|
||||
|
||||
return "Factors of $query: $factors_list", structured_answer => {
|
||||
data => {
|
||||
title => $factors,
|
||||
title => $factors_list,
|
||||
subtitle => "Factors of: $query"
|
||||
},
|
||||
templates => {
|
||||
group => 'text'
|
||||
}
|
||||
templates => { group => 'text' }
|
||||
};
|
||||
};
|
||||
|
||||
|
|
59
t/Factors.t
59
t/Factors.t
|
@ -9,26 +9,55 @@ use DDG::Test::Goodie;
|
|||
zci answer_type => "factors";
|
||||
zci is_cached => 1;
|
||||
|
||||
sub build_test
|
||||
{
|
||||
my ($text_answer, $input, $answer) = @_;
|
||||
return test_zci($text_answer, structured_answer =>{
|
||||
data => {
|
||||
title => $answer,
|
||||
subtitle => "Factors of: $input"
|
||||
},
|
||||
templates => {
|
||||
group => 'text'
|
||||
sub build_test {
|
||||
my ( $text_answer, $input, $answer ) = @_;
|
||||
|
||||
return test_zci(
|
||||
$text_answer,
|
||||
structured_answer => {
|
||||
data => {
|
||||
title => $answer,
|
||||
subtitle => "Factors of: $input"
|
||||
},
|
||||
templates => { group => 'text' }
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw( DDG::Goodie::Factors)],
|
||||
'30 factors' => build_test('Factors of 30: 1, 2, 3, 5, 6, 10, 15, 30', '30', '1, 2, 3, 5, 6, 10, 15, 30'),
|
||||
'factors of 72' => build_test('Factors of 72: 1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72', '72', '1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72'),
|
||||
'factors of 30' => build_test('Factors of 30: 1, 2, 3, 5, 6, 10, 15, 30', '30', '1, 2, 3, 5, 6, 10, 15, 30'),
|
||||
'72 factors' => build_test('Factors of 72: 1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72', '72', '1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72'),
|
||||
'30 factors' => build_test(
|
||||
'Factors of 30: 1, 2, 3, 5, 6, 10, 15, 30',
|
||||
'30',
|
||||
'1, 2, 3, 5, 6, 10, 15, 30'
|
||||
),
|
||||
'factors of 72' => build_test(
|
||||
'Factors of 72: 1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72',
|
||||
'72',
|
||||
'1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72'
|
||||
),
|
||||
'factors of 30' => build_test(
|
||||
'Factors of 30: 1, 2, 3, 5, 6, 10, 15, 30',
|
||||
'30',
|
||||
'1, 2, 3, 5, 6, 10, 15, 30'
|
||||
),
|
||||
'72 factors' => build_test(
|
||||
'Factors of 72: 1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72',
|
||||
'72',
|
||||
'1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72'
|
||||
),
|
||||
'factors of -6' => build_test(
|
||||
'Factors of -6: -6, -3, -2, -1, 1, 2, 3, 6',
|
||||
'-6',
|
||||
'-6, -3, -2, -1, 1, 2, 3, 6'
|
||||
),
|
||||
'-12 factors' => build_test(
|
||||
'Factors of -12: -12, -6, -4, -3, -2, -1, 1, 2, 3, 4, 6, 12',
|
||||
'-12',
|
||||
'-12, -6, -4, -3, -2, -1, 1, 2, 3, 4, 6, 12'
|
||||
),
|
||||
'factors of 2.4' => undef,
|
||||
'factors of fear' => undef,
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue