Merge remote branch 'upstream/master'

master
digit4lfa1l 2012-12-10 16:41:33 -05:00
commit e4b474c231
4 changed files with 75 additions and 4 deletions

View File

@ -7,8 +7,8 @@ triggers startend => "choose";
zci answer_type => "rand";
primary_example_queries 'yes or no';
secondary_example_queries 'this or that or none';
primary_example_queries 'choose yes or no';
secondary_example_queries 'choose heads or tails', 'choose this or that or none';
description 'make a random choice';
name 'ABC';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/ABC.pm';

View File

@ -11,6 +11,7 @@ package DDG::Goodie::ResistorColors;
use DDG::Goodie;
use Math::Round;
use POSIX qw(abs floor log10 pow);
use utf8;
# \x{2126} is the unicode ohm symbol
triggers query_nowhitespace => qr/^(.*)(ohm|ohms|\x{2126})/i;
@ -19,11 +20,11 @@ zci is_cached => 1;
zci answer_type => 'resistor_colors';
primary_example_queries '4.7k ohm';
secondary_example_queries '1\x{2126}';
secondary_example_queries '1';
description 'find resistor color bands';
name 'ResistorColors';
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/ResistorColors.pm';
category 'conversions';
category 'reference';
topics 'science';
attribution twitter => 'joewalnes',

49
lib/DDG/Goodie/Sort.pm Normal file
View File

@ -0,0 +1,49 @@
package DDG::Goodie::Sort;
# ABSTRACT: Sort a sequence of signed numbers
use strict;
use DDG::Goodie;
# Used to restrict long inputs
use constant MAX_LIST_SIZE => 32;
# Whether to convert numbers from strings to real numbers (1) or not (0)
use constant NORMALIZE => 1;
triggers start => 'sort';
zci is_cached => 1;
zci answer_type => 'sort';
primary_example_queries 'sort -3 -10 56 10';
secondary_example_queries 'sort descending 10, -1, +5.3, -95, 1';
description 'Return the given numbers list in a sorted order.';
name 'Sort';
code_url 'http://github.com/koosha--';
category 'computing_tools';
topics 'programming';
attribution github => ['https://github.com/koosha--', 'koosha--'],
twitter => '_koosha_';
handle remainder => sub {
my $input = $_;
$input =~ s/^\s+|[\s,;]+$|\s+[({[]+|[})]]\s+$//;
my $number_re = qr/[-+]?(?:\d+|(?:\d*\.\d+))/;
my $ascending = 1;
if ($input =~ /^(?:asc|desc)(?:ending(?:ly)?)?/i) {
$ascending = 0 if $input =~ /^desc/i;
$input =~ s/^(?:asc|desc)(?:ending(?:ly)?)?\s*//i;
}
if ($input =~ /^$number_re(?:[\s,;]+$number_re)+$/) {
my @numbers = split /[\s,;]+/, $input;
if (scalar @numbers > MAX_LIST_SIZE) {
@numbers = @numbers[0..MAX_LIST_SIZE - 1];
}
@numbers = map {$_ + 0} @numbers if NORMALIZE;
my @sorted = sort { $ascending ? $a <=> $b : $b <=> $a } @numbers;
my $list = join(', ', @sorted);
return sprintf("$list (Sorted %s)", $ascending ? 'ascendingly' : 'descendingly');
}
return;
};
1;

21
t/Sort.pm Normal file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'sort';
zci is_cached => 1;
ddg_goodie_test(
[qw(
DDG::Goodie::Sort
)],
'sort -1, +4, -3, 5.7' => test_zci('-3, -1, 4, 5.7 (Sorted ascendingly)'),
'sort [-1, +4, -3, 5.7]' => test_zci('-3, -1, 4, 5.7 (Sorted ascendingly)'),
'sort (-1, +4, -3, 5.7)' => test_zci('-3, -1, 4, 5.7 (Sorted ascendingly)'),
'sort desc -4.4 .5 1 66 15 -55' => test_zci('66, 15, 1, 0.5, -4.4, -55 (Sorted descendingly)'),
);
done_testing;