diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-18 21:22:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-18 21:22:57 +0000 |
| commit | ed97fcb121a265692e35e86b081fa0bab04ec7ba (patch) | |
| tree | 9eb23914328420161766cedd49e79df90d8d7af5 | |
| parent | b25a057c64870d936ec8d1891797b4cbc668fe5d (diff) | |
| parent | b58a2bd4fcf64591ab9e8db0d1901228e561c441 (diff) | |
| download | perlweeklychallenge-club-ed97fcb121a265692e35e86b081fa0bab04ec7ba.tar.gz perlweeklychallenge-club-ed97fcb121a265692e35e86b081fa0bab04ec7ba.tar.bz2 perlweeklychallenge-club-ed97fcb121a265692e35e86b081fa0bab04ec7ba.zip | |
Merge pull request #7732 from zapwai/branch-for-208
Week 208
| -rw-r--r-- | challenge-208/zapwai/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-208/zapwai/perl/ch-2.pl | 21 |
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-208/zapwai/perl/ch-1.pl b/challenge-208/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..1285580b25 --- /dev/null +++ b/challenge-208/zapwai/perl/ch-1.pl @@ -0,0 +1,29 @@ +use v5.30.0; +my @list1 = ("Perl", "Raku", "Love"); +my @list2 = ("Raku", "Perl", "Hate"); + +#my @list1 = ("A", "B", "C"); +#my @list2 = ("C", "A", "B"); + +#my @list1 = ("A", "B", "C"); +#my @list2 = ("D", "e", "f"); + +my (@match, @ind1, @ind2); +for my $i (0 .. $#list1) { + for my $j (0 .. $#list2) { + if ($list1[$i] eq $list2[$j]) { + push @match, $list1[$i]; + push @ind1, $i; + push @ind2, $j; + } + } +} + +say "Input: \@list1 = (".join(", ",@list1).")"; +say "\t\@list2 = (".join(", ",@list2).")"; +say "Output: (".join(", ",@match).")"; + +say "There are ".($#match + 1)." common strings: @match"; +for my $i (0 .. $#match) { + say "Index sum of " . $match[$i] . ": $ind1[$i] + $ind2[$i] = ". ($ind1[$i] + $ind2[$i]); +} diff --git a/challenge-208/zapwai/perl/ch-2.pl b/challenge-208/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..e418495b78 --- /dev/null +++ b/challenge-208/zapwai/perl/ch-2.pl @@ -0,0 +1,21 @@ +use v5.30.0; +my @nums = (1, 2, 2, 4); +#my @nums = (1,2,3,3); +#my @nums = (1,2,3,4); +say "Input: \@nums = (".join(",",@nums).")"; +print "Output: "; +my $catch; # flag for printing -1 +for (1 .. $#nums) { + my $i = $nums[$_ - 1]; + my $j = $nums[$_]; + if ($j - $i == 2) { + say "(".($i+1).",".($i+2).")"; + $catch = 1; + last; + } elsif ($j - $i == 0) { + say "($i,".($i+1).")"; + $catch = 1; + last; + } +} +say "-1" unless ($catch); |
