aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-20 00:12:04 +0000
committerGitHub <noreply@github.com>2023-03-20 00:12:04 +0000
commite64caacc6522f3a28ddaeefe9bd6b14a353d4611 (patch)
tree0971f4a7c8d0cbfa18fdbce314b03731fae17bcb
parentb855c98e4ac73a9af20be851bf1d3d0cf870717f (diff)
parent0fd9d9f3e94cf69f0ef2868957996cb66b494350 (diff)
downloadperlweeklychallenge-club-e64caacc6522f3a28ddaeefe9bd6b14a353d4611.tar.gz
perlweeklychallenge-club-e64caacc6522f3a28ddaeefe9bd6b14a353d4611.tar.bz2
perlweeklychallenge-club-e64caacc6522f3a28ddaeefe9bd6b14a353d4611.zip
Merge pull request #7759 from Solathian/branch-for-challenge-208
Added files for challenge 208
-rw-r--r--challenge-208/solathian/perl/ch-1.pl80
-rw-r--r--challenge-208/solathian/perl/ch-2.pl67
2 files changed, 147 insertions, 0 deletions
diff --git a/challenge-208/solathian/perl/ch-1.pl b/challenge-208/solathian/perl/ch-1.pl
new file mode 100644
index 0000000000..8354f5cd2d
--- /dev/null
+++ b/challenge-208/solathian/perl/ch-1.pl
@@ -0,0 +1,80 @@
+#!usr/bin/perl
+use v5.36;
+use builtin 'indexed';
+no warnings 'experimental';
+
+# Challenge 208 - 2 - Minimum Index Sum
+# 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.
+
+
+
+my @list0 = ("Perl", "Raku", "Love");
+my @list1 = ("Raku", "Perl", "Hate");
+minIndSum(\@list0, \@list1);
+
+
+my @list2 = ("A", "B", "C");
+my @list3 = ("D", "E", "F");
+minIndSum(\@list2, \@list3);
+
+
+my @list4 = ("A", "B", "C");
+my @list5 = ("C", "A", "B");
+minIndSum(\@list4, \@list5);
+
+sub getIndex($target, $arrayRef)
+{
+ my $retVal = -1;
+
+ foreach my ($i, $value) (indexed @$arrayRef)
+ {
+ if($value eq $target)
+ {
+ $retVal = $i;
+ last;
+ }
+ }
+
+ die "getIndex would return with -1" if($retVal == -1);
+
+ return $retVal;
+
+}
+
+sub minIndSum($list0, $list1)
+{
+
+ my @common;
+
+ OUTER:
+ foreach my $first (@$list0)
+ {
+ foreach my $second (@$list1)
+ {
+ if($first eq $second)
+ {
+ push(@common, $first);
+ next OUTER;
+ }
+ }
+ }
+
+ if(@common == 0)
+ {
+ return -1;
+ say("No common string found, so no result.");
+ }
+ foreach my $commonWord (@common)
+ {
+ my $i0 = getIndex($commonWord, $list0);
+ my $i1 = getIndex($commonWord, $list1);
+ my $sum = $i0 + $i1;
+
+ say("Index sum of \"$commonWord\":\t $i0 + $i1 = $sum" );
+ }
+
+
+
+} \ No newline at end of file
diff --git a/challenge-208/solathian/perl/ch-2.pl b/challenge-208/solathian/perl/ch-2.pl
new file mode 100644
index 0000000000..5b4f158c8d
--- /dev/null
+++ b/challenge-208/solathian/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!usr/bin/perl
+use v5.36;
+use builtin 'indexed';
+no warnings 'experimental';
+
+
+# Challenge 208 - 2 - Duplicate and Missing
+# 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.
+
+
+duplicateAndMissing(1, 2, 2, 4); # Duplicate is 2 and Missing is 3.
+duplicateAndMissing(1, 2, 3, 4); # No duplicate and missing found.
+duplicateAndMissing(1, 2, 3, 3); # Duplicate is 3 and Missing is 4.
+
+sub findDuplicate($arrayRef)
+{
+ my $retVal = -1;
+
+ foreach my ($i, $iVal) (indexed @$arrayRef)
+ {
+ foreach my ($j, $jVal) (indexed @$arrayRef)
+ {
+ if( ($i != $j) && ($iVal == $jVal))
+ {
+ $retVal = $iVal;
+ last;
+ }
+ }
+ }
+ return $retVal;
+}
+
+sub findMissing($arrayRef)
+{
+ my $missing = ($arrayRef->[-1]) + 1; # set is as the last element + 1
+ my $last = ($arrayRef->[0]) - 1; # set it for the first element as expected
+
+ foreach my $current ( @$arrayRef )
+ {
+ if($current == ($last + 1)) { $last++ }
+ else { $missing = ($last + 1) }
+ }
+
+ return $missing;
+}
+
+
+sub duplicateAndMissing( @list )
+{
+ my $duplicate = findDuplicate(\@list);
+ my $missing;
+
+ if( $duplicate == -1)
+ {
+ say("No duplicate and missing found.");
+ return -1;
+ }
+ else
+ {
+ $missing = findMissing(\@list);
+
+ say("Duplicate: $duplicate, Missing: $missing");
+ }
+
+} \ No newline at end of file