aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-208/peter-meszaros/perl/ch-1.pl98
-rwxr-xr-xchallenge-208/peter-meszaros/perl/ch-2.pl71
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;