diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-19 12:45:32 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-19 12:45:32 +0000 |
| commit | ec91befc098768c4c615f9e3779f53fc9138e5b7 (patch) | |
| tree | 7b5d111cfa4caf63a3031df2b0b06071d2fe9cb1 | |
| parent | 3c7ad5fae8aa1d0913a3da539273a4b075c21d6c (diff) | |
| parent | 773b7c74d7d6bebb18d680cb9461423967885403 (diff) | |
| download | perlweeklychallenge-club-ec91befc098768c4c615f9e3779f53fc9138e5b7.tar.gz perlweeklychallenge-club-ec91befc098768c4c615f9e3779f53fc9138e5b7.tar.bz2 perlweeklychallenge-club-ec91befc098768c4c615f9e3779f53fc9138e5b7.zip | |
Merge pull request #7752 from pip/branch-for-challenge-208
Pip Stuart's submission for challenge-208.
| -rw-r--r-- | challenge-208/pip/perl/ch-1.pl | 54 | ||||
| -rw-r--r-- | challenge-208/pip/perl/ch-2.pl | 43 | ||||
| -rw-r--r-- | challenge-208/pip/raku/ch-1.raku | 54 | ||||
| -rw-r--r-- | challenge-208/pip/raku/ch-2.raku | 43 |
4 files changed, 194 insertions, 0 deletions
diff --git a/challenge-208/pip/perl/ch-1.pl b/challenge-208/pip/perl/ch-1.pl new file mode 100644 index 0000000000..16d9f49e97 --- /dev/null +++ b/challenge-208/pip/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #208 - Pip Stuart +# Task1: 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. +# Example1: +# In-put: @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 +# Example2: +# In-put: @list1 = ("A", "B", "C") +# @list2 = ("D", "E", "F") +# Output: () +# No common string found, so no result. +# Example3: +# In-put: @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 +use strict;use warnings;use utf8;use v5.12;my $d8VS='N3JM5MJp'; +sub MInS {my @lst2 = @_;my @lst1 = ();my @mins = ();my @nsmz = ();my %ndxs = (); + while ($lst2[0] ne ';') { + push(@lst1, shift(@lst2)); + }; if ($lst2[0] eq ';') { shift(@lst2) ; } # remove the semicolon separator + for my $ndx1 (0..$#lst1) { + for my $ndx2 (0..$#lst2) { + if ($lst1[$ndx1] eq $lst2[$ndx2]) { + push(@{$ndxs{ $ndx1 + $ndx2 }}, $lst1[$ndx1]); + } + } + } + @nsmz = sort { $a <=> $b } keys(%ndxs); + @mins = @{$ndxs{$nsmz[0]}} if (@nsmz); + printf( "(\"%s\") ,\n", join('","', @lst1)); + printf( "(\"%s\") => ", join('","', @lst2)); + if (@mins) { printf("(\"%s\");\n" , join('","', @mins)); } + else { say "();"; } + return(@mins); +} +if (@ARGV) { + MInS(@ARGV); +} else { + MInS("Perl","Raku","Love",";", # I've decided to use a single semicolon to separate the 2 input lists of strings. + "Raku","Perl","Hate" ); # => ("Perl","Raku"); + MInS("A","B","C",";", + "D","E","F" ); # => (); + MInS("A","B","C",";", + "C","A","B" ); # => ("A"); +} diff --git a/challenge-208/pip/perl/ch-2.pl b/challenge-208/pip/perl/ch-2.pl new file mode 100644 index 0000000000..8b3fc4a301 --- /dev/null +++ b/challenge-208/pip/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #208 - Pip Stuart +# Task2: 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. +# Example1: +# In-put: @nums = (1,2,2,4) +# Output: (2,3) +# Duplicate is 2 and Missing is 3. +# Example2: +# In-put: @nums = (1,2,3,4) +# Output: -1 +# No duplicate and missing found. +# Example3: +# In-put: @nums = (1,2,3,3) +# Output: (3,4) +# Duplicate is 3 and Missing is 4. +# Last date to submit the solution 23:59 (UK Time) Sunday 19th March 2023. +use strict;use warnings;use utf8;use v5.12;my $d8VS='N3JM5uBR'; +sub DpaM {my @nums = sort { $a <=> $b } @_;my @dpam = ();my %hasn = (); + for (0..$#nums-1) { + if ($nums[$_] == $nums[$_+1]) { + push(@dpam, $nums[$_ ] ); last; + } + } + for (0..$#nums ) { $hasn{$nums[$_]} = 1; } + for (0..$#nums ) { + if ( @dpam && !exists($hasn{$_+1})) { + push(@dpam, $_+1); last; + } + } + printf( "(%-7s) => ", join(',', @nums)); + if (@dpam) { printf("(%s);\n", join(',', @dpam)); } + else { say "-1;"; } + return(@dpam); +} +if (@ARGV) { + DpaM(@ARGV); +} else { + DpaM(1,2,2,4); # => (2,3); + DpaM(1,2,3,4); # => -1 ; + DpaM(1,2,3,3); # => (3,4); +} diff --git a/challenge-208/pip/raku/ch-1.raku b/challenge-208/pip/raku/ch-1.raku new file mode 100644 index 0000000000..f122395c06 --- /dev/null +++ b/challenge-208/pip/raku/ch-1.raku @@ -0,0 +1,54 @@ +#!/usr/bin/env raku +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #208 - Pip Stuart +# Task1: 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. +# Example1: +# In-put: @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 +# Example2: +# In-put: @list1 = ("A", "B", "C") +# @list2 = ("D", "E", "F") +# Output: () +# No common string found, so no result. +# Example3: +# In-put: @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 +use v6;my $d8VS='N3JM6rwn'; +sub MInS {my @lst2 = @_;my @lst1 = ();my @mins = ();my @nsmz = ();my %ndxs = (); + while (@lst2[0] ne ';') { + push(@lst1, shift(@lst2)); + }; if (@lst2[0] eq ';') { shift(@lst2) ; } # remove the semicolon separator + for (0..(@lst1.elems-1)) -> $ndx1 { + for (0..(@lst2.elems-1)) -> $ndx2 { + if (@lst1[$ndx1] eq @lst2[$ndx2]) { + %ndxs{ $ndx1 + $ndx2 }.push(@lst1[$ndx1]); + } + } + } + @nsmz = sort +*, keys(%ndxs); + @mins = %ndxs{@nsmz[0]}[] if (@nsmz); + printf( "(\"%s\") ,\n", join('","', @lst1)); + printf( "(\"%s\") => ", join('","', @lst2)); + if (@mins) { printf("(\"%s\");\n" , join('","', @mins)); } + else { say "();"; } + return(@mins); +} +if (@*ARGS) { + MInS(@*ARGS); +} else { + MInS("Perl","Raku","Love",";", # I've decided to use a single semicolon to separate the 2 input lists of strings. + "Raku","Perl","Hate" ); # => ("Perl","Raku"); + MInS("A","B","C",";", + "D","E","F" ); # => (); + MInS("A","B","C",";", + "C","A","B" ); # => ("A"); +} diff --git a/challenge-208/pip/raku/ch-2.raku b/challenge-208/pip/raku/ch-2.raku new file mode 100644 index 0000000000..8aa18bb729 --- /dev/null +++ b/challenge-208/pip/raku/ch-2.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env raku +# HTTPS://TheWeeklyChallenge.Org - Perl/Raku Weekly Challenge #208 - Pip Stuart +# Task2: 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. +# Example1: +# In-put: @nums = (1,2,2,4) +# Output: (2,3) +# Duplicate is 2 and Missing is 3. +# Example2: +# In-put: @nums = (1,2,3,4) +# Output: -1 +# No duplicate and missing found. +# Example3: +# In-put: @nums = (1,2,3,3) +# Output: (3,4) +# Duplicate is 3 and Missing is 4. +# Last date to submit the solution 23:59 (UK Time) Sunday 19th March 2023. +use v6;my $d8VS='N3JM6vA8'; +sub DpaM {my @nums = sort +*, @_;my @dpam = ();my %hasn = (); + for (0..(@nums.elems-2)) { + if (@nums[$_] == @nums[$_+1]) { + push(@dpam, @nums[$_ ]); last; + } + } + for (0..(@nums.elems-1)) {%hasn{@nums[$_]} = 1; } + for (0..(@nums.elems-1)) { + if ( @dpam && !(%hasn{$_+1}:exists)) { + push(@dpam, $_+1); last; + } + } + printf( "(%-7s) => ", join(',', @nums)); + if (@dpam) { printf("(%s);\n", join(',', @dpam)); } + else { say "-1;"; } + return(@dpam); +} +if (@*ARGS) { + DpaM(@*ARGS); +} else { + DpaM(1,2,2,4); # => (2,3); + DpaM(1,2,3,4); # => -1 ; + DpaM(1,2,3,3); # => (3,4); +} |
