diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-01 23:42:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-01 23:42:46 +0100 |
| commit | 39262eec7bdc17f4db90fd04fe165ed44220ad90 (patch) | |
| tree | 7ba2a2741d1439fb8a5729b6bf8922392c476032 | |
| parent | 34c586ff20bfba3a2eba42d95563c98d46e40f55 (diff) | |
| parent | f166830bc14684a1fd8e8b8bcf8f98fd68fe347f (diff) | |
| download | perlweeklychallenge-club-39262eec7bdc17f4db90fd04fe165ed44220ad90.tar.gz perlweeklychallenge-club-39262eec7bdc17f4db90fd04fe165ed44220ad90.tar.bz2 perlweeklychallenge-club-39262eec7bdc17f4db90fd04fe165ed44220ad90.zip | |
Merge pull request #10751 from pme/challenge-208
challenge-208
| -rwxr-xr-x | challenge-208/peter-meszaros/perl/ch-1.pl | 98 | ||||
| -rwxr-xr-x | challenge-208/peter-meszaros/perl/ch-2.pl | 71 |
2 files changed, 169 insertions, 0 deletions
diff --git a/challenge-208/peter-meszaros/perl/ch-1.pl b/challenge-208/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..d8c4f58f2d --- /dev/null +++ b/challenge-208/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,98 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @list1 = ("Perl", "Raku", "Love") + @list2 = ("Raku", "Perl", "Hate") + + Output: ("Perl", "Raku") + + There are two common strings "Perl" and "Raku". + Index sum of "Perl": 0 + 1 = 1 + Index sum of "Raku": 1 + 0 = 1 + +=head2 Example 2 + + Input: @list1 = ("A", "B", "C") + @list2 = ("D", "E", "F") + + Output: () + + No common string found, so no result. + +=head2 Example 3 + + Input: @list1 = ("A", "B", "C") + @list2 = ("C", "A", "B") + + Output: ("A") + + There are three common strings "A", "B" and "C". + Index sum of "A": 0 + 1 = 1 + Index sum of "B": 1 + 2 = 3 + Index sum of "C": 2 + 0 = 2 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::MoreUtils qw/duplicates/; +use List::Util qw/min/; + +my $cases = [ + [[["Perl", "Raku", "Love"], + ["Raku", "Perl", "Hate"]], + ["Perl", "Raku"], 'Example 1'], + [[["A", "B", "C"], + ["D", "E", "F"]], + undef, 'Example 2'], + [[["A", "B", "C"], + ["C", "A", "B"]], + ["A"], 'Example 3'], +]; + +sub _index_of_first +{ + my $l = shift; + my $v = shift; + + for my $i (0 .. $#$l) { + return $i if $v eq $l->[$i]; + } + return undef; +} + +sub minimum_index_sum +{ + my $l1 = $_[0]->[0]; + my $l2 = $_[0]->[1]; + + my %d = map {$_ => undef} duplicates(@$l1, @$l2); + return undef unless %d; + + my $min = 0; + for my $d (keys %d) { + $d{$d} = _index_of_first($l1, $d) + _index_of_first($l2, $d); + } + + my $min_idx = min values %d; + return [sort grep { $d{$_} == $min_idx } keys %d]; +} + +for (@$cases) { + is(minimum_index_sum($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-208/peter-meszaros/perl/ch-2.pl b/challenge-208/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..b1fafab273 --- /dev/null +++ b/challenge-208/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1: + + Input: @nums = (1,2,2,4) + Output: (2,3) + + Duplicate is 2 and Missing is 3. + +=head2 Example 2: + + Input: @nums = (1,2,3,4) + Output: -1 + + No duplicate and missing found. + +=head2 Example 3: + + Input: @nums = (1,2,3,3) + Output: (3,4) + + Duplicate is 3 and Missing is 4. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::MoreUtils qw/duplicates/; + +my $cases = [ + [[1, 2, 2, 4], [2, 3], 'Example 1'], + [[1, 2, 3, 4], [], 'Example 2'], + [[1, 2, 3, 3], [3, 4], 'Example 3'], +]; + +sub duplicate_and_missing +{ + my $l = shift; + + my @res = duplicates @$l; + for my $i (1 .. $#$l) { + my $v = $l->[$i-1] + 1; + if ($l->[$i] != $v) { + push @res, $v; + last; + } + } + return \@res; +} + +for (@$cases) { + is(duplicate_and_missing($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
