aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-20 00:26:42 +0000
committerGitHub <noreply@github.com>2023-03-20 00:26:42 +0000
commitfc7f88c537298b8ca4a2610aaeb35120cc80e7d2 (patch)
tree0d17d60b4cd9da357d66cb3aaedcc83afe3925e8
parentcb32faeac355702c74e053b5bfd91cd93f1f2201 (diff)
parent519dd5aaf4bacd263e3260dd000ffa825c0ea691 (diff)
downloadperlweeklychallenge-club-fc7f88c537298b8ca4a2610aaeb35120cc80e7d2.tar.gz
perlweeklychallenge-club-fc7f88c537298b8ca4a2610aaeb35120cc80e7d2.tar.bz2
perlweeklychallenge-club-fc7f88c537298b8ca4a2610aaeb35120cc80e7d2.zip
Merge pull request #7763 from jaldhar/challenge-208
Challenge 208 by Jaldhar H. Vyas.
-rw-r--r--challenge-208/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-208/jaldhar-h-vyas/perl/ch-1.pl55
-rwxr-xr-xchallenge-208/jaldhar-h-vyas/perl/ch-2.pl27
-rwxr-xr-xchallenge-208/jaldhar-h-vyas/raku/ch-1.raku56
-rwxr-xr-xchallenge-208/jaldhar-h-vyas/raku/ch-2.raku28
5 files changed, 167 insertions, 0 deletions
diff --git a/challenge-208/jaldhar-h-vyas/blog.txt b/challenge-208/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..5c01c6cebc
--- /dev/null
+++ b/challenge-208/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2023/03/perl_weekly_challenge_week_208.html \ No newline at end of file
diff --git a/challenge-208/jaldhar-h-vyas/perl/ch-1.pl b/challenge-208/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..7249b5694b
--- /dev/null
+++ b/challenge-208/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+my @strings = @ARGV;
+
+my @list1;
+my @list2;
+my $destination = 'first';
+
+for my $string (@strings) {
+ if ($string eq '-') {
+ $destination = 'second';
+ next;
+ }
+ if ($destination eq 'first') {
+ push @list1, $string;
+ } elsif ($destination eq 'second') {
+ push @list2, $string;
+ }
+}
+
+my %common;
+
+for my $i (0 .. scalar @list1 - 1) {
+ $common{$list1[$i]}->[0] = $i;
+ unless (exists $common{$list1[$i]}->[1]) {
+ $common{$list1[$i]}->[1] = -1;
+ }
+}
+
+for my $i (0 .. scalar @list2 - 1) {
+ unless (exists $common{$list2[$i]}->[0]) {
+ $common{$list2[$i]}->[0] = -1;
+ }
+ $common{$list2[$i]}->[1] = $i;
+}
+
+my %indexsum;
+
+for my $string (keys %common) {
+ if ($common{$string}->[0] != -1 && $common{$string}->[1] != -1) {
+ $indexsum{$string} = $common{$string}->[0] + $common{$string}->[1];
+ }
+}
+
+my $minimumindex = (sort { $a <=> $b } values %indexsum)[0];
+
+say q{(} . (
+ join q{, },
+ sort
+ map { q{"} . $_ . q{"}}
+ grep { $indexsum{$_} == $minimumindex }
+ keys %indexsum
+) . q{)};
diff --git a/challenge-208/jaldhar-h-vyas/perl/ch-2.pl b/challenge-208/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..ba3c32a940
--- /dev/null
+++ b/challenge-208/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+my @nums = @ARGV;
+my $missing;
+my %count;
+
+for my $i (0 .. scalar @nums - 1) {
+ $count{$nums[$i]}++;
+
+ if ($i > 0 && $nums[$i] - $nums[$i - 1] > 1) {
+ $missing = $nums[$i] - 1;
+ }
+}
+
+my $duplicate //= (grep { $count{$_} > 1; } keys %count)[0];
+
+if ($duplicate) {
+ if ($duplicate == $nums[-1]) {
+ $missing = $nums[-1] + 1;
+ }
+
+ say q{(} . (join q{,}, ($duplicate, $missing)) . q{)};
+} else {
+ say "-1";
+}
diff --git a/challenge-208/jaldhar-h-vyas/raku/ch-1.raku b/challenge-208/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..e6ece03c5d
--- /dev/null
+++ b/challenge-208/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,56 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ *@strings #= two lists of strings separated by '-'
+) {
+ my @list1;
+ my @list2;
+ my $destination = 'first';
+
+ for @strings -> $string {
+ if $string eq '-' {
+ $destination = 'second';
+ next;
+ }
+ if $destination eq 'first' {
+ @list1.push($string);
+ } elsif $destination eq 'second' {
+ @list2.push($string);
+ }
+ }
+
+ my %common;
+
+ for 0 .. @list1.end -> $i {
+ %common{@list1[$i]}[0] = $i;
+ unless %common{@list1[$i]}[1]:exists {
+ %common{@list1[$i]}[1] = -1;
+ }
+ }
+
+ for 0 .. @list2.end -> $i {
+ unless %common{@list2[$i]}[0]:exists {
+ %common{@list2[$i]}[0] = -1;
+ }
+ %common{@list2[$i]}[1] = $i;
+ }
+
+ my %indexsum;
+
+ for %common.keys -> $string {
+ if %common{$string}[0] != -1 && %common{$string}[1] != -1 {
+ %indexsum{$string} = %common{$string}.sum;
+ }
+ }
+
+ my $minimumindex = %indexsum.values.sort({ $^a <=> $^b }).first;
+
+ say q{(} ~
+ %indexsum
+ .keys
+ .grep({ %indexsum{$_} == $minimumindex })
+ .map({ q{"} ~ $_ ~ q{"}})
+ .sort
+ .join(q{, })
+ ~ q{)};
+} \ No newline at end of file
diff --git a/challenge-208/jaldhar-h-vyas/raku/ch-2.raku b/challenge-208/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..464a374c3c
--- /dev/null
+++ b/challenge-208/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,28 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ *@nums
+) {
+ my $missing;
+ my %count;
+
+ for 0 .. @nums.end -> $i {
+ %count{@nums[$i]}++;
+
+ if $i > 0 && @nums[$i] - @nums[$i - 1] > 1 {
+ $missing = @nums[$i] - 1;
+ }
+ }
+
+ my $duplicate = %count.keys.grep({ %count{$_} > 1; }).first;
+
+ if $duplicate {
+ if $duplicate == @nums[*-1] {
+ $missing = @nums[*-1] + 1;
+ }
+
+ say q{(} ~ ($duplicate, $missing).join(q{,}) ~ q{)};
+ } else {
+ say -1;
+ }
+} \ No newline at end of file