diff options
| author | Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> | 2020-03-29 14:32:50 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-29 14:32:50 +0800 |
| commit | de36304d152a06501b2c79667da704789efa66dd (patch) | |
| tree | 63df4215863f11a8bfb4bb5462b7559bd8b45c5f | |
| parent | 82f7c612b098da64552875a18799aad6282dc430 (diff) | |
| download | perlweeklychallenge-club-de36304d152a06501b2c79667da704789efa66dd.tar.gz perlweeklychallenge-club-de36304d152a06501b2c79667da704789efa66dd.tar.bz2 perlweeklychallenge-club-de36304d152a06501b2c79667da704789efa66dd.zip | |
Add files via upload
| -rw-r--r-- | ch-1.pl | 55 | ||||
| -rw-r--r-- | ch-2.pl | 64 |
2 files changed, 119 insertions, 0 deletions
diff --git a/ch-1.pl b/ch-1.pl new file mode 100644 index 0000000000..10743d413e --- /dev/null +++ b/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/ch-2.pl b/ch-2.pl new file mode 100644 index 0000000000..a3472d9115 --- /dev/null +++ b/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, "");
+
|
