aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-25 17:05:47 +0000
committerGitHub <noreply@github.com>2023-03-25 17:05:47 +0000
commit27a7802fe892efcc81a126cd30ea7e1635e43f6f (patch)
treee850b9be2abdeffdf9e932c8fb1b798c69e1e729
parentc2eacf5f4a4b9c8403e3818f0a1aa2c5498923ef (diff)
parent6034b48ee0a7291f3fe349bc7b3bef78b3551d1e (diff)
downloadperlweeklychallenge-club-27a7802fe892efcc81a126cd30ea7e1635e43f6f.tar.gz
perlweeklychallenge-club-27a7802fe892efcc81a126cd30ea7e1635e43f6f.tar.bz2
perlweeklychallenge-club-27a7802fe892efcc81a126cd30ea7e1635e43f6f.zip
Merge pull request #7774 from vamsi-meenavilli/vamsi-challenge-208
Vamsi challenge 208
-rw-r--r--challenge-208/vamsi-meenavilli/perl/ch-1.pl93
-rw-r--r--challenge-208/vamsi-meenavilli/perl/ch-2.pl66
2 files changed, 159 insertions, 0 deletions
diff --git a/challenge-208/vamsi-meenavilli/perl/ch-1.pl b/challenge-208/vamsi-meenavilli/perl/ch-1.pl
new file mode 100644
index 0000000000..cc53b2c2d8
--- /dev/null
+++ b/challenge-208/vamsi-meenavilli/perl/ch-1.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/perl
+use strict;
+use warnings FATAL => 'all';
+
+use List::Util qw( max );
+use Test2::V0;
+
+=head1 AUTHORS
+
+Vamsi Meenavilli
+
+=head1 DESCRIPTION
+
+ Week 208:
+
+ 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.
+
+ 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
+ Example 2
+ Input: @list1 = ("A", "B", "C")
+ @list2 = ("D", "E", "F")
+
+ Output: ()
+
+ No common string found, so no result.
+ 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
+
+is(MinimumIndexSum(["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"]), ["Raku", "Perl"], 'Test Case 1.');
+is(MinimumIndexSum(["A", "B", "C"], ["D", "E", "F"]), [], 'Test Case 2.');
+is(MinimumIndexSum(["A", "B", "C"], ["C", "A", "B"]), ["A"], 'Test Case 3.');
+
+sub MinimumIndexSum {
+ my ($list1, $list2) = @_;
+
+ my $list1_size = scalar(@{$list1});
+ my $list2_size = scalar(@{$list2});
+
+ if ($list2 > $list1) {
+ ($list1, $list2) = ($list2, $list1);
+ ($list1_size, $list2_size) = ($list2_size, $list1_size);
+ }
+
+ my %list1_hash = ();
+ my $index = 0;
+ my $minimum_index_sum = List::Util::max($list1_size, $list2_size);
+ my @common_strings = ();
+
+ foreach (@{$list1}) {
+ $list1_hash{$_} = $index;
+ $index += 1;
+ }
+
+ for (my $i = 0; $i < $list2_size; $i++) {
+ my $list2_string = $list2->[$i];
+ my $list1_index = $list1_hash{$list2_string};
+
+ if (defined $list1_index) {
+ my $index_sum = $i + $list1_index;
+
+ if ($index_sum <= $minimum_index_sum) {
+ $minimum_index_sum = $index_sum;
+ push @common_strings, $list2_string;
+ }
+ }
+ }
+
+ return \@common_strings;
+}
+
+done_testing();
diff --git a/challenge-208/vamsi-meenavilli/perl/ch-2.pl b/challenge-208/vamsi-meenavilli/perl/ch-2.pl
new file mode 100644
index 0000000000..7db1345dc8
--- /dev/null
+++ b/challenge-208/vamsi-meenavilli/perl/ch-2.pl
@@ -0,0 +1,66 @@
+#!/usr/bin/perl
+use strict;
+use warnings FATAL => 'all';
+
+use Test2::V0;
+
+=head1 AUTHORS
+
+Vamsi Meenavilli
+
+=head1 DESCRIPTION
+
+ Week 208:
+
+
+ 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.
+
+ Example 1:
+
+ Input: @nums = (1,2,2,4)
+ Output: (2,3)
+
+ Duplicate is 2 and Missing is 3.
+ Example 2:
+
+ Input: @nums = (1,2,3,4)
+ Output: -1
+
+ No duplicate and missing found.
+ Example 3:
+
+ Input: @nums = (1,2,3,3)
+ Output: (3,4)
+
+ Duplicate is 3 and Missing is 4.
+
+=cut
+
+
+is(GetDuplicateAndMissing(1, 2, 2, 4), [2, 3], 'Test Case 1.');
+is(GetDuplicateAndMissing(1, 2, 3, 4), [-1], 'Test Case 2.');
+is(GetDuplicateAndMissing(1, 2, 3, 3), [3, 4], 'Test Case 3.');
+
+sub GetDuplicateAndMissing {
+ my @sequence = @_;
+
+ my @missing_and_duplicate = (-1);
+ my $sequence_size = scalar(@sequence);
+
+ foreach my $index (1..$sequence_size - 1) {
+ if ($sequence[$index - 1] == $sequence[$index]) {
+ @missing_and_duplicate = ($sequence[$index], $sequence[$index] + 1);
+ last;
+ }
+ }
+
+ return \@missing_and_duplicate;
+}
+
+done_testing();