From b3ad72ad6a81f54d8b883f0a690153623bc08ea8 Mon Sep 17 00:00:00 2001 From: Koosha Khajeh Moogahi Date: Wed, 5 Dec 2012 23:47:45 +0330 Subject: [PATCH 1/4] Add the Sort goodie --- lib/DDG/Goodie/Sort.pm | 47 ++++++++++++++++++++++++++++++++++++++++++ t/Sort.pm | 19 +++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 lib/DDG/Goodie/Sort.pm create mode 100644 t/Sort.pm diff --git a/lib/DDG/Goodie/Sort.pm b/lib/DDG/Goodie/Sort.pm new file mode 100644 index 000000000..0bdad8686 --- /dev/null +++ b/lib/DDG/Goodie/Sort.pm @@ -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; diff --git a/t/Sort.pm b/t/Sort.pm new file mode 100644 index 000000000..e9a7f45ed --- /dev/null +++ b/t/Sort.pm @@ -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; From 2856e2614187e33d574ac87bc1eed4da96109efe Mon Sep 17 00:00:00 2001 From: Koosha Khajeh Moogahi Date: Thu, 6 Dec 2012 00:47:01 +0330 Subject: [PATCH 2/4] Change the output delimiter from space to comma --- lib/DDG/Goodie/Sort.pm | 2 +- t/Sort.pm | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/DDG/Goodie/Sort.pm b/lib/DDG/Goodie/Sort.pm index 0bdad8686..b9dab4a9d 100644 --- a/lib/DDG/Goodie/Sort.pm +++ b/lib/DDG/Goodie/Sort.pm @@ -38,7 +38,7 @@ handle remainder => sub { @numbers = @numbers[0..MAX_LIST_SIZE - 1]; } my @sorted = sort { $ascending ? $a <=> $b : $b <=> $a } @numbers; - my $list = join(' ', @sorted); + my $list = join(', ', @sorted); return sprintf("$list (Sorted %s)", $ascending ? 'ascendingly' : 'descendingly'); } return; diff --git a/t/Sort.pm b/t/Sort.pm index e9a7f45ed..45dea4a1c 100644 --- a/t/Sort.pm +++ b/t/Sort.pm @@ -12,8 +12,8 @@ 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)'), + '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; From bbbcdc513c7f85629b25393bafeeb2cd95eb32d6 Mon Sep 17 00:00:00 2001 From: Koosha Khajeh Moogahi Date: Thu, 6 Dec 2012 12:48:41 +0330 Subject: [PATCH 3/4] Normalize the output format --- lib/DDG/Goodie/Sort.pm | 7 +++++-- t/Sort.pm | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/DDG/Goodie/Sort.pm b/lib/DDG/Goodie/Sort.pm index b9dab4a9d..3fe4d9e95 100644 --- a/lib/DDG/Goodie/Sort.pm +++ b/lib/DDG/Goodie/Sort.pm @@ -6,6 +6,8 @@ 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'; @@ -28,15 +30,16 @@ handle remainder => sub { $input =~ s/[\s,;]+$//; my $number_re = qr/[-+]?(?:\d+|(?:\d*\.\d+))/; my $ascending = 1; - if ($input =~ /^(?:asc|desc)(?:ending)?/i) { + if ($input =~ /^(?:asc|desc)(?:ending(?:ly)?)?/i) { $ascending = 0 if $input =~ /^desc/i; - $input =~ s/^(?:asc|desc)(?:ending)?\s*//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'); diff --git a/t/Sort.pm b/t/Sort.pm index 45dea4a1c..5db1af64e 100644 --- a/t/Sort.pm +++ b/t/Sort.pm @@ -12,8 +12,8 @@ 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)'), + '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; From 1b17f84346c879f43247d3475c9264d33b3455e1 Mon Sep 17 00:00:00 2001 From: Dylan Lloyd Date: Fri, 7 Dec 2012 14:11:27 -0500 Subject: [PATCH 4/4] add support to Sort for common array/list syntax --- lib/DDG/Goodie/Sort.pm | 3 +-- t/Sort.pm | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/DDG/Goodie/Sort.pm b/lib/DDG/Goodie/Sort.pm index 3fe4d9e95..ac2b5d4ed 100644 --- a/lib/DDG/Goodie/Sort.pm +++ b/lib/DDG/Goodie/Sort.pm @@ -26,8 +26,7 @@ attribution github => ['https://github.com/koosha--', 'koosha--'], handle remainder => sub { my $input = $_; - $input =~ s/^\s+//; - $input =~ s/[\s,;]+$//; + $input =~ s/^\s+|[\s,;]+$|\s+[({[]+|[})]]\s+$//; my $number_re = qr/[-+]?(?:\d+|(?:\d*\.\d+))/; my $ascending = 1; if ($input =~ /^(?:asc|desc)(?:ending(?:ly)?)?/i) { diff --git a/t/Sort.pm b/t/Sort.pm index 5db1af64e..3fe0b9f31 100644 --- a/t/Sort.pm +++ b/t/Sort.pm @@ -13,6 +13,8 @@ ddg_goodie_test( 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)'), );