functionality complete with tests

master
Brad Steinbacher 2013-02-24 21:17:04 -05:00
parent 06be8b18b7
commit 968ba6afc6
2 changed files with 43 additions and 7 deletions

View File

@ -8,12 +8,16 @@ triggers start => "anagram", "anagrams";
handle remainder => sub {
my $in = $_;
my $n;
my @output;
my $full_word = 1;
if(/^\s*([a-zA-Z]+)\s*([0-9]+)?\s*$/) {
my $word = lc($1);
my $n = length $word;
$n = $2 if $2 and ($2 <= $n && $2 > 0);
$in = $word;
$n = length $word;
$n = $2 if ($2 && $2 <= $n && $2 > 0);
$full_word = 0 if $n != length($word);
my %freq;
for (split //, $word) {
@ -50,12 +54,21 @@ handle remainder => sub {
}
}
}
return join(", ", @output) if @output;
# copied verbatim from Anagram.pm
my @chars = split(//, $in); #convert each character of the query to an array element
my @garbledChars = shuffle(@chars); #randomly reorder the array
my $garbledAnswer = join('',@garbledChars); #convert array to string
return $garbledAnswer;
@chars = shuffle(@chars); #randomly reorder the array
my $garbledAnswer = '"'.$in.'" garbled: '.join('',@chars);
# end Anagram.pm
if($full_word) {
if(@output) {
my $ana = "anagram: ";
$ana = "anagrams: " if scalar(@output) > 1;
return $garbledAnswer.", with proper ".$ana.join(', ', @output);
}
return $garbledAnswer;
}
return "Anagrams of $in of size $n: ".join(', ', @output);
};
zci is_cached => 0;

23
t/AnagramSolver.t Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'anagramsolver';
zci is_cached => 0;
ddg_goodie_test(
[qw(DDG::Goodie::AnagramSolver)],
'anagram filter' => test_zci(qr/\"filter\" garbled: [filter]{6}, with proper anagram: trifle/),
'anagrams filter' => test_zci(qr/\"filter\" garbled: [filter]{6}, with proper anagram: trifle/),
'anagram filter 5' => test_zci("Anagrams of filter of size 5: filet, flier, flirt, lifer, liter, refit, rifle"),
'anagram favorite' => test_zci(qr/\"favorite\" garbled: [favorite]{8}/),
'anagram times' => test_zci(qr/\"times\" garbled: [times]{5}, with proper anagrams: emits, items, mites, smite/),
'anagram algorithm' => test_zci(qr/\"algorithm\" garbled: [algorithm]{9}, with proper anagram: logarithm/),
'anagram Mixing it up' => test_zci(qr/\"Mixing it up\" garbled: [ Mixngtup]{12}/),
'anagram algorithm 14' => test_zci(qr/\"algorithm\" garbled: [algorithm]{9}, with proper anagram: logarithm/)
);
done_testing;