diff options
| -rw-r--r-- | challenge-215/lance-wicks/perl/ch-1.pl | 11 | ||||
| -rw-r--r-- | challenge-215/lance-wicks/perl/lib/OddOneOut.pm | 23 | ||||
| -rw-r--r-- | challenge-215/lance-wicks/perl/t/ch-1.t | 35 |
3 files changed, 69 insertions, 0 deletions
diff --git a/challenge-215/lance-wicks/perl/ch-1.pl b/challenge-215/lance-wicks/perl/ch-1.pl new file mode 100644 index 0000000000..b09cc30b74 --- /dev/null +++ b/challenge-215/lance-wicks/perl/ch-1.pl @@ -0,0 +1,11 @@ +#!/usr/bin/env perl + +use lib './lib'; +use OddOneOut; + +my $srv = OddOneOut->new; +use Data::Dumper; + +print 'Input: ', join ', ', @ARGV, "\n"; +print ' Output: ', $srv->count(@ARGV), "\n\n"; + diff --git a/challenge-215/lance-wicks/perl/lib/OddOneOut.pm b/challenge-215/lance-wicks/perl/lib/OddOneOut.pm new file mode 100644 index 0000000000..35005a665a --- /dev/null +++ b/challenge-215/lance-wicks/perl/lib/OddOneOut.pm @@ -0,0 +1,23 @@ +package OddOneOut; +use Moo; + +sub count { + my ( $self, @words ) = @_; + my $count = 0; + + map { $count += $self->is_word_not_alpha_order($_) } @words; + + return $count; +} + +sub is_word_not_alpha_order { + my ( $self, $word ) = @_; + + my @letters = split '', $word; + @letters = sort @letters; + my $sorted_word = join '', @letters; + + return 0 + !!( $word ne $sorted_word ); +} + +1. diff --git a/challenge-215/lance-wicks/perl/t/ch-1.t b/challenge-215/lance-wicks/perl/t/ch-1.t new file mode 100644 index 0000000000..3d38ab29c7 --- /dev/null +++ b/challenge-215/lance-wicks/perl/t/ch-1.t @@ -0,0 +1,35 @@ +use Test2::V0 -target => 'OddOneOut'; + +subtest 'Module structure' => sub { + isa_ok( $CLASS, 'OddOneOut' ); +}; + +=pod + +You are given a list of words (alphabetic characters only) of same size. + +Write a script to remove all words not sorted alphabetically and print the number of words in the list that are not alphabetically sorted. + +=cut + +subtest 'Functionality' => sub { + my @words = ( 'abc', 'xyz', 'tsu' ); + my $result = $CLASS->count(@words); + is $result, 1, 'Example 1 should return "1"'; + + @words = ( 'rat', 'cab', 'dad' ); + $result = $CLASS->count(@words); + is $result, 3, 'Example 2 should return "3"'; + + @words = ('x', 'y', 'z'); + $result = $CLASS->count(@words); + is $result, 0, 'Example 3 should return "0"'; + +}; + +subtest 'Is word alpha sorted' => sub { + is $CLASS->is_word_not_alpha_order('abc'), 0, '"abc" is aplha order (0)'; + is $CLASS->is_word_not_alpha_order('cab'), 1, '"cab" is NOT alpha order (1)'; +}; + +done_testing; |
