sumOfNaturalNumbers: Convert to structured answer (#4255)
* convert sumOfNaturalNumbers to structured answer * cleaned up syntaxmaster
parent
aff77f1509
commit
192047914c
|
@ -27,11 +27,19 @@ handle remainder => sub {
|
||||||
# Check if we're getting integers.
|
# Check if we're getting integers.
|
||||||
return unless $_ =~ /^(?:from )?(\d+)\s*(to|-)\s*(\d+)$/i;
|
return unless $_ =~ /^(?:from )?(\d+)\s*(to|-)\s*(\d+)$/i;
|
||||||
|
|
||||||
if ($1 > $3) {
|
return if ($1 > $3);
|
||||||
return;
|
|
||||||
} else {
|
my $sum = ((($3 * ($3 + 1)) / 2)-(($1 * ($1 - 1)) / 2));
|
||||||
my $sum = ((($3 * ($3 + 1)) / 2)-(($1 * ($1 - 1)) / 2));
|
my ($from_number, $to_number, $sum) = map { commify($_) } ($1, $3, $sum);
|
||||||
return 'Sum of natural numbers from ' . commify($1) . ' to ' . commify($3) . ' is ' . commify($sum) . '.';
|
my $string_answer = 'Sum of natural numbers from ' . $from_number . ' to ' . $to_number;
|
||||||
|
return $string_answer, structured_answer => {
|
||||||
|
data => {
|
||||||
|
title => "$sum",
|
||||||
|
subtitle => $string_answer
|
||||||
|
},
|
||||||
|
templates => {
|
||||||
|
group => 'text'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,25 +1,39 @@
|
||||||
#!/usr/bin/env perl
|
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use Test::More;
|
use Test::More;
|
||||||
use Test::Deep;
|
use Test::Deep;
|
||||||
use DDG::Test::Goodie;
|
use DDG::Test::Goodie;
|
||||||
|
|
||||||
zci is_cached => 1;
|
|
||||||
zci answer_type => 'sum';
|
zci answer_type => 'sum';
|
||||||
|
zci is_cached => 1;
|
||||||
|
|
||||||
|
sub build_sum_answer {
|
||||||
|
my($title, $subtitle) = @_;
|
||||||
|
return $subtitle,
|
||||||
|
structured_answer => {
|
||||||
|
data => {
|
||||||
|
title => "$title",
|
||||||
|
subtitle => $subtitle
|
||||||
|
},
|
||||||
|
templates => {
|
||||||
|
group => 'text',
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
sub build_sum_test { test_zci(build_sum_answer(@_)) }
|
||||||
|
|
||||||
ddg_goodie_test(
|
ddg_goodie_test(
|
||||||
[
|
[
|
||||||
'DDG::Goodie::SumOfNaturalNumbers'
|
'DDG::Goodie::SumOfNaturalNumbers'
|
||||||
],
|
],
|
||||||
'sum 1 to 10' => test_zci('Sum of natural numbers from 1 to 10 is 55.'),
|
'sum 1 to 10' => build_sum_test('55', 'Sum of natural numbers from 1 to 10'),
|
||||||
'sum 55 to 63' => test_zci('Sum of natural numbers from 55 to 63 is 531.'),
|
'sum 55 to 63' => build_sum_test('531', 'Sum of natural numbers from 55 to 63'),
|
||||||
'add 33 to 100' => test_zci('Sum of natural numbers from 33 to 100 is 4,522.'),
|
'add 33 to 100' => build_sum_test('4,522', 'Sum of natural numbers from 33 to 100'),
|
||||||
'sum 1-10' => test_zci('Sum of natural numbers from 1 to 10 is 55.'),
|
'sum 1-10' => build_sum_test('55', 'Sum of natural numbers from 1 to 10'),
|
||||||
'sum from 1 to 10' => test_zci('Sum of natural numbers from 1 to 10 is 55.'),
|
'sum from 1 to 10' => build_sum_test('55', 'Sum of natural numbers from 1 to 10'),
|
||||||
'1-10 sum' => test_zci('Sum of natural numbers from 1 to 10 is 55.'),
|
'1-10 sum' => build_sum_test('55', 'Sum of natural numbers from 1 to 10'),
|
||||||
'add from 1 to 100' => test_zci('Sum of natural numbers from 1 to 100 is 5,050.'),
|
'add from 1 to 100' => build_sum_test('5,050', 'Sum of natural numbers from 1 to 100'),
|
||||||
|
|
||||||
# Invalid Input
|
# Invalid Input
|
||||||
'sum 1 --- 10' => undef,
|
'sum 1 --- 10' => undef,
|
||||||
|
|
Loading…
Reference in New Issue