Merge pull request #2775 from duckduckgo/gd/calc-roots-full-template
CalcRoots: Update to use full templatemaster
commit
404420734c
|
@ -11,6 +11,8 @@ zci is_cached => 1;
|
|||
|
||||
zci answer_type => 'root';
|
||||
|
||||
my $UPPER_BOUND = 1000000000; # 1 Billion
|
||||
|
||||
handle query => sub {
|
||||
# The 'root' trigger is very ambigous so this regex provides the specific triggers
|
||||
return unless m/^((?:.*square|.*cube(?:d|)|.*th|.*rd|.*nd|.*st|.*[0-9]+)) root(?: of|) (?!of)(.*)/i;
|
||||
|
@ -53,6 +55,7 @@ handle query => sub {
|
|||
|
||||
# Solve using the absolute value of the base
|
||||
$base = abs($base);
|
||||
return unless $base < $UPPER_BOUND;
|
||||
my $calc = $base ** (1/$exp);
|
||||
if($sign eq "-"){$calc = -$calc;}
|
||||
|
||||
|
@ -85,59 +88,57 @@ handle query => sub {
|
|||
$base = $';
|
||||
$base = str2nbr($base) if $base =~ m/[^0-9]/;
|
||||
$base =~ s/[^0-9\.]//g;
|
||||
if ($base ne '') {
|
||||
return unless $base ne '' && $base < $UPPER_BOUND;
|
||||
|
||||
my $calc = $base ** (1/$exp) * -1;
|
||||
if($sign eq "-"){$calc = -$calc;}
|
||||
my $calc = $base ** (1/$exp) * -1;
|
||||
if($sign eq "-"){$calc = -$calc;}
|
||||
|
||||
my $secondsign = "-"; #sign of the calcuated answer
|
||||
if($calc>0){$secondsign="";}
|
||||
|
||||
my $secondsign = "-"; #sign of the calcuated answer
|
||||
if($calc>0){$secondsign="";}
|
||||
# Try and simplify the radical if answer is not a whole number
|
||||
unless(($calc - int($calc))==0){
|
||||
my $count = int(abs($calc));
|
||||
while ($count > 1) {
|
||||
|
||||
# Try and simplify the radical if answer is not a whole number
|
||||
unless(($calc - int($calc))==0){
|
||||
my $count = int(abs($calc));
|
||||
while ($count > 1) {
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc ($secondsign$count times the $sign$exp-root of $newBase).", qq|$sign<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($secondsign$count⋅<sup>$exp</sup>√$newBase)|);
|
||||
}
|
||||
|
||||
$count--;
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc ($secondsign$count times the $sign$exp-root of $newBase).", qq|$sign<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($secondsign$count⋅<sup>$exp</sup>√$newBase)|);
|
||||
}
|
||||
|
||||
$count--;
|
||||
}
|
||||
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc.", qq|$sign<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
|
||||
}
|
||||
return structured($sign . $exp,"-$base","The $sign$exp-root of -$base is $calc.", qq|$sign<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
|
||||
}
|
||||
elsif ($exp =~ m/[0-9]+/) {
|
||||
|
||||
# Solve normally
|
||||
$base = str2nbr($base) if $base =~ m/[^0-9]/;
|
||||
$base =~ s/[^0-9\.]//g;
|
||||
if ($base ne '') {
|
||||
my $calc = $base ** (1/$exp);
|
||||
if($sign eq "-"){$calc = -$calc;}
|
||||
return unless $base ne '' && $base < $UPPER_BOUND;
|
||||
|
||||
#If the answer is not a whole number, try to simplify the radical
|
||||
unless(($calc - int($calc))==0){
|
||||
my $count = int(abs($calc));
|
||||
while ($count > 1) {
|
||||
my $calc = $base ** (1/$exp);
|
||||
if($sign eq "-"){$calc = -$calc;}
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
#If the answer is not a whole number, try to simplify the radical
|
||||
unless(($calc - int($calc))==0){
|
||||
my $count = int(abs($calc));
|
||||
while ($count > 1) {
|
||||
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc ($sign$count times the $exp-root of $newBase).", qq|$sign<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($sign$count⋅<sup>$exp</sup>√$newBase)|);
|
||||
}
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
|
||||
$count--;
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc ($sign$count times the $exp-root of $newBase).", qq|$sign<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($sign$count⋅<sup>$exp</sup>√$newBase)|);
|
||||
}
|
||||
|
||||
$count--;
|
||||
}
|
||||
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc.", qq|$sign<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
|
||||
}
|
||||
return structured($sign . $exp,$base,"The $sign$exp-root of $base is $calc.", qq|$sign<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|);
|
||||
}
|
||||
|
||||
return;
|
||||
|
@ -146,12 +147,18 @@ handle query => sub {
|
|||
sub structured{
|
||||
my($exp,$base,$text, $html) = @_;
|
||||
return $text,
|
||||
heading => "Root Calculator",
|
||||
structured_answer => {
|
||||
input => ["$exp-root of $base"],
|
||||
operation => 'Calculate',
|
||||
result => $html,
|
||||
};
|
||||
structured_answer => {
|
||||
data => {
|
||||
title => $html,
|
||||
subtitle => "Calculate $exp-root of $base",
|
||||
},
|
||||
templates => {
|
||||
group => 'text',
|
||||
options => {
|
||||
title_content => 'DDH.calc_roots.title',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
<h3 class="c-base__title">{{{title}}}</h3>
|
|
@ -9,45 +9,33 @@ use DDG::Test::Goodie;
|
|||
zci answer_type => 'root';
|
||||
zci is_cached => 1;
|
||||
|
||||
sub build_structured_answer {
|
||||
my ($exp, $base, $text, $html) = @_;
|
||||
return $text,
|
||||
structured_answer => {
|
||||
data => {
|
||||
title => $html,
|
||||
subtitle => "Calculate $exp-root of $base",
|
||||
},
|
||||
templates => {
|
||||
group => 'text',
|
||||
options => {
|
||||
title_content => 'DDH.calc_roots.title',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
sub build_test { test_zci(build_structured_answer(@_)) }
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(
|
||||
DDG::Goodie::CalcRoots
|
||||
)],
|
||||
'square root of negative 9' => test_zci(
|
||||
"The 2-root of -9 is 3 i",
|
||||
heading => "Root Calculator",
|
||||
structured_answer => {
|
||||
input => ["2-root of -9"],
|
||||
operation => 'Calculate',
|
||||
result => "<sup>2</sup>√-9 = 3<em>i</em>"
|
||||
}
|
||||
),
|
||||
'negative square root of negative 25' =>test_zci(
|
||||
"The -2-root of -25 is -5 i",
|
||||
heading => "Root Calculator",
|
||||
structured_answer => {
|
||||
input => ["-2-root of -25"],
|
||||
operation => 'Calculate',
|
||||
result => "-<sup>2</sup>√-25 = -5<em>i</em>"
|
||||
}
|
||||
),
|
||||
'2nd root of 100' => test_zci(
|
||||
"The 2-root of 100 is 10.",
|
||||
heading => "Root Calculator",
|
||||
structured_answer => {
|
||||
input => ["2-root of 100"],
|
||||
operation => 'Calculate',
|
||||
result => qq|<sup>2</sup>√100 = <a href="javascript:;" onclick="document.x.q.value='10';document.x.q.focus();">10</a>|
|
||||
}
|
||||
),
|
||||
'cube root of 33' => test_zci(
|
||||
"The 3-root of 33 is 3.20753432999583.",
|
||||
heading => "Root Calculator",
|
||||
structured_answer => {
|
||||
input => ["3-root of 33"],
|
||||
operation => 'Calculate',
|
||||
result => qq|<sup>3</sup>√33 = <a href="javascript:;" onclick="document.x.q.value='3.20753432999583';document.x.q.focus();">3.20753432999583</a>|
|
||||
}
|
||||
),
|
||||
[qw( DDG::Goodie::CalcRoots )],
|
||||
'square root of negative 9' => build_test('2', '-9', 'The 2-root of -9 is 3 i', '<sup>2</sup>√-9 = 3<em>i</em>'),
|
||||
'negative square root of negative 25' => build_test('-2', '-25', 'The -2-root of -25 is -5 i', '-<sup>2</sup>√-25 = -5<em>i</em>'),
|
||||
'2nd root of 100' => build_test('2', '100', 'The 2-root of 100 is 10.',
|
||||
qq|<sup>2</sup>√100 = <a href="javascript:;" onclick="document.x.q.value='10';document.x.q.focus();">10</a>|),
|
||||
'cube root of 33' => build_test('3', '33', 'The 3-root of 33 is 3.20753432999583.',
|
||||
qq|<sup>3</sup>√33 = <a href="javascript:;" onclick="document.x.q.value='3.20753432999583';document.x.q.focus();">3.20753432999583</a>|),
|
||||
'2nd root of 9999999999' => undef
|
||||
);
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue