diff options
| -rwxr-xr-x | challenge-208/perlboy1967/perl/ch1.pl | 51 | ||||
| -rwxr-xr-x | challenge-208/perlboy1967/perl/ch2.pl | 51 |
2 files changed, 102 insertions, 0 deletions
diff --git a/challenge-208/perlboy1967/perl/ch1.pl b/challenge-208/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..9f3f213e53 --- /dev/null +++ b/challenge-208/perlboy1967/perl/ch1.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 208 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-208 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Minimum Index Sum +Submitted by: Mohammad S Anwar + +You are given two arrays of strings. + +Write a script to find out all common strings in the given two arrays with minimum +index sum. If no common strings found returns an empty list. + +=cut + +use v5.16; + +use common::sense; + +use List::Util qw(min); +use List::MoreUtils qw(frequency); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub mimimumIndexSum(\@\@) { + my %f = frequency(@{$_[0]},@{$_[1]}); + + my %i; + for (@_) { + my $i = 0; + for (@$_) { + $i{$_} += $i++ if ($f{$_} > 1); + } + } + + my $min = min(values %i); + + return [sort grep { $i{$_} == $min } keys %i]; +} + +cmp_deeply(mimimumIndexSum(@{[qw(Perl Raku Love)]},@{[qw(Raku Perl Hate)]}),[qw(Perl Raku)]); +cmp_deeply(mimimumIndexSum(@{[qw(A B C)]},@{[qw(D E F)]}),[]); +cmp_deeply(mimimumIndexSum(@{[qw(A B C)]},@{[qw(C A B)]}),['A']); +cmp_deeply(mimimumIndexSum(@{[qw(Z B C D)]},@{[qw(D C A B)]}),[qw(B C D)]); + +done_testing; diff --git a/challenge-208/perlboy1967/perl/ch2.pl b/challenge-208/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..4f93b72cd4 --- /dev/null +++ b/challenge-208/perlboy1967/perl/ch2.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 208 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-208 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Duplicate and Missing +Submitted by: Mohammad S Anwar + +You are given an array of integers in sequence with one missing and one duplicate. + +Write a script to find the duplicate and missing integer in the given array. Return -1 if none found. + +For the sake of this task, let us assume the array contains no more than one duplicate and missing. + +=cut + +use v5.16; + +use common::sense; + +use List::MoreUtils qw(slide); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub duplicateAndMissing(@) { + @_ = sort { $a <=> $b } @_; + + my (@duplicate,@missing); + slide { + if ($a == $b) { push(@duplicate,$a) if ($duplicate[-1] != $b) } + elsif ($b - $a > 1) { push(@missing,$a + 1 .. $b - 1) } + } @_; + + # 'Special' rule to comply to the task: + push(@missing,$_[-1] + 1) + if (scalar(@missing) == 0 && scalar(@duplicate) > 0); + + return [[@duplicate],[@missing]]; +} + +cmp_deeply(duplicateAndMissing(1,2,2,4),[[2],[3]]); +cmp_deeply(duplicateAndMissing(1,2,3,4),[[],[]]); +cmp_deeply(duplicateAndMissing(1,2,3,3),[[3],[4]]); +cmp_deeply(duplicateAndMissing(1,1,1,3,3,5,6,8,8,9),[[1,3,8],[2,4,7]]); + +done_testing; |
