Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Factors: Add support for negative numbers. Add negative cases and tidy (
Browse files Browse the repository at this point in the history
#4245) (#4299)

* Add support for negative numbers. Add negative cases and tidy (#4245)

* Remove extraneous regex match
  • Loading branch information
g33kgrrl authored and moollaza committed Jul 13, 2017
1 parent 6307864 commit b9297e0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
24 changes: 17 additions & 7 deletions lib/DDG/Goodie/Factors.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package DDG::Goodie::Factors;

# ABSTRACT: Returns the factors of the entered number

use strict;
Expand All @@ -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+)$/;

# 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", structured_answer => {
return "Factors of $query: $factors_list", structured_answer => {
data => {
title => $factors,
title => $factors_list,
subtitle => "Factors of: $query"
},
templates => {
group => 'text'
}
templates => { group => 'text' }
};
};

Expand Down
59 changes: 44 additions & 15 deletions t/Factors.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit b9297e0

Please sign in to comment.