Add the Sort goodie
parent
514faf51a1
commit
b3ad72ad6a
|
@ -0,0 +1,47 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
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+//;
|
||||||
|
$input =~ s/[\s,;]+$//;
|
||||||
|
my $number_re = qr/[-+]?(?:\d+|(?:\d*\.\d+))/;
|
||||||
|
my $ascending = 1;
|
||||||
|
if ($input =~ /^(?:asc|desc)(?:ending)?/i) {
|
||||||
|
$ascending = 0 if $input =~ /^desc/i;
|
||||||
|
$input =~ s/^(?:asc|desc)(?:ending)?\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];
|
||||||
|
}
|
||||||
|
my @sorted = sort { $ascending ? $a <=> $b : $b <=> $a } @numbers;
|
||||||
|
my $list = join(' ', @sorted);
|
||||||
|
return sprintf("$list (Sorted %s)", $ascending ? 'ascendingly' : 'descendingly');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
1;
|
|
@ -0,0 +1,19 @@
|
||||||
|
#!/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 desc -4.4 .5 1 66 15 -55' => test_zci('66 15 1 .5 -4.4 -55 (Sorted descendingly)'),
|
||||||
|
);
|
||||||
|
|
||||||
|
done_testing;
|
Loading…
Reference in New Issue