From b9297e004a2347307543ee711ad61cbd94964b6d Mon Sep 17 00:00:00 2001 From: Lisa Georgiades Date: Thu, 13 Jul 2017 10:12:18 -0700 Subject: [PATCH] Factors: Add support for negative numbers. Add negative cases and tidy (#4245) (#4299) * Add support for negative numbers. Add negative cases and tidy (#4245) * Remove extraneous regex match --- lib/DDG/Goodie/Factors.pm | 24 +++++++++++----- t/Factors.t | 59 +++++++++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/lib/DDG/Goodie/Factors.pm b/lib/DDG/Goodie/Factors.pm index 66b6a7b6d2d..982c0694b0a 100755 --- a/lib/DDG/Goodie/Factors.pm +++ b/lib/DDG/Goodie/Factors.pm @@ -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+)$/; + + # 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' } }; }; diff --git a/t/Factors.t b/t/Factors.t index 289df363fbf..92c817c2f91 100755 --- a/t/Factors.t +++ b/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;