Make the output of Fibonacci verbose

master
Koosha Khajeh Moogahi 2013-01-24 12:50:34 +03:30
parent e5d92a1060
commit 16c629c738
2 changed files with 8 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package DDG::Goodie::Fibonacci;
use strict; use strict;
use DDG::Goodie; use DDG::Goodie;
use Lingua::EN::Numbers::Ordinate qw(ordsuf);
triggers any => 'fib', 'fibonacci'; triggers any => 'fib', 'fibonacci';
zci is_cached => 1; zci is_cached => 1;
@ -29,7 +30,9 @@ handle remainder => sub {
for my $i (2..$#fib) { for my $i (2..$#fib) {
$fib[$i] = $fib[$i - 1] + $fib[$i - 2]; $fib[$i] = $fib[$i - 1] + $fib[$i - 2];
} }
return "fib($_) = ${fib[$_]} with f(0) = 0"; my $suf = ordsuf($_);
return "The $_$suf fibonacci number is ${fib[$_]} (assuming f(0) = 0).",
html => "The $_<sup>$suf</sup> fibonacci number is ${fib[$_]} (assuming f(0) = 0).";
}; };
1; 1;

View File

@ -7,8 +7,10 @@ zci is_cached => 1;
ddg_goodie_test( ddg_goodie_test(
[qw(DDG::Goodie::Fibonacci)], [qw(DDG::Goodie::Fibonacci)],
'fib 7' => test_zci('fib(7) = 13 with f(0) = 0'), 'fib 7' => test_zci('The 7th fibonacci number is 13 (assuming f(0) = 0).',
'fibonacci 33' => test_zci('fib(33) = 3524578 with f(0) = 0'), html => 'The 7<sup>th</sup> fibonacci number is 13 (assuming f(0) = 0).'),
'fibonacci 33' => test_zci('The 33rd fibonacci number is 3524578 (assuming f(0) = 0).',
html => 'The 33<sup>rd</sup> fibonacci number is 3524578 (assuming f(0) = 0).'),
); );
done_testing; done_testing;