diff options
| -rw-r--r-- | challenge-265/kjetillll/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-265/kjetillll/perl/ch-2.pl | 42 |
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-265/kjetillll/perl/ch-1.pl b/challenge-265/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..90d01d0826 --- /dev/null +++ b/challenge-265/kjetillll/perl/ch-1.pl @@ -0,0 +1,32 @@ +use strict; use warnings; use Test::More; + +sub a33 { + my %times; + ( + sort { $a <=> $b} + grep { 100 * ++$times{$_} / @_ >= 33 } + @_ + ) [0] +} + + + +my @tests = ( + { + input => [1,2,3,3,3,3,4,2], + output => 3 + }, + { + input => [1,1], + output => 1 + }, + { + input => [1,2,3], + output => 1 + } +); +for my $test ( @tests ){ + my $got = a33( @{ $$test{input} } ); + is( $got, $$test{output} ); +} +done_testing; diff --git a/challenge-265/kjetillll/perl/ch-2.pl b/challenge-265/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..360a3d33d8 --- /dev/null +++ b/challenge-265/kjetillll/perl/ch-2.pl @@ -0,0 +1,42 @@ +use strict; use warnings; use Test::More; + +sub shortest_completing { + my($str, @array) = @_; + ( + sort { length($a) <=> length($b) or $a cmp $b } + grep { is_completing($str, $_) } + @array + )[0] +} + +sub is_completing { + my( $str1, $str2 ) = @_; + return 1 if $str1 !~ s/[a-z]//i; #draw a letter out of str1, return true if no letter found + return 0 if $str2 !~ s/$&//i; #draw same letter out of str2, return false if not found + is_completing( $str1, $str2 ) #...or let the reduced strings decide +} + + + +my @tests = ( + { + input => 'aBc 11c', + array => ['accbbb', 'abc', 'abbc'], + output => 'accbbb' + }, + { + input => 'Da2 abc', + array => ['abcm', 'baacd', 'abaadc'], + output => 'baacd' + }, + { + input => 'JB 007', + array => ['jj', 'bb', 'bjb'], + output => 'bjb' + } +); +for my $test ( @tests ) { + my $got = shortest_completing( $$test{input}, @{ $$test{array} } ); + is( $got, $$test{output} ); +} +done_testing; |
