From 519dd5aaf4bacd263e3260dd000ffa825c0ea691 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sun, 19 Mar 2023 19:56:22 -0400 Subject: Challenge 208 by Jaldhar H. Vyas. --- challenge-208/jaldhar-h-vyas/blog.txt | 1 + challenge-208/jaldhar-h-vyas/perl/ch-1.pl | 55 ++++++++++++++++++++++++++++ challenge-208/jaldhar-h-vyas/perl/ch-2.pl | 27 ++++++++++++++ challenge-208/jaldhar-h-vyas/raku/ch-1.raku | 56 +++++++++++++++++++++++++++++ challenge-208/jaldhar-h-vyas/raku/ch-2.raku | 28 +++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 challenge-208/jaldhar-h-vyas/blog.txt create mode 100755 challenge-208/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-208/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-208/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-208/jaldhar-h-vyas/raku/ch-2.raku 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 -- cgit