Merge pull request #1802 from Mailkov/issue1418
Issue #1418 - Factorial: Move integration into Calculator.pmmaster
commit
897a6173db
|
@ -7,6 +7,7 @@ with 'DDG::GoodieRole::NumberStyler';
|
|||
|
||||
use List::Util qw( max );
|
||||
use Math::Trig;
|
||||
use Math::BigInt;
|
||||
use utf8;
|
||||
|
||||
zci answer_type => "calc";
|
||||
|
@ -33,7 +34,7 @@ triggers query_nowhitespace => qr<
|
|||
[\( \) x X × ∙ ⋅ * % + ÷ / \^ 0-9 \. , _ \$ -]*
|
||||
|
||||
(?(1) (?: -? [0-9 \. , _ ]+ |) |)
|
||||
(?: [\( \) x X × ∙ ⋅ * % + ÷ / \^ \$ -] | times | divided by | plus | minus | cos | sin | tan | cotan | log | ln | log[_]?\d{1,3} | exp | tanh | sec | csc | squared | sqrt | pi | e )+
|
||||
(?: [\( \) x X × ∙ ⋅ * % + ÷ / \^ \$ -] | times | divided by | plus | minus | fact | factorial | cos | sin | tan | cotan | log | ln | log[_]?\d{1,3} | exp | tanh | sec | csc | squared | sqrt | pi | e )+
|
||||
|
||||
(?: [0-9 \. ,]* )
|
||||
(?: gross | dozen | pi | e | c | squared | score |)
|
||||
|
@ -59,6 +60,7 @@ my %named_operations = (
|
|||
'÷' => '/',
|
||||
'ln' => 'log', # perl log() is natural log.
|
||||
'squared' => '**2',
|
||||
'fact' => 'Math::BigInt->new',
|
||||
);
|
||||
|
||||
my %named_constants = (
|
||||
|
@ -85,9 +87,11 @@ handle query_nowhitespace => sub {
|
|||
return if ($query =~ /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/); # Probably are searching for a phone number, not making a calculation
|
||||
|
||||
$query =~ s/^(?:whatis|calculate|solve|math)//;
|
||||
$query =~ s/(factorial)/fact/; #replace factorial with fact
|
||||
|
||||
# Grab expression.
|
||||
my $tmp_expr = spacing($query, 1);
|
||||
|
||||
|
||||
return if $tmp_expr eq $query; # If it didn't get spaced out, there are no operations to be done.
|
||||
|
||||
|
@ -114,6 +118,7 @@ handle query_nowhitespace => sub {
|
|||
return unless $style;
|
||||
|
||||
$tmp_expr = $style->for_computation($tmp_expr);
|
||||
$tmp_expr =~ s/(Math::BigInt->new\((.*)\))/(Math::BigInt->new\($2\))->bfac()/g; #correct expression for fact
|
||||
# Using functions makes us want answers with more precision than our inputs indicate.
|
||||
my $precision = ($query =~ $funcy) ? undef : ($query =~ /^\$/) ? 2 : max(map { $style->precision_of($_) } @numbers);
|
||||
my $tmp_result;
|
||||
|
@ -152,6 +157,7 @@ sub prepare_for_display {
|
|||
# Show them how 'E' was interpreted. This should use the number styler, too.
|
||||
$query =~ s/((?:\d+?|\s))E(-?\d+)/\($1 * 10^$2\)/i;
|
||||
$query =~ s/\s*\*{2}\s*/^/g; # Use prettier exponentiation.
|
||||
$query =~ s/(Math::BigInt->new\((.*)\))/fact\($2\)/g; #replace Math::BigInt->new( with fact(
|
||||
$result = $style->for_display($result);
|
||||
foreach my $name (keys %named_constants) {
|
||||
$query =~ s#\($name\)#$name#xig;
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
package DDG::Goodie::Factorial;
|
||||
# ABSTRACT: n factorial
|
||||
|
||||
use strict;
|
||||
use DDG::Goodie;
|
||||
|
||||
use List::Util qw(reduce);
|
||||
use bigint;
|
||||
|
||||
zci answer_type => "factorial";
|
||||
zci is_cached => 1;
|
||||
|
||||
name "Factorial";
|
||||
description "Returns Factorial of n";
|
||||
primary_example_queries "factorial 7", "7 factorial";
|
||||
secondary_example_queries "12 fact";
|
||||
topics 'math';
|
||||
category 'calculations';
|
||||
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Factorial.pm";
|
||||
attribution github => ["https://github.com/codenirvana", "Udit Vasu"],
|
||||
twitter => "uditdistro";
|
||||
|
||||
triggers any => "factorial", "fact";
|
||||
|
||||
handle remainder => sub {
|
||||
my $n = $_;
|
||||
|
||||
return unless $n =~ /^\d+$/;
|
||||
|
||||
$n = int($n);
|
||||
|
||||
my $fact = '1';
|
||||
$fact = reduce { $a * $b } 1 .. $n if ($n > 0);
|
||||
|
||||
return "Factorial of $n is $fact",
|
||||
structured_answer => {
|
||||
input => [$n],
|
||||
operation => 'Factorial',
|
||||
result => $fact
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
1;
|
|
@ -759,6 +759,24 @@ ddg_goodie_test(
|
|||
result => qr"6.28318530717958"
|
||||
}
|
||||
),
|
||||
'fact(3)' => test_zci(
|
||||
'fact(3) = 6',
|
||||
heading => 'Calculator',
|
||||
structured_answer => {
|
||||
input => ['fact(3)'],
|
||||
operation => 'Calculate',
|
||||
result => qr/>6</
|
||||
}
|
||||
),
|
||||
'factorial(3)' => test_zci(
|
||||
'fact(3) = 6',
|
||||
heading => 'Calculator',
|
||||
structured_answer => {
|
||||
input => ['fact(3)'],
|
||||
operation => 'Calculate',
|
||||
result => qr/>6</
|
||||
}
|
||||
),
|
||||
'123.123.123.123/255.255.255.255' => undef,
|
||||
'83.166.167.160/27' => undef,
|
||||
'9 + 0 x 07' => undef,
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
#!/usr/bin/env perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Test::More;
|
||||
use DDG::Test::Goodie;
|
||||
use bigint;
|
||||
|
||||
zci answer_type => "factorial";
|
||||
zci is_cached => 1;
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw( DDG::Goodie::Factorial )],
|
||||
'fact 4' => test_zci(
|
||||
'Factorial of 4 is 24',
|
||||
structured_answer => {
|
||||
input => ['4'],
|
||||
operation => 'Factorial',
|
||||
result => 24
|
||||
}
|
||||
),
|
||||
'factorial 6' => test_zci(
|
||||
'Factorial of 6 is 720',
|
||||
structured_answer => {
|
||||
input => ['6'],
|
||||
operation => 'Factorial',
|
||||
result => 720
|
||||
}
|
||||
),
|
||||
'factorial 0' => test_zci(
|
||||
'Factorial of 0 is 1',
|
||||
structured_answer => {
|
||||
input => ['0'],
|
||||
operation => 'Factorial',
|
||||
result => 1
|
||||
}
|
||||
),
|
||||
'tell a factorial' => undef,
|
||||
'what is factorial?' => undef,
|
||||
'a factorial' => undef,
|
||||
'25abc factorial' => undef,
|
||||
'factorial xyz' => undef,
|
||||
'factorial -9' => undef,
|
||||
'factorial 0!' => undef,
|
||||
'-12 factorial' => undef,
|
||||
'fact -8' => undef,
|
||||
'fact 12!' => undef,
|
||||
'abc fact' => undef,
|
||||
'n8 fact' => undef,
|
||||
);
|
||||
|
||||
done_testing;
|
Loading…
Reference in New Issue