BLOG: The Weekly Challenge #062

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

Perl Weekly Challenge 104: Fusc Sequence and NIM Game

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

Spoiler Alert: This weekly challenge deadline is due in a couple of days (March 21, 2021). 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: Fusc Sequence

Write a script to generate first 50 members of FUSC Sequence. Please refer to OEIS for more information.

The sequence is defined as below:

fusc(0) = 0
fusc(1) = 1
for n > 1:
    when n is even: fusc(n) = fusc(n / 2),
    when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)

Fusc Sequence in Raku

Perl Weekly Challenge 059: Linked List and Bit Sum

Linked List

You are given a linked list and a value k. Write a script to partition the linked list such that all nodes less than k come before nodes greater than or equal to k. Make sure you preserve the original relative order of the nodes in each of the two partitions.

For example:

Linked List: 1 → 4 → 3 → 2 → 5 → 2
k = 3
Expected Output: 1 → 2 → 2 → 4 → 3 → 5.

We saw Linked List not so long ago, when solving the LRU Cache. Nevertheless, I didn’t reuse my solution, as I previously used a cyclic variant which doesn’t seem to be helpful here.

So, let’s start with a class of list elements. I call them “nodes”. Each node has a value and a link to a next node (undef if there’s none). A node can be disconnected from the next node, or a new node can be attached to it.

Coders In Cars Getting Chatty

The last time I spoke at Craft Conference, I also took part in what is probably the most fun and unusual interview of my career: Ivette Ördög’s Morning Commute.

Bear in mind, however, that this discussion took place back in 2018, so you’ll need to mentally s/Perl 6/Raku/ in a couple of places.

Even if you’re not interested in my random thoughts on the importance of linguistic diversity, Ivette’s other interviews are well worth watching.

PWC 058: Task #1, Compare Version & Task #2, Ordered Lineup

PWC Task #1, Compare Version

jaredor submission for Task #1

I usually am bored with string twisting (as opposed to bit twiddling) because there is a certain level of grunt work, even with such classics as ELIZA and FESTOON and I can easily fall down the rabbit hole of trying to get pluralization perfect. So when the exciting topic of version numbers came up, I would have passed, but we perfectionists have to finish one to get to two....

Plus there was one thing that I liked, the problem was set up as a comparison operator, like <=> and cmp, suitable for use in a sort routine. Whomever you are out there, with a need to sort thousands of version strings via perl, this script is for you ;-)

Input

Input is on the command line. The versions will be compared in pairs. Creating a version sort routine of command line arguments was considered, but I decided to emulate the problem statement example output instead.

Details

Perl Weekly Challenge 100: Fun Time and Triangle Sum

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

Task 1: Fun Time

You are given a time (12 hour / 24 hour).

Write a script to convert the given time from 12 hour format to 24 hour format and vice versa.

Ideally we expect a one-liner.

Example 1:

Input: 05:15 pm or 05:15pm
Output: 17:15

Example 2:

Input: 19:15
Output: 07:15 pm or 07:15pm

Monthly Report - April

New release of RT::Client::REST

A very welcome PR for adding the new SLA parameters for RT 4.4.3 was provided to RT::Client::REST on githib, which went out in v0.57 just earlier this week.

However this spurred me to take care of another PR that was been floating which allowed more verbose error messaging to be enabled. I also returned to my proposed fix for RT118729 which is because of mishandling of RT's strange "REST" (it's not really) interface.

If you are using RT::Client::REST i urge you to update it. Testing it carefully first before deploying. Bug reports are welcome especially when a fix is provided!

See https://metacpan.org/release/RT-Client-REST

CY's take on Perl Weekly Challenge #057

This is a part of Perl Weekly Challenge(PWC) #057 and the followings are related to my solution.

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

Task 1: Invert Tree

There is a module Tree::Binary on CPAN and its method "mirror" does what exactly describe in the Task 1. Of course, the experience of using a shortcut won't be filled a blog post.

Last week (PWC #056) I did not attempt the binary tree task but I did read the blogs of other PWC members.

Hence, it's time for my "blog report". Blog posts I use as reference are

Discovered from reading, one of the ways of representing a binary tree which I hadn't thought of but very intuitive, is putting the nodes row by row!

Just like this:

Perl Weekly Challenge 99: Pattern Match and Unique Subsequence

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

Spoiler Alert: This weekly challenge deadline is due in a couple of days (February 14, 2021). 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: Pattern Match

You are given a string $S and a pattern $P.

Write a script to check if given pattern validate the entire string. Print 1 if pass otherwise 0.

The patterns can also have the following characters: - ? - Match any single character. - * - Match any sequence of characters.

Example 1:

Input: $S = "abcde" $P = "a*e"
Output: 1

Example 2:

Input: $S = "abcde" $P = "a*d"
Output: 0

Example 3:

BLOG: The Weekly Challenge #057

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

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 057: Task #1, Invert Tree & Task #2, Shortest Unique Prefix

PWC Task #1, Invert Tree

jaredor submission for Task #1

The problem is in two parts, flipping the tree and pretty-printing it.

The flipping part is pretty easy, but since I'm a huge fan of Higher Order Perl I thought I should at least try to make it sort of like the tree walking code I remembered reading, where you give the tree-walker the function you want to operate on each node. (That word, "remembered" should be a hint that I haven't read the book in years and you should really go read the master.) I wrote both a depth-first and a breadth-first binary tree walker. For the purposes of flipping the whole tree, either one would have sufficed, but it is handy to have the option when you are experimenting.

Perl Weekly Challenge 98: Read N-Characters and Search Insert Position

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

Task 1: Read N-characters

You are given file $FILE.

Create subroutine readN($FILE, $number) that returns the first n-characters and moves the pointer to the (n+1)th character.

Example:

Input: Suppose the file (input.txt) contains "1234567890"
Output:
    print readN("input.txt", 4); # returns "1234"
    print readN("input.txt", 4); # returns "5678"
    print readN("input.txt", 4); # returns "90"

Read N-characters in Raku

This is my first attempt:

BLOG: The Weekly Challenge #056

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

the Giant Planet of Perl

Finally I saw posts of PWC#056 on blogs.perl.org .

I haven't found what to discuss about #056 Task #1. Just to keep people know this code producer is alive and healthy, I share my recent life:

On Perl resources:

1. Perl Monks

From a blogpost[1], I was hooked to https://www.perlmonks.com/ . Apart from many advanced Perl discussions, there is a book review section (not very active):

Book Reviews of Perl Monks

Yesterday I got the "Perl Debugged" and "Perl Best Practices" on my hand, which are both recommended for fresh-to-intermediate Perl programmers. Yeah!

( The Best Practices seems to be accepted as one of commons among Perl programmers. There is Perl::Critic (learnt from the PWC Champion Interview March 2020). And there is a reference sheet on the book. )

The monks also have a tutorial section:

Tutorials of Perl Monks

2. Perl Weekly

I discovered the perlweekly.com by... Searching my own name on the Internet. (Sorry, I have some kinds of narcissistic behaviour).

PWC 056: Task #1, Diff-K & Task #2, Path Sum

After posting two separate blogs for PWC 055 and seeing how awkward the explanations were, I'll try a new tack: Both submissions will be elaborated in one blog post. The elaborations will not be explanations. I'll focus more on the "idea" part and let any programming details come out in the comments, if at all.

PWC Task #1, Diff-K

jaredor submission for Task #1

Input

For input I decided that all the numbers in the array would be command line arguments. That meant that 'k' would have to be a command line option. There was some validation that the stated requirements were met.

One input requirement "given an array @N of positive integers (sorted)" seemed to say that the input array should be sorted. The algorithm I chose worked without any sorting requirement on input, so this was not checked.

Case 1: k > 0

Perl Weekly Challenge 97: Caesar Cipher and Binary Substrings

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

Task 1: Caesar Cipher

You are given string $S containing only the letters A..Z and a number $N.

Write a script to encrypt the given string $S using a Caesar Cipher with left shift of size $N.

Example:

Input: $S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", $N = 3
Output: "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"

Plain:    ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher:   XYZABCDEFGHIJKLMNOPQRSTUVW

Plaintext:  THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD

Perl Weekly Challenge 058: Compare Version and Ordered Lineup

Compare Version

Compare two given version number strings v1 and v2 such that:
  • If v1 > v2 return 1
  • If v1 < v2 return -1
  • Otherwise, return 0

The version numbers are non-empty strings containing only digits, and the dot (“.”) and underscore (“_”) characters. (“_” denotes an alpha/development version, and has a lower precedence than a dot, “.”). Here are some examples:

v1v2Result
0.1<1.1-1
2.0>1.21
1.2<1.2_5-1
1.2.1>1.2_11
1.2.1=1.2.10

When I read the task assignment, I thought to myself: I’m not the first person in the world that needs to compare versions. There already must be a module on CPAN that does exactly that. As usually, it wasn’t so simple.

BLOG: The Weekly Challenge #058

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

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.