CY's take on PWC#065

Good morning from the Asian timezone.

This is a part of Perl Weekly Challenge(PWC) and the followings are related to my solutions. 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)(before Monday GMT+0 00:00) if possible, before reading my blog post.

๐Ÿค”

There are a lots we can discuss besides coding... They are on public politics. (1) I just want peace, as many Ladakhis wish. I am a non-comformist buddhist and I did travel to Ladakh, I miss people there but I have been in debted to them. And Mohammad Anwar, our organizer of Perl Weekly Challenge, is an Indian. His (and Ryan's) encouragement and effort has been one of the factors keeps me code for PWC. (2) I have mixed feelings towards "PRC" (see Wikipedia if you haven't been thinking of a country yet), and I believe it is the mode of the feelings of "silent majority" in Hong Kong... (My online activity might get (or have been ๐Ÿ˜…) cracked(i.e. disturbed by black-hat hackers) by expressing any positive feelings towards "PRC".)

Let go back to coding, a more rational place.

This had been a busy week for me, both from restaurant work, and responsibilities from hostel, therefore I thought I could just code and blog on Task #1. However, Task #2 is "lucrative" -- in the sense of diffculity -- , -- I could learn a lot from coding it --, -- if I coded it elegantly, I could show off my solution around --, I have finally coded a "dirty" solution and hope seeing other PWC teammates' elegant solutions.

Task #1 is a typical PWC problem. I would like to refer to a previous post which is for PWC #052. Firstly I coded the easiest way: enumeration from 100000 to 999999 (ch-1.pl) . Then it comes to consider a more elegant way. My ch-1a.pl uses recursion. Personally I was quite nervous with recursion. While I was using Windows only for coding, there are no easy ways to kill an application on command line. We have to kill the terminal screen and start all over. Now, under terminal on Linux, ^Z kills processes.

One of the important factors of a recursion is the "stop statement". The code uses number of digits as the indicator.

My ch-1a.pl is not very elegant. Skip it here.  <-- I should have checked it during blogging. Bugs discovered after official submission deadline.

The problem statement of Task #2 is a bit confusing for me.

I am not sure that whether it is a task for regular expression. My code even in "draft" condition is quite brute force: list out all partitions, check whether each substring is a palindrome. And then it comes to another problem: some of the answers are subsequence of some of the other answers, for example: using "aabaab" as input (Example 1 in problem statement), it generates 5 solutions: (1)"aabaa", (2)"aa", (3)"baab", (4) "aa" "baab", (5) "aba". (2) and (3) are not welcomed as they could be part of (4). Afterwards I wrote a brute-force routine in order to remove subsequences.

Sample Run:

$perl ch-2.pl bbcctccaabaabaabaaba
string: bbcctccaabaabaabaaba
bb,cc,aa,aabaa,aba
bb,cc,aa,aabaa,baab
bb,cc,aa,aabaabaa
bb,cc,aa,aba,abaaba
bb,cc,aa,aba,baab
bb,cc,aa,abaaba,aba
bb,cc,aa,abaabaaba
bb,cc,aa,baab,aabaa
bb,cc,aa,baab,aba
bb,cc,aa,baab,abaaba
bb,cc,aa,baabaab,aba
bb,cc,aa,baabaabaab
bb,cc,aabaa,aa,aba
bb,cc,aabaa,aa,baab
bb,cc,aabaa,abaaba
bb,cc,aabaa,baab,aa
bb,cc,aabaa,baab,aba
bb,cc,aabaa,baabaab
bb,cc,aabaabaa,aa
bb,cc,aabaabaa,aba
bb,cc,aabaabaa,baab
bb,cc,aabaabaabaa
bb,cc,aba,aa,baab
bb,cc,aba,abaabaaba
bb,cc,aba,baab,aa
bb,cc,abaabaaba,aba
bb,cc,abaabaabaaba
bb,cc,baab,aa,aba
bb,cctcc,aa,aabaa,aba
bb,cctcc,aa,aabaa,baab
bb,cctcc,aa,aabaabaa
bb,cctcc,aa,aba,abaaba
bb,cctcc,aa,aba,baab
bb,cctcc,aa,abaaba,aba
bb,cctcc,aa,abaabaaba
bb,cctcc,aa,baab,aabaa
bb,cctcc,aa,baab,aba
bb,cctcc,aa,baab,abaaba
bb,cctcc,aa,baabaab,aba
bb,cctcc,aa,baabaabaab
bb,cctcc,aabaa,aa,aba
bb,cctcc,aabaa,aa,baab
bb,cctcc,aabaa,abaaba
bb,cctcc,aabaa,baab,aa
bb,cctcc,aabaa,baab,aba
bb,cctcc,aabaa,baabaab
bb,cctcc,aabaabaa,aa
bb,cctcc,aabaabaa,aba
bb,cctcc,aabaabaa,baab
bb,cctcc,aabaabaabaa
bb,cctcc,aba,aa,baab
bb,cctcc,aba,abaabaaba
bb,cctcc,aba,baab,aa
bb,cctcc,abaabaaba,aba
bb,cctcc,abaabaabaaba
bb,cctcc,baab,aa,aba
bb,ctc,aa,aabaa,aba
bb,ctc,aa,aabaa,baab
bb,ctc,aa,aabaabaa
bb,ctc,aa,aba,abaaba
bb,ctc,aa,aba,baab
bb,ctc,aa,abaaba,aba
bb,ctc,aa,abaabaaba
bb,ctc,aa,baab,aabaa
bb,ctc,aa,baab,aba
bb,ctc,aa,baab,abaaba
bb,ctc,aa,baabaab,aba
bb,ctc,aa,baabaabaab
bb,ctc,aabaa,aa,aba
bb,ctc,aabaa,aa,baab
bb,ctc,aabaa,abaaba
bb,ctc,aabaa,baab,aa
bb,ctc,aabaa,baab,aba
bb,ctc,aabaa,baabaab
bb,ctc,aabaabaa,aa
bb,ctc,aabaabaa,aba
bb,ctc,aabaabaa,baab
bb,ctc,aabaabaabaa
bb,ctc,aba,aa,baab
bb,ctc,aba,abaabaaba
bb,ctc,aba,baab,aa
bb,ctc,abaabaaba,aba
bb,ctc,abaabaabaaba
bb,ctc,baab,aa,aba

The Three Most Useful Stuff Which I Have Learnt/firstly Applied This Time:

  • Use binary string to generate partitions
  • Reduce the duplicate solutions by hashes (learnt it from reading the blogpost of laurent_r on his blogpost on PWC#055 [1])
  • Remove the subsequences by index
  • "Extracting Unique Elements" from Perl Cookbook

The Most "Feel-Good" Stuff This Week in My Codes

  • Completing the Task #2 (though with dirt and dust) ๐Ÿ™„
  • Completing both a brute-force solution and a more effective solution for Task #1 [2]
I am looking forward to the challenge statements of Challenge #066 today. Learn to be a better coder. Maybe I do not have the natural talent of a hacker but I will try to be a better coder.


Remarks:

[1] http://blogs.perl.org/users/laurent_r/2020/04/perl-weekly-challenge-55-binary-numbers-and-wave-arrays.html
[2] This practice is actually imitated from Ryan (rjt):
https://ry.ca (his blog)


My code can be found in GitHub.

Stay healthy, for public hygiene
Stay healthy, mentally! (Invitation of cracking my passwords again?) โ–ก

1 Comment

You are doing great. We all love your contributions. Keep it up.

Leave a comment

About C.-Y. Fung

user-pic a beginner in programming; passionate in mathematics, distance running and gender issues; from Hong Kong. https://twitter.com/e7_87