diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-04 17:48:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-04 17:48:02 +0000 |
| commit | cd402f7a6a7e0dba70771f92ff9e96e20bf750ba (patch) | |
| tree | cef79ae6a1ea4daf491a3d976506da0c932de422 | |
| parent | aaab417272f7ae13ade34c68a033d2b1214886d3 (diff) | |
| parent | f5508397240ade08a77224d91ca07a4e7676990b (diff) | |
| download | perlweeklychallenge-club-cd402f7a6a7e0dba70771f92ff9e96e20bf750ba.tar.gz perlweeklychallenge-club-cd402f7a6a7e0dba70771f92ff9e96e20bf750ba.tar.bz2 perlweeklychallenge-club-cd402f7a6a7e0dba70771f92ff9e96e20bf750ba.zip | |
Merge pull request #11528 from zapwai/branch-for-307
Week 307
| -rw-r--r-- | challenge-307/zapwai/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-307/zapwai/perl/ch-2.pl | 35 |
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-307/zapwai/perl/ch-1.pl b/challenge-307/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..2ff1dc4bf4 --- /dev/null +++ b/challenge-307/zapwai/perl/ch-1.pl @@ -0,0 +1,17 @@ +use v5.38; +sub proc(@ints) { + say "Input: \@ints = @ints"; + my @int = sort {$a <=> $b} @ints; + my @ind; + + for my $i (0 .. $#ints) { + push @ind, $i if ($ints[$i] != $int[$i]); + } + say "Output: @ind"; +} +my @ints = (5,2,4,3,1); +proc(@ints); +@ints = (1,2,1,1,3); +proc(@ints); +@ints = (3,1,3,2,3); +proc(@ints); diff --git a/challenge-307/zapwai/perl/ch-2.pl b/challenge-307/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..ac8b4f396f --- /dev/null +++ b/challenge-307/zapwai/perl/ch-2.pl @@ -0,0 +1,35 @@ +use v5.38; +# return true if given words are anagrams +sub is_anagram($w1, $w2) { + my @l1 = sort split "", $w1; + my @l2 = sort split "", $w2; + return 0 if (@l1 != @l2); + for my $i (0 .. $#l1) { + return 0 if ($l1[$i] ne $l2[$i]); + } + return 1; +} +sub proc(@words) { + say "Input: \@words = @words"; + my $n; + do { + $n = 0; + my @ind; + for my $i (0 .. $#words - 1) { + if (is_anagram($words[$i], $words[$i+1])) { + $n++; + push @ind, $i; + + } + } + for (reverse @ind) { + splice @words, $_, 1; + } + } while ($n); + say "Output: ". scalar @words . " (@words)"; + +} +my @words = ("acca", "dog", "god", "perl", "repl"); +proc(@words); +@words = ("abba", "baba", "aabb", "ab", "ab"); +proc(@words); |
