CY's Take on PWC#067

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.


The discussion of Perl 7 in blogs.perl.org # was so hot last week made me too shy to write PWC experience (stop, it's just an excuse!).

Some discussions were quite technical for a beginner. Anyway as a beginning coder in Perl 5, I would add "use warnings" in my final coding stage from now on to prepare for the change.

PWC#67 Task #2: Letter Phone

I accidentally woke up at 3am and decided to finish this challenge. The code was completed within 45 min. What a record for me. :) Make use of hash of arrays.

PWC#67 Task #1: Number Combinations

At first, I wondered how come such standard combination and permutation tasks repeat to appear in PWC. After coding, it is uneasy but meaningful experience.

Thinking of the need of using recursion, I suddenly thought of using the combinatorical identity which every person finishing high school math would know:

C(n,r) = C(n-1,r-1) + C(n-1,r) ;

thus recursion can be avoid.

After revising high school math -- listing (sorted) combinations and the Pascal triangle -- on paper, I was quite confident; however, I had struggled with messages like "Experimental push on scalar is now forbidden" during solving this task.

Another difficulty of this task is the nested data structures. But we have Data::Dumper which also had helped a lot. My final bugged codeblock in this task was

1 while ($i<=$m) {
2     $A[$i][0] = [];
3     my @tempA1 = map {[$_]} (1..$i);
4     $A[$i][1] = \@tempA1;
5     $csize = 2;
6     while ($csize+1<=$i) {
7       my @temp0 = @{$A[$i-1][$csize]} ;
8       my @temp1 = map{ [ @{$_}, ] } @temp0; 
9       my @temp2 = @{$A[$i-1][$csize-1]};  
10      push @temp1, map { [ @{$_} , $i] } @{$A[$i-1][$csize-1]}; ]  
11      $A[$i][$csize] = \@temp1;
12      $csize++;
13  }
14  $A[$i][$i] =[1..$i];
15  $i++;
16  }

Through Data::Dumper, I got the bug was on line 14 and it should be [[1..$i]] instead of [1..$i].

PWC#66 Task 2

This is actually a very interesting task. Insight from an ex-mathlete, I quickly got we have to use factorization if we want to avoid direct counting (brute-force).

Then I read some articles on integer factorization on wikipedia, discovered interesting but a-bit-too-advanced-for-me stuff like "... congruence of squares modulo the integer N..."; however... again... They were irrelevent for the task.

And I wrote an unfinished brute-force code and it seemed faster than my factorization attempt!

Fixing the few-lines brute-force script had been on my "Perl Learning Todo".

By the way, there has been an easy lazy way: using is_power in Math::Prime::Util. :P


Remarks:

# which was said the "revitalization" proposal was rejected; I actually misunderstood here would be removed... Anyway here still one of the hubs of Perl coders, though small.

source of the image: User Bomkia on sv.wikipedia, public domain


We still have to live healthily during difficult times. Tough times never last but tough people do, as the old saying goes.

My code of Challenge #067 can be found in GitHub and the power integer task in Challenge #066 can be found nearby.

Leave a comment

About C.-Y. Fung

user-pic This blog is inactive and replaced by https://e7-87-83.github.io/coding/blog.html ; but I post highly Perl-related posts here.