Perl Weekly Challenge 057: Invert Tree and Shortest Unique Prefix

Shortest Unique Prefix

Write a script to find the shortest unique prefix for each each word in the given list. The prefixes will not necessarily be of the same length.

Sample Input

[ "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" ]

Expected Output

[ "alph", "b", "car", "cadm", "cade", "alpi" ]

Let me start with the second task as it was definitely simpler (at least for me).

We iterate over all the input words. For each word, we try to find the shortest prefix possible. To know what prefixes have already been used, we keep two hashes: one stores the abandoned prefixes (i.e. those that were not unique anymore), the second one stores the “current” prefixes (the prefix is the key, the actual word is the value). We start from length 1 and add 1 in each step. If the prefix isn’t used and hasn’t been used, we assign it to the word and proceed to the next word. If the prefix is currently used for a different word, we store the prefix as “used” and prolong the prefix for the old word by one—but we continue the loop for the current word, in case their common prefix is longer.

PWC 055, Task #2: Wave Array

This blog post contains the "missing comments" from my contribution to the Perl Weekly Challenge 055. If you haven't read the Task #2 Problem Description: Wave Array you might want to do that first.

My submission for PWC 056 Task #2.

The Idea

Sort your array of numbers. Select a number. You have now two sub-arrays:
  1. The sub-array "to the left" of the selected number, and
  2. The sub-array "to the right" of the selected number.
This sounds trivial, but I want to point out that the setup has been done so that "less than or equal to" or "greater than or equal to" are not mentioned. These qualities are implicit with the sort of the data when we start. The algorithm then follows this idea:
  1. I have a sorted list of numbers listed vertically on a sheet of paper.
  2. I ask you to select any number in that list.
  3. I take that number, write it down,
  4. I draw a line through that number on the list.
REPEAT

Perl Weekly Challenge 81: Frequency Sort

These are some answers to the Week 81 of the Perl Weekly Challenge organized by Mohammad S. Anwar

Frequency Sort

You are given file named input.

Write a script to find the frequency of all the words.

It should print the result as the first column of each line should be the frequency of the the word followed by all the words of that frequency arranged in lexicographical order. Also sort the words in the ascending order of frequency.

Input file

CY's take on PWC#054

This is a part of Perl Weekly Challenge(PWC) #054 and the followings are related to my solution. If you want to challenge yourself on Perl, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email) if possible, before reading my blog post.


Apr03_2020.png

My laptop spent about 40.5 hours for calculating the list for the extra credit in task #2. While it was calculating, I found that my code hadn't been optimized. Anyway, even if I optimized it by 50%, the wait of 20 hours could still be a record for an impatient and blunt person like me.

2nd Apr, 2020
time | number_reached
1427 1
1745 309560
1809 325441
1831 339572
1852 353486
1951 386882
2205 453250
2253 475841
2308 482951
2358 502248

3rd Apr, 2020
time | number_reached
0419 600125
1405 772771
1534 794982
1538 796651
1543 798343
1555 800112 At this point, I realized that I should do more optimization.
1558 801872
1607 803625
1630 808808
1724 822777
1730 824482
1955 859075

Perl Weekly Challenge 056: Diff-K and Path Sum

Diff-K

You are given an array @N of positive integers (sorted) and another non negative integer $k. Write a script to find if there exists 2 indices $i and $j such that $A[$i] - $A[$j] == $k and $i != $j. It should print the pairs of indices, if any such pairs exist.

Example:

@N = (2, 7, 9);
$k = 2;

Output: 2, 1

I totally ignored the fact that the input array is sorted. My solution works for any input array, but it’s still rather efficient.

The basic trick is that we don’t have to compute $A[$i] - $A[$j] for each combination or $i and $j. We know $k from the very beginning, so we can just iterate the array for the first time to store it in a hash, and iterate it for the second time to check the hash whether the corresponding number exists in the array.

PWC 055, Task #1: Flip Binary

This blog post contains the "missing comments" from my contribution to the Perl Weekly Challenge 055. If you haven't read the Task #1 Problem Description: Flip Binary you might want to do that first.

Now to document a bit more, my submission for PWC 056 Task #1.

First, please feel free to suggest a better style of exposition. At work, I usually lard my code with comments galore, but with small demonstration programs I prefer to get as much code onto one page as possible, so now I'll try to illuminate a bit more my thoughts, no promises that they will be illuminating. Of course, let me know of any outright errors you catch as well, please.

The Idea

Perl Weekly Challenge 80: Smallest Positive Number Bits and Count Candies

These are some answers to the Week 80 of the Perl Weekly Challenge organized by Mohammad S. Anwar

Spoiler Alert: This weekly challenge deadline is due in several days (October 4, 2020). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Smallest Positive Number Bits

You are given unsorted list of integers @N.

Write a script to find out the smallest positive number missing.

Example 1:

Input: @N = (5, 2, -2, 0)
Output: 1

Example 2:

Input: @N = (1, 8, -1)
Output: 2

Example 3:

Input: @N = (2, 0, -1)
Output: 1

Smallest Positive Integer Missing in Raku

BLOG: The Weekly Challenge #054

https://perlweeklychallenge.org/blog/weekly-challenge-054

Keyhole surgery 100% successful

Part 1.

On Match 20th I had keyhole surgery to repair the aortic arch.

The lining had peeled off the wall. This happens when the lining develops a tear due to (in my case) childhood and later stress. Then, blood is pumped thru the tear and thus between the lining and the wall. Where the blood ought to go is called the 'true lumen' and when it's behind the lining and thus where is should not be - which is the bad news - , is called the 'false lumen'. So it's blood pressure in the false lumen which splits the lining off the wall. I just checked that original post and now realise I did not explain that at all.

Perl Weekly Challenge 053: Rotate Matrix and Vowel Strings

Rotate Matrix

Write a script to rotate the following matrix by given 90/180/270 degrees clockwise.
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ]

For example, if you rotate by 90 degrees then expected result should be like below

[ 7, 4, 1 ]
[ 8, 5, 2 ]
[ 9, 6, 3 ]

The easiest way to work with multidimensional data in Perl is PDL. Interestingly, I haven’t found a direct method to rotate a matrix in this way.

What I have found, though, was a method to transpose a matrix, which means to switch the columns and rows. The result for the sample input is

Perl Weekly Challenge 79: Count Set Bits and Trapped Rain Water

These are some answers to the Week 79 of the Perl Weekly Challenge organized by Mohammad S. Anwar

Spoiler Alert: This weekly challenge deadline is due in a couple of days (September 27, 2020). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Count Set Bits

You are given a positive number $N.

Write a script to count the total number of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007.

Example 1:

Rotation in R^2 - CY's take on PWC#053 Task 1

This is a part of Perl Weekly Challenge(PWC) #053 and the followings are related to my solution. If you want to challenge yourself on Perl, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email) if possible, before reading my blog post.

Do tell me if I am wrong or you strongly oppose my statements!

Oh. Task #1 has been funner than what I thought. I would like to introduce the "advanced" version I coded; it requests a specific module to run; well, I write these codes while I am studying OO hence a package (or module?or class? Which word is more suitable?) exists).

I have supplied a simpler script on GitHub, where the idea is based on a spiral.
#the spiral for the simpler script
    3,  2,  1, 
    4,  X,  0, 
    5,  6,  7
- - - - - - - - - - - - - - -
The idea behind this so-called "advanced" version is based on linear transformations on plane. In words:

new_position_vector = ReverseTranslation(Rotation(Translation(old_position_vector))) .

Content inside xy.pm

BLOG: The Weekly Challenge #053

https://perlweeklychallenge.org/blog/weekly-challenge-053

Back to Paws

It has been a little while since I played with my little PAWS and yes like many of us these days I have been just a little distracted, trip planned, trip changed, trip canceled etc etc etc.

Anyway to recap where I left off I was just getting the 'SubscribeToShard' action to work with a HTTP stream to work, after a fashion anyway. Then I got side tracked a little playing about with the problem of testing if the stream was correctly sending data down the pipe and if I was decoding it correctly.

As a byproduct of getting to the bottom of that I finally figured out what the PAWS 'Paginators' are for and I guess how to use them.

I noticed the odd "NextToken" tag in some of the Boto Json files as well most of the services have a ''paginators-1.json' definition file as well and looking at the Kinesis pod I see that there paginators listed.

PAGINATORS
Paginator methods are helpers that repetitively call methods that return partial results

Perl Weekly Challenge 78: Leader Element and Left Rotation

These are some answers to the Week 78 of of the Perl Weekly Challenge organized by Mohammad S. Anwar

Spoiler Alert: This weekly challenge deadline is due in a few days (September 20, 2020). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Leader Element

You are given an array @A containing distinct integers.

Write a script to find all leader elements in the array @A. Print (0) if none found.

An element is leader if it is greater than all the elements to its right side.

Example 1:

Input: @A = (9, 10, 7, 5, 6, 1)
Output: (10, 7, 6, 1)

Example 2:

Input: @A = (3, 4, 5)
Output: (5)

Comparing coronavirus SARS-CoV-2 spike protein sequences with BioPerl

Continuing from extraction of coronavirus spike protein sequences I decided to compare them and see if any mutations could be found. To do this I needed to align sequences to each other and get multiple sequence alignment. There are many tools that might be used and I have chosen MUSCLE as it is fast, easy to use and accurate enough. However, other tools such as MAFFT or T-Coffee should also work well and could give more accurate alignments in more complicated cases than the one I was dealing with.

Let’s gain some MUSCLE

For analysis of multiple sequence alignment (MSA) I chose BioPerl module Bio::Tools::Run::Alignment::Muscle. I found out that in order to install this module I need some dependencies. This worked for me with Perl v5.30 PDL edition on Windows:
cpanm -n XML::DOM::XPath cpanm Bio::DB::EUtilities Bio::Tools::Run::Alignment::Muscle
Next I installed MUSCLE

BLOG: The Weekly Challenge #052

https://perlweeklychallenge.org/blog/weekly-challenge-052

To-Study Plan: a beginning to OO in Perl

Now I have a todo list command-line script. I want to have a to-do list for my wikipedia editing activities, a to-study list for math I want to study sparely, a to-study-or-to-code in computer programming and maybe a to-read list for books.

The current format of the todo list script is roughly like this:

C:\Users\user>todo.pl h

Perl Weekly Challenge: Fibonacci Sum and Lonely X

These are some answers to the Week 77 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a couple of days (September 13, 2020). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Fibonacci Sum

You are given a positive integer $N.

Write a script to find out the smallest combination of Fibonacci Numbers required to get $N on addition.

You are NOT allowed to repeat a number. Print 0 if none found.

Example 1:

Input: $N = 6
Output:
    1 + 5 = 6

Example 2:

Input: $N = 9
Output:
    1 + 8 = 9

Perl Weekly Challenge 052: Stepping Numbers & Lucky Winner

Stepping Numbers

Write a script to accept two numbers between 100 and 999. It should then print all Stepping Numbers between them.

A number is called a stepping number if the adjacent digits have a difference of 1. For example, 456 is a stepping number but 129 is not.

The naive approach would be to iterate over all the numbers from 100 to 999 and check the difference between each adjacent digits.

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.