DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 145

Challenge, My solutions

Sorry for no blog post last week. I was away from home catching up with family and friends during the Xmas period.

In both this weeks solutions, the Perl solution is a transliteration of the Python version.

TASK #1 › Dot Product

Task

You are given 2 arrays of same size, @a and @b.

Write a script to implement Dot Product.

My solution

So it isn't explicitly stated, but I've assumed a and b are an array of integers. It also isn't stated what the expected output is, so I've based it on the string in the example.

This is pretty straight forward. I take the first two values in command line input, and call them first_nums and second_nums. I then check they are the same size.

I then go through the arrays, and create a new array called dots that has the string (a × b) and another array called sums that has the result of a × b. Finally I use a print statement to display the result.

Examples

$ ./ch-1.py "(1, 2, 3)" "(4, 5, 6)"
(1 × 4) + (2 × 5) + (3 × 6) => 4 + 10 + 18 => 32

$ ./ch-1.pl "(1, 2, 3)" "(4, 5, 6)"
(1 × 4) + (2 × 5) + (3 × 6) => 4 + 10 + 18 => 32
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Palindromic Tree

Task

You are given a string $s.

Write a script to create a Palindromic Tree for the given string.

My solution

Okay, I fully expect Colin to complain about my solution, and rightly so. I decided to skip doing the whole tree thing, and use the sliding window method to compute the results. This is slower in computation terms, but faster in my ability produce a working solution :)

The sliding window method is straight forward. Set the start point as each character, and the end point as each possible remaining length of the string. If that part of the string hasn't been seen before and is a palindrome, add it to the strings list.

Examples

$ ./ch-2.py redivider
r redivider e edivide d divid i ivi v

$ ./ch-2.py deific
d e i ifi f c

$ ./ch-2.py rotors
r rotor o oto t s

$ ./ch-2.py challenge
c h a l ll e n g

$ ./ch-2.py christmas
c h r i s t m a
Enter fullscreen mode Exit fullscreen mode

Top comments (0)