diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-03 09:33:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-03 09:33:21 +0000 |
| commit | 9c6171ae7ecfd0fe4840439c04e07f003257c27a (patch) | |
| tree | 88ca8dc1f95b982811ff2254ac0a2d7e4f9ef48f | |
| parent | 14d1e11913396216dade604c1dee3945dd4eb4e8 (diff) | |
| parent | c2f7e147f7d467ebf4e91f3d4dc3bae47bf8f465 (diff) | |
| download | perlweeklychallenge-club-9c6171ae7ecfd0fe4840439c04e07f003257c27a.tar.gz perlweeklychallenge-club-9c6171ae7ecfd0fe4840439c04e07f003257c27a.tar.bz2 perlweeklychallenge-club-9c6171ae7ecfd0fe4840439c04e07f003257c27a.zip | |
Merge pull request #5457 from jaldhar/challenge-145
Challenge 145 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-145/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-145/jaldhar-h-vyas/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-145/jaldhar-h-vyas/perl/ch-2.pl | 33 | ||||
| -rwxr-xr-x | challenge-145/jaldhar-h-vyas/raku/ch-1.raku | 6 | ||||
| -rwxr-xr-x | challenge-145/jaldhar-h-vyas/raku/ch-2.raku | 30 |
5 files changed, 100 insertions, 0 deletions
diff --git a/challenge-145/jaldhar-h-vyas/blog.txt b/challenge-145/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..d551a3d14a --- /dev/null +++ b/challenge-145/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2022/01/perl_weekly_challenge_week_145.html diff --git a/challenge-145/jaldhar-h-vyas/perl/ch-1.pl b/challenge-145/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..2427a80bd5 --- /dev/null +++ b/challenge-145/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +sub sum { + my ($arr) = @_; + my $total = 0; + + for my $elem (@{$arr}) { + $total += $elem; + } + + return $total; +} + +sub Zmultiply { + my @a = @{ $_[0] }; + my @b = @{ $_[1] }; + + my @result; + for my $i (0 .. scalar @b - 1) { + push @result, $a[$i] * $b[$i]; + } + return @result; +} + +my @a = (1, 2, 3); +my @b = (4, 5, 6); + +say sum([Zmultiply(\@a, \@b)]); diff --git a/challenge-145/jaldhar-h-vyas/perl/ch-2.pl b/challenge-145/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..b6b0413c07 --- /dev/null +++ b/challenge-145/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +sub isPalindrome { + my ($word) = @_; + + return $word eq join q{}, reverse split //, $word; +} + +my $s = shift // die "Need a word.\n"; +my @palindromes; + +for my $i (0 .. length($s) - 1) { + + my $char = substr $s, $i, 1; + if (!scalar grep { $_ eq $char; } @palindromes) { + push @palindromes, $char; + } + + my $distance = length($s) - $i; + while ($distance > 1) { + my $part = substr $s, $i, $distance; + if (isPalindrome($part) && !scalar grep { $_ eq $part; } @palindromes) { + push @palindromes, $part; + last; + } else { + $distance--; + } + } +} + +say join q{ }, @palindromes; diff --git a/challenge-145/jaldhar-h-vyas/raku/ch-1.raku b/challenge-145/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..a3ba3496d5 --- /dev/null +++ b/challenge-145/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/usr/bin/raku + +my @a = (1, 2, 3); +my @b = (4, 5, 6); + +say [+] (@a Z @b).map({ [*] $_; });
\ No newline at end of file diff --git a/challenge-145/jaldhar-h-vyas/raku/ch-2.raku b/challenge-145/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..2c7d65648f --- /dev/null +++ b/challenge-145/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,30 @@ +#!/usr/bin/raku + +sub isPalindrome(Str $word) { + return $word eq $word.flip; +} + +sub MAIN( + Str $s +) { + my @palindromes; + + for 0 ..^ $s.chars -> $i { + if $s.substr($i, 1) ne @palindromes.any { + @palindromes.push($s.substr($i, 1)); + } + + my $distance = $s.chars - $i; + while $distance > 1 { + my $part = $s.substr($i, $distance); + if isPalindrome($part) && $part ne @palindromes.any { + @palindromes.push($part); + last; + } else { + $distance--; + } + } + } + + say @palindromes.join(q{ }); +}
\ No newline at end of file |
