diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-28 10:40:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-28 10:40:09 +0000 |
| commit | 727406876bf79d06abc2b7d878d9c6afeb6838a4 (patch) | |
| tree | 26e99ee2c4c473e7b5defad69e566687a4c2db1b /challenge-145 | |
| parent | 691eee706ef29c937d22e75349abce677082ed42 (diff) | |
| parent | 7191fb81bf1851e1a3804bf7975b6902432e889e (diff) | |
| download | perlweeklychallenge-club-727406876bf79d06abc2b7d878d9c6afeb6838a4.tar.gz perlweeklychallenge-club-727406876bf79d06abc2b7d878d9c6afeb6838a4.tar.bz2 perlweeklychallenge-club-727406876bf79d06abc2b7d878d9c6afeb6838a4.zip | |
Merge pull request #5428 from pauloscustodio/devel
Devel
Diffstat (limited to 'challenge-145')
| -rw-r--r-- | challenge-145/paulo-custodio/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-145/paulo-custodio/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-145/paulo-custodio/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-145/paulo-custodio/python/ch-2.py | 50 | ||||
| -rw-r--r-- | challenge-145/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-145/paulo-custodio/t/test-2.yaml | 30 |
6 files changed, 186 insertions, 0 deletions
diff --git a/challenge-145/paulo-custodio/perl/ch-1.pl b/challenge-145/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..bf986b31f1 --- /dev/null +++ b/challenge-145/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +# Challenge 145 +# +# TASK #1 > Dot Product +# Submitted by: Mohammad S Anwar +# 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 Modern::Perl; + +sub dot_product { + my(@ab) = @_; + my $n = int(@ab/2); + my $p = 0; + for my $i (0..$n-1) { + $p += $ab[$i]*$ab[$n+$i]; + } + return $p; +} + +say dot_product(@ARGV); diff --git a/challenge-145/paulo-custodio/perl/ch-2.pl b/challenge-145/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..2c1b966324 --- /dev/null +++ b/challenge-145/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Challenge 145 +# +# TASK #2 > Palindromic Tree +# Submitted by: Mohammad S Anwar +# 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. +# +# 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 Modern::Perl; + +# https://rosettacode.org/wiki/Eertree#Perl +sub build_eertree { + my($str) = @_; + + my @pal; + for my $n (1 .. length($str)) { + for my $m (1 .. length($str)-($n-1)) { + my $strpal = substr($str, $n-1, $m); + push @pal, $strpal if ($strpal eq reverse $strpal); + } + } + + my %seen; + say join ' ', grep {not $seen{$_}++} @pal; +} + +build_eertree(shift||""); diff --git a/challenge-145/paulo-custodio/python/ch-1.py b/challenge-145/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..bde8b1b742 --- /dev/null +++ b/challenge-145/paulo-custodio/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +# Challenge 145 +# +# TASK #1 > Dot Product +# Submitted by: Mohammad S Anwar +# 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 + +import sys +import numpy + +ab = [int(x) for x in sys.argv[1:]] +n = int(len(ab)/2) +print(numpy.dot(ab[:n], ab[n:])) diff --git a/challenge-145/paulo-custodio/python/ch-2.py b/challenge-145/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..0756bb13ae --- /dev/null +++ b/challenge-145/paulo-custodio/python/ch-2.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 + +# Challenge 145 +# +# TASK #2 > Palindromic Tree +# Submitted by: Mohammad S Anwar +# 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. +# +# 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 + +import sys + +# https://rosettacode.org/wiki/Eertree#Perl +def build_eertree(text): + pal = [] + for n in range(1, len(text)+1): + for m in range(1, len(text)-(n-1)+1): + strpal = text[n-1:n-1+m] + if strpal==strpal[::-1]: + pal.append(strpal) + + seen = set() + for strpal in pal: + if strpal not in seen: + print(strpal, end=" ") + seen.add(strpal) + print("") + +build_eertree(sys.argv[1]) diff --git a/challenge-145/paulo-custodio/t/test-1.yaml b/challenge-145/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6d13141d85 --- /dev/null +++ b/challenge-145/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 1 2 3 4 5 6 + input: + output: 32 diff --git a/challenge-145/paulo-custodio/t/test-2.yaml b/challenge-145/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..5f3d3ba49a --- /dev/null +++ b/challenge-145/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: redivider + input: + output: r redivider e edivide d divid i ivi v +- setup: + cleanup: + args: deific + input: + output: d e i ifi f c +- setup: + cleanup: + args: rotors + input: + output: r rotor o oto t s +- setup: + cleanup: + args: challenge + input: + output: c h a l ll e n g +- setup: + cleanup: + args: champion + input: + output: c h a m p i o n +- setup: + cleanup: + args: christmas + input: + output: c h r i s t m a |
