diff options
| -rw-r--r-- | challenge-266/lance-wicks/perl/lib/UCW.pm | 38 | ||||
| -rw-r--r-- | challenge-266/lance-wicks/perl/t/01-ucw.t | 24 |
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-266/lance-wicks/perl/lib/UCW.pm b/challenge-266/lance-wicks/perl/lib/UCW.pm new file mode 100644 index 0000000000..58f81a66b7 --- /dev/null +++ b/challenge-266/lance-wicks/perl/lib/UCW.pm @@ -0,0 +1,38 @@ +package UCW; + +=pod + +> A word is uncommon if it appears exactly once +> in one of the sentences and doesn’t appear in +> other sentence. + +Example 1 + +Input: $line1 = 'Mango is sweet' + $line2 = 'Mango is sour' +Output: ('sweet', 'sour') +=cut + +sub words { + my $self = shift; + my $line1 = shift; + my $line2 = shift; + + my @line1_words = split( /\s+/, $line1 ); + my @line2_words = split( /\s+/, $line2 ); + + my %words; + for ( @line1_words, @line2_words ) { + $words{$_}++; + } + + my @words; + for ( keys %words ) { + if ( $words{$_} == 1 ) { + push @words, $_; + } + } + return [ sort @words ]; +} + +1;
\ No newline at end of file diff --git a/challenge-266/lance-wicks/perl/t/01-ucw.t b/challenge-266/lance-wicks/perl/t/01-ucw.t new file mode 100644 index 0000000000..270d4039b4 --- /dev/null +++ b/challenge-266/lance-wicks/perl/t/01-ucw.t @@ -0,0 +1,24 @@ +use Test2::V0 -target => 'UCW'; + +subtest 'Example 1' => sub { + my $line1 = 'Mango is sweet'; + my $line2 = 'Mango is sour'; + + is $CLASS->words( $line1, $line2 ), [ 'sour', 'sweet' ]; +}; + +subtest 'Example 2' => sub { + my $line1 = 'Mango Mango'; + my $line2 = 'Orange'; + + is $CLASS->words( $line1, $line2 ), ['Orange']; +}; + +subtest 'Example 3' => sub { + my $line1 = 'Mango is Mango'; + my $line2 = 'Orange is Orange'; + + is $CLASS->words( $line1, $line2 ), []; +}; + +done_testing;
\ No newline at end of file |
