diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-01 12:36:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-01 12:36:22 +0000 |
| commit | a3b05bd5c4e041a80a1cb5e1f566daf884b5f261 (patch) | |
| tree | d8a4846d923d6f995d01fc92ec69650029d58572 | |
| parent | 5ab9569065bc8be286efedb2ad391a432813aaa4 (diff) | |
| parent | 1130a4ed7ed676c28230c57e69a068f5f8f6ae02 (diff) | |
| download | perlweeklychallenge-club-a3b05bd5c4e041a80a1cb5e1f566daf884b5f261.tar.gz perlweeklychallenge-club-a3b05bd5c4e041a80a1cb5e1f566daf884b5f261.tar.bz2 perlweeklychallenge-club-a3b05bd5c4e041a80a1cb5e1f566daf884b5f261.zip | |
Merge pull request #7014 from poti1/tim189
Challenge 189.
| -rwxr-xr-x | challenge-189/tim-potapov/perl/ch-1.pl | 63 | ||||
| -rwxr-xr-x | challenge-189/tim-potapov/perl/ch-2.pl | 91 |
2 files changed, 154 insertions, 0 deletions
diff --git a/challenge-189/tim-potapov/perl/ch-1.pl b/challenge-189/tim-potapov/perl/ch-1.pl new file mode 100755 index 0000000000..3c0aeb661d --- /dev/null +++ b/challenge-189/tim-potapov/perl/ch-1.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use List::Util qw( minstr ); + +=pod + + Task 1: Greater Character + You are given an array of characters (a..z) + and a target character. + + Write a script to find out the smallest + character in the given array lexicographically + greater than the target character. + +=cut + +sub greater_char { + my ( $array, $target ) = @_; + minstr(grep { $_ gt $target } @$array) // $target; +} + +my @cases = ( + { + Name => 'Example 1', + Array => [qw/e m u g/], + Target => 'b', + Output => 'e', + }, + { + Name => 'Example 2', + Array => [qw/d c e f/], + Target => 'a', + Output => 'c', + }, + { + Name => 'Example 3', + Array => [qw/j a r/], + Target => 'o', + Output => 'r', + }, + { + Name => 'Example 4', + Array => [qw/d c a f/], + Target => 'a', + Output => 'c', + }, + { + Name => 'Example 5', + Array => [qw/t g a l/], + Target => 'v', + Output => 'v', + }, +); + +for ( @cases ) { + my $output = greater_char( @$_{qw/ Array Target /} ); + is $output, $_->{Output}, $_->{Name}; +} + +done_testing(); diff --git a/challenge-189/tim-potapov/perl/ch-2.pl b/challenge-189/tim-potapov/perl/ch-2.pl new file mode 100755 index 0000000000..7959519568 --- /dev/null +++ b/challenge-189/tim-potapov/perl/ch-2.pl @@ -0,0 +1,91 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use List::Util qw( max ); +use List::MoreUtils qw( first_index last_index ); + +=pod + + Task 2: Array Degree + You are given an array of 2 or more + non-negative integers. + + Write a script to find out the smallest slice, + i.e. contiguous subarray of the original array, + having the degree of the given array. + + The degree of an array is the maximum frequency + of an element in the array. + +=cut + +sub smallest_slice_of_degree { + my ( $array ) = @_; + + my %frequency; + $frequency{$_}++ for @$array; + + my $degree = max values %frequency; + + my @nums = + grep { $frequency{$_} == $degree } + keys %frequency; + + my @smallest_slice; + for my $num ( @nums ) { + my $i1 = first_index { $_ == $num } @$array; + my $i2 = last_index { $_ == $num } @$array; + my @slice = @$array[ $i1 .. $i2 ]; + if ( !@smallest_slice or @slice < @smallest_slice ) { + @smallest_slice = @slice; + } + } + + \@smallest_slice; +} + +my @cases = ( + + # The degree of the given array is 2. + # The possible subarrays having the degree 2 + # are as below: + # (3, 3) + # (1, 3, 3) + # (3, 3, 2) + # (1, 3, 3, 2) + # And the smallest of all is (3, 3). + { + Name => 'Example 1', + Input => [ 1, 3, 3, 2 ], + Output => [ 3, 3 ], + }, + { + Name => 'Example 2', + Input => [ 1, 2, 1, 3 ], + Output => [ 1, 2, 1 ], + }, + { + Name => 'Example 3', + Input => [ 1, 3, 2, 1, 2 ], + Output => [ 2, 1, 2 ], + }, + { + Name => 'Example 4', + Input => [ 1, 1, 2, 3, 2 ], + Output => [ 1, 1 ], + }, + { + Name => 'Example 5', + Input => [ 2, 1, 2, 1, 1 ], + Output => [ 1, 2, 1, 1 ], + }, +); + +for ( @cases ) { + my $output = smallest_slice_of_degree( $_->{Input} ); + is_deeply $output, $_->{Output}, $_->{Name}; +} + +done_testing(); |
