diff options
| -rw-r--r-- | challenge-053/cheok-yin-fung/perl/ch-1.pl | 55 | ||||
| -rw-r--r-- | challenge-053/cheok-yin-fung/perl/ch-1a.pl | 93 | ||||
| -rw-r--r-- | challenge-053/cheok-yin-fung/perl/ch-2.pl | 64 | ||||
| -rw-r--r-- | challenge-053/cheok-yin-fung/perl/xy.pm | 17 |
4 files changed, 229 insertions, 0 deletions
diff --git a/challenge-053/cheok-yin-fung/perl/ch-1.pl b/challenge-053/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..10743d413e --- /dev/null +++ b/challenge-053/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl
+use strict;
+
+# Perl Weekly Challenge #053 Task #1
+# Usage : ch-1.pl [the ANGLE, 90's multiples] 1 2 3 4 5 6 7 8 9
+# Example Input : ch-1.pl 90 1 2 3 4 5 6 7 8 9
+# Example Output:
+# 7 4 1
+# 8 5 2
+# 9 6 3
+
+my $ANGLE = shift @ARGV;
+
+my @cp = ( #short for coordinateplane
+ 3, 2, 1,
+ 4, -1, 0,
+ 5, 6, 7);
+# contrast with the natural array 3x3->9
+# 0 1 2
+# 3 4 5
+# 6 7 8
+
+my %hash;
+foreach (1..9) {
+ if ($_>=0) {$hash{$cp[$_-1]} = $ARGV[$_-1];}
+}
+$hash{-1} = @ARGV[4];
+
+
+sub rcaqx { #short for Rotation_Clockwise_A_Quarter, x for multiple
+ if ($_[0]>=1) {
+ my %nhash;
+ for (0..8) {
+ if ( $cp[$_] >= 0 ) {
+ $nhash{ $cp[$_] } = $hash{ ($cp[($_ )]+2) % 8 };
+ }
+ }
+ $nhash{-1} = $hash{-1};
+ %hash = %nhash;
+ rcaqx($_[0]-1);
+ }
+}
+
+rcaqx($ANGLE/90);
+
+
+for (0..8) {
+ print "\n" if $_ % 3 == 0;
+ if ($cp[$_] != -1) {
+ print $hash{$cp[$_]}, " ";
+ } else {
+ print $hash{-1}," ";
+ }
+}
+
diff --git a/challenge-053/cheok-yin-fung/perl/ch-1a.pl b/challenge-053/cheok-yin-fung/perl/ch-1a.pl new file mode 100644 index 0000000000..a64e7386f5 --- /dev/null +++ b/challenge-053/cheok-yin-fung/perl/ch-1a.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl
+use strict;
+use xy; #upload the module with the perl script
+
+# 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;
+
+my @temp = (-$hN, -$hN);
+my $T = \@temp;
+
+
+sub translation_add_negT {
+ $_[0] -= $T->[0];
+ $_[1] -= $T->[1];
+ return ($_[0], $_[1])
+}
+
+sub translation_add_T {
+ $_[0] += $T->[0];
+ $_[1] += $T->[1];
+ return ($_[0], $_[1])
+}
+
+
+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);
+ } else {return ($_[0], $_[1])}
+}
+
+my $xcoord = 0;
+my $ycoord = $N;
+my $i = 0;
+my $matrix;
+foreach (@ARGV) {
+ $i++;
+ $xcoord++;
+ $matrix->[$i] = xy->new($_, $xcoord, $ycoord);
+ if ($xcoord == $N) {
+ $ycoord--;
+ $xcoord=0;
+ }
+}
+
+die "Not a square matrix" unless $i==$N*$N;
+
+my $newmatrix;
+
+my @coordinateplane;
+
+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;
+}
+
+
+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, the coordinates are:
+# (1,5) (2,5) (3,5) (4,5) (5,5)
+# (1,4) (2,4) (3,4) (4,4) (5,4)
+# (1,3) (2,3) (3,3) (4,3) (5,3)
+# (1,2) (2,2) (3,2) (4,2) (5,2)
+# (1,1) (2,1) (3,1) (4,1) (5,1)
diff --git a/challenge-053/cheok-yin-fung/perl/ch-2.pl b/challenge-053/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..a3472d9115 --- /dev/null +++ b/challenge-053/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl
+# Perl Weekly Challenge #053 Task 2
+use Tree;
+use Switch;
+use strict;
+use warnings;
+
+my $N = $ARGV[0]; # Usage: perl ch-2 N
+
+my $vowel = Tree->new( "" );
+
+sub produce_child {
+ my $t = $_[0];
+ if ($t->size == 1) { switch($t->value) {
+ case ("a") {$t->add_child(Tree->new("e")); $t->add_child(Tree->new("i"));}
+ case ("e") {$t->add_child(Tree->new("i"));}
+ case ("i") {$t->add_child(Tree->new("a")); $t->add_child(Tree->new("e"));
+ $t->add_child(Tree->new("o")); $t->add_child(Tree->new("u"));}
+ case ("o") {$t->add_child(Tree->new("a")); $t->add_child(Tree->new("u"));}
+ case ("u") {$t->add_child(Tree->new("e")); $t->add_child(Tree->new("o"));}
+ }
+ }
+}
+
+my $tree_a = Tree->new("a");
+my $tree_e = Tree->new("e");
+my $tree_i = Tree->new("i");
+my $tree_o = Tree->new("o");
+my $tree_u = Tree->new("u");
+
+$vowel->add_child( $tree_a );
+$vowel->add_child( $tree_e );
+$vowel->add_child( $tree_i );
+$vowel->add_child( $tree_o );
+$vowel->add_child( $tree_u );
+
+#######################################
+
+# grow : traverse and force the tree nodes produce child(ren)
+sub grow {
+ my ($t, $d) = ($_[0], $_[1]);
+ produce_child($t);
+ my @t_baby = $t->children;
+ foreach (@t_baby) {
+ grow($_, $d) if $_->depth < $d;
+ }
+}
+
+#print out the required strings
+sub traverse_and_named {
+ my ($t, $d, $str) = ($_[0], $_[1], $_[2]);
+ print $str."\n" if length($str)==$d;
+ my @t_baby = $t->children;
+ foreach (@t_baby) {
+ traverse_and_named($_, $d, $str.$_->value) if $_->depth<=$d;
+ }
+}
+
+#######################################
+
+grow($vowel, 5);
+
+traverse_and_named($vowel, $N, "");
+
diff --git a/challenge-053/cheok-yin-fung/perl/xy.pm b/challenge-053/cheok-yin-fung/perl/xy.pm new file mode 100644 index 0000000000..809c126e91 --- /dev/null +++ b/challenge-053/cheok-yin-fung/perl/xy.pm @@ -0,0 +1,17 @@ +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;
|
