diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-03 01:10:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-03 01:10:33 +0000 |
| commit | cd2e1c26ffd0b4768c170d3078790f6eb2781eb5 (patch) | |
| tree | ca958b4d03e97a3c2fe67d44d72a1c039d39b733 | |
| parent | 60c28ce1b5b6f162272828e50238414023a4ee66 (diff) | |
| parent | e9f65f1fec37b0925cd535444c7e48e4111093d1 (diff) | |
| download | perlweeklychallenge-club-cd2e1c26ffd0b4768c170d3078790f6eb2781eb5.tar.gz perlweeklychallenge-club-cd2e1c26ffd0b4768c170d3078790f6eb2781eb5.tar.bz2 perlweeklychallenge-club-cd2e1c26ffd0b4768c170d3078790f6eb2781eb5.zip | |
Merge pull request #5447 from andrezgz/challenge-145
challenge-145 andrezgz solution
| -rw-r--r-- | challenge-145/andrezgz/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-145/andrezgz/perl/ch-2.pl | 59 |
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-145/andrezgz/perl/ch-1.pl b/challenge-145/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..ef50641095 --- /dev/null +++ b/challenge-145/andrezgz/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-145/ +# TASK #1 > Dot Product +# +# You are given 2 arrays of same size, @a and @b. +# +# Write a script to implement Dot Product. +# +# Example: +# @a = (1, 2, 3); +# @b = (4, 5, 6); +# +# $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 + +use strict; +use warnings; +use feature 'say'; + +my @a = split /,/, shift || die "First array of numbers is missing e.g. 1,2,3\n"; +my @b = split /,/, shift || die "Second array of numbers is missing e.g. 4,5,6\n"; + +die "Arrays should only contain numbers\n" if grep { /[^\d]/ } (@a,@b); +die "Arrays should have the same size\n" if @a != @b; + +my $dot_product = 0; +$dot_product += shift(@a) * shift(@b) while @a; +say $dot_product; + +__END__ + +$ ./challenge-145/andrezgz/perl/ch-1.pl 1,2,3 4,5,6 +32 diff --git a/challenge-145/andrezgz/perl/ch-2.pl b/challenge-145/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..7fd2814cf3 --- /dev/null +++ b/challenge-145/andrezgz/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-145/ +# Task #2 > Palindromic Tree +# +# You are given a string $s. +# +# Write a script to create a Palindromic Tree for the given string. +# +# I found this blog exaplaining Palindromic Tree in detail. +# https://medium.com/@alessiopiergiacomi/eertree-or-palindromic-tree-82453e75025b +# +# Example 1: +# Input: $s = 'redivider' +# Output: r redivider e edivide d divid i ivi v +# Example 2: +# Input: $s = 'deific' +# Output: d e i ifi f c +# Example 3: +# Input: $s = 'rotors' +# Output: r rotor o oto t s +# Example 4: +# Input: $s = 'challenge' +# Output: c h a l ll e n g +# Example 5: +# Input: $s = 'champion' +# Output: c h a m p i o n +# Example 6: +# Input: $s = 'christmas' +# Output: c h r i s t m a + +use strict; +use warnings; +use feature 'say'; + +my $str = shift || die "Argument string required\n"; +my $len = length $str; + +my %eertree; +for my $start (0 .. $len - 1) { + for my $chars (1 .. $len - $start ) { + my $w = substr($str, $start, $chars); + $eertree{$w} = $start.$chars if !exists $eertree{$w} && $w eq reverse $w; + } +} + +sub by_position { $eertree{$a} <=> $eertree{$b} }; +say join ' ', sort by_position keys %eertree; + +__END__ + +$ ./ch-2.pl rotors +r rotor o oto t s + +$ ./ch-2.pl christmas +c h r i s t m a + +$ ./ch-2.pl redivider +r redivider e edivide d divid i ivi v |
