March 2020 Archives

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
package xy;
use strict;

sub new {
    my ($class) = @_;
    bless{
        _value=> $_[1],
        _x=>$_[2],
        _y=>$_[3],
      }, $class;
}

sub x{ $_[0]->{_x}}
sub y{ $_[0]->{_y}}
sub value{ $_[0]->{_value} }

1;
Highlights of the Content in ch-1a.pl
#!/usr/bin/perl
use strict;
use xy;
# Perl Weekly Challenge #053 Task #1
# 90/180/270 degree clockwise Rotation of a N x N square matrix
# Usage(example): put the module in proper place, then:
#                 $ ch-1a.pl 3 90 1 2 3 4 5 6 7 8 9

my $N = shift @ARGV;

my $ANGLE = shift @ARGV;

my $hN = $N/2 + 0.5;

#...
sub rcaqx {
#short for Rotation_Clockwise_A_Quarter, x stands for multiple if ($_[2]>=1) { my ($xcoord, $ycoord) = ($_[0], $_[1]); $_[0] = $ycoord; $_[1] = -$xcoord; return rcaqx($_[0], $_[1], $_[2]-1);
# I know we can skip the use of temporary variables to be more concise
# but the version now is more readable to me } else {return ($_[0], $_[1])} }
#...

sub position { return $_[0] + $N*($N-$_[1]) - 1 } for $i (1..$N*$N) { $newmatrix->[$i] = xy->new($matrix->[$i]->value, translation_add_negT(rcaqx ((translation_add_T( $matrix->[$i]->x, $matrix->[$i]->y) ), $ANGLE/90 )) ); $coordinateplane[position( $newmatrix->[$i]->x, $newmatrix->[$i]->y )] = $newmatrix->[$i]->value; }
# print out the result for (0..$N*$N-1) { if ($_ % $N == 0) {print "\n"}; printf "%3d " , $coordinateplane[$_]; }

# ref: https://en.wikipedia.org/wiki/Rotation_matrix # For N=3, the coordinates and the corresponding index of @coordinateplane: # (1,3) (2,3) (3,3) 0, 1, 2, # (1,2) (2,2) (3,2) 3, 4, 5, # (1,1) (2,1) (3,1) 6, 7, 8 # For N=5: # (1,5) (2,5) (3,5) (4,5) (5,5) 0, 1 ,2 ,3, 4, # (1,4) (2,4) (3,4) (4,4) (5,4) 5, 6, 7, 8, 9, # (1,3) (2,3) (3,3) (4,3) (5,3) 10,11,12,13,14, # (1,2) (2,2) (3,2) (4,2) (5,2) 15,16,17,18,19, # (1,1) (2,1) (3,1) (4,1) (5,1) 20,21,22,23,24 ============================
Notes/Aftermath:

  • Just discover that more than one source codes can be submitted for a same task in PWC; we just name the late comers by ch-1a.pl ch-1b.pl ch-1c.pl ...
  • Having been using the web interface of GitHub, I have a short-term goal: learning to use Git and GitHub in console.
  • Just read the review of my mentionable code for PWC #049 Task #1, reviewed by Ryan. Yet another short-term goal: Need to learn how to document and name the variables well. XP
  • Yet one more short-term goal: learn to use Test::More and Test::Deep .
  • ++: (optional) learn to use perl default debugger .
There are lots of things to be discovered in the serious coders' world... (17:43 in GMT+8 Timezone)

Tree as a tool for enumeration - CY's take on PWC#053 Task 2

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!

( The first task is simple.
Not much to say about this task.
4-3 = 1; 4-2 =2; 4-1 = 3.

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

Command-line Hacker's Todo List, v0.1, GNU gpl-3.0; I work with a file "hackerstodo.txt" on the destination location.
todo.pl a : add new item
todo.pl t 2 : make the second item become the first priority/users/c_y_fung/2020/03/index.html

CY's Take on Perl Weekly Challenge #052

This is a part of Perl Weekly Challenge(PWC) #052 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!

I register a free account on blogs.perl.org . The process is surprisingly easy but I am a bit nervous that m…

On my technical Background - a beginner's story

Different people have come to an interest or develop a skill for different reasons. Nowadays, lots people are learning Python, R, JavaScript, Java, etc. for job hunting as data scientists or software engineers...

Beginner's Enthusiasm in Programming

For me, solitaires (games) always attract me. Programming has been felt like a solitaire . (Some people must disagree with me on this line. They are lucky. And I also want myself can grow as strong as a contributor to the opensource programming community.) Testing. Possible modifications. I learnt LOGO programming language in t…

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.