From c2f7e147f7d467ebf4e91f3d4dc3bae47bf8f465 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sun, 2 Jan 2022 23:54:04 -0500 Subject: Challenge 145 by Jaldhar H. Vyas. --- challenge-145/jaldhar-h-vyas/blog.txt | 1 + challenge-145/jaldhar-h-vyas/perl/ch-1.pl | 30 ++++++++++++++++++++++++++ challenge-145/jaldhar-h-vyas/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++ challenge-145/jaldhar-h-vyas/raku/ch-1.raku | 6 ++++++ challenge-145/jaldhar-h-vyas/raku/ch-2.raku | 30 ++++++++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 challenge-145/jaldhar-h-vyas/blog.txt create mode 100755 challenge-145/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-145/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-145/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-145/jaldhar-h-vyas/raku/ch-2.raku 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 -- cgit