aboutsummaryrefslogtreecommitdiff
path: root/challenge-129
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-09-08 14:22:07 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-09-08 14:22:07 +0800
commit35e8a6b42a11ec752c2b9f26a45f9ec33b305487 (patch)
treeab272ac63fbe75cb21f2d123a1d439a8b18289fd /challenge-129
parent8c96f55ac870f91ad167048422465538dad3ac3d (diff)
downloadperlweeklychallenge-club-35e8a6b42a11ec752c2b9f26a45f9ec33b305487.tar.gz
perlweeklychallenge-club-35e8a6b42a11ec752c2b9f26a45f9ec33b305487.tar.bz2
perlweeklychallenge-club-35e8a6b42a11ec752c2b9f26a45f9ec33b305487.zip
week 129
Diffstat (limited to 'challenge-129')
-rw-r--r--challenge-129/cheok-yin-fung/blog.txt1
-rw-r--r--challenge-129/cheok-yin-fung/perl/ch-1.pl74
-rw-r--r--challenge-129/cheok-yin-fung/perl/ch-2.pl231
3 files changed, 306 insertions, 0 deletions
diff --git a/challenge-129/cheok-yin-fung/blog.txt b/challenge-129/cheok-yin-fung/blog.txt
new file mode 100644
index 0000000000..632289ba5f
--- /dev/null
+++ b/challenge-129/cheok-yin-fung/blog.txt
@@ -0,0 +1 @@
+https://e7-87-83.github.io/coding/challenge_129.html
diff --git a/challenge-129/cheok-yin-fung/perl/ch-1.pl b/challenge-129/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..36dff7a037
--- /dev/null
+++ b/challenge-129/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+# The Weekly Challenge 129
+# Task 1 Root Distance
+# Usage: $ ch-1.pl
+use v5.12.0;
+use warnings;
+use Test::More tests => 9;
+use Object::Pad 0.51;
+use Object::Pad::SlotAttr::Isa;
+
+class TreeNode {
+ has $parent :reader :writer :param :Isa(TreeNode) = undef;
+ has $list_of_children :reader :writer :param = [];
+ has $is_root :reader :param = 0;
+
+ method set_children {
+ my $c = $_[0];
+ $c->set_parent($self);
+ push $list_of_children->@*, $c;
+ }
+
+ method root_distance {
+ my $p = $self;
+ my $d = 0;
+ while (!$p->is_root) {
+ $p = $p->parent;
+ $d++;
+ }
+ return $d;
+ }
+
+ BUILD {
+ if (defined($parent)) {
+ $parent->set_children($self);
+ }
+ else {
+ $is_root = 1;
+ }
+ }
+}
+
+# Example 1
+my $n1 = TreeNode->new();
+my $n2 = TreeNode->new(parent => $n1);
+my $n3 = TreeNode->new(parent => $n1);
+
+my $n4 = TreeNode->new(parent => $n3);
+my $n5 = TreeNode->new(parent => $n4);
+my $n6 = TreeNode->new(parent => $n4);
+
+# Example 2
+my $t1 = TreeNode->new();
+my $t2 = TreeNode->new(parent => $t1);
+my $t3 = TreeNode->new(parent => $t1);
+
+my $t4 = TreeNode->new(parent => $t2);
+my $t5 = TreeNode->new(parent => $t3);
+my $t6 = TreeNode->new(parent => $t4);
+
+my $t7 = TreeNode->new(parent => $t5);
+my $t8 = TreeNode->new(parent => $t6);
+my $t9 = TreeNode->new(parent => $t6);
+
+ok $n6->root_distance() == 3, "Example 1 - Node 6";
+ok $n5->root_distance() == 3, "Example 1 - Node 5";
+ok $n2->root_distance() == 1, "Example 1 - Node 2";
+ok $n4->root_distance() == 2, "Example 1 - Node 4";
+
+ok $t7->root_distance() == 3, "Example 2 - Node 7";
+ok $t8->root_distance() == 4, "Example 2 - Node 8";
+ok $t6->root_distance() == 3, "Example 2 - Node 6";
+
+ok $n1->root_distance() == 0, "Example 1 - Node 1";
+ok $t1->root_distance() == 0, "Example 2 - Node 1";
diff --git a/challenge-129/cheok-yin-fung/perl/ch-2.pl b/challenge-129/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..f65e805dbd
--- /dev/null
+++ b/challenge-129/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,231 @@
+#!/usr/bin/perl
+# The Weekly Challenge 129
+# Task 2 Add Linked Lists
+# Usage: $ ch-2.pl
+
+# From the Task description:
+#... expecting a class representing linked list.
+# It should have methods to create a linked list
+# given list of single digit positive numbers (I) and
+# a method to add new member. (II) Also have a method
+# that takes 2 linked list objects and returns a
+# new linked list. (III) Finally a method to print the
+# linked list object in a user friendly format. (IV)
+
+# methods in the class LinkedList perform required actions:
+# (I) create_list
+# (II) append_element
+# (III) wk129_add_another_linked_list
+# (IV) print
+
+use v5.12.0;
+use warnings;
+use Object::Pad 0.52;
+use Object::Pad::SlotAttr::Isa;
+
+
+class LLNode {
+ has $next :reader :writer :param :Isa(LLNode) = undef;
+ has $val :reader :writer :param = undef;
+
+ method a_copy {
+ return LLNode->new(val=>$self->val);
+ }
+}
+
+
+
+class LinkedList {
+
+ has $head_node :reader :writer :param :Isa(LLNode) = undef;
+
+
+
+ method create_list {
+ my @digit = @_;
+ $head_node = LLNode->new(val => $digit[0]);
+
+ my $p = $head_node;
+ for my $t (1..$#digit) {
+ $p->set_next(LLNode->new(val=>$digit[$t]));
+ $p = $p->next;
+ }
+ }
+
+
+ method append_element {
+ # an O(N) operation, seldem used)
+ my $dgt = $_[0];
+ my $p = $head_node;
+ while (defined($p->next)) {
+ $p = $p->next;
+ }
+ $p->set_next( LLNode->new(val=>$dgt) );
+ }
+
+
+ method a_reverse_linked_list {
+ my $rvrs = LinkedList->new();
+ my @stack;
+ my $p = $head_node;
+ push @stack, $p->a_copy;
+ $p = $p->next;
+ while (defined($p)) {
+ push @stack, $p->a_copy;
+ $p = $p->next;
+ }
+
+ my $t = pop @stack;
+ $rvrs->set_head_node( $t );
+ while (scalar @stack > 0) {
+ $t->set_next($stack[-1]);
+ $t = pop @stack;
+ }
+ return $rvrs;
+ }
+
+
+
+ method a_reverse_linked_list_with_carriers {
+ my $rvrs = LinkedList->new();
+
+ my @stack;
+ my $p = $head_node;
+ my $carrier = 0;
+ while (defined($p)) {
+ my $temp_val = $p->val+$carrier;
+ if ($temp_val >= 10) {
+ $temp_val -= 10;
+ $carrier = 1;
+ }
+ else {
+ $carrier = 0;
+ }
+ push @stack, LLNode->new(val=>$temp_val);
+ $p = $p->next;
+ }
+
+ my $t = pop @stack;
+ $rvrs->set_head_node( $t );
+ while (scalar @stack > 0) {
+ $t->set_next($stack[-1]);
+ $t = pop @stack;
+ }
+
+ if ($carrier == 1) {
+ warn "WARNING: Should \"1\" be the head node of linked list?\n";
+ }
+
+ return $rvrs;
+ }
+
+
+
+ method directly_add_another_linked_list {
+ my $sum = LinkedList->new();
+ my $another = $_[0];
+ my $s = $self->head_node;
+ my $a = $another->head_node;
+ $sum->set_head_node(LLNode->new( val => $s->val + $a->val ));
+ my $p = $sum->head_node;
+ while (defined($s->next) && defined($a->next)) {
+ $s = $s->next;
+ $a = $a->next;
+ $p->set_next( LLNode->new( val => $s->val + $a->val ) );
+ $p = $p->next;
+ }
+ while (defined($s->next)) {
+ $s = $s->next;
+ $p->set_next( LLNode->new( val => $s->val ) );
+ $p = $p->next;
+ }
+ while (defined($a->next)) {
+ $a = $a->next;
+ $p->set_next( LLNode->new( val => $a->val ) );
+ $p = $p->next;
+ }
+ return $sum;
+ }
+
+
+
+ method wk129_add_another_linked_list {
+ return $self->a_reverse_linked_list
+ ->directly_add_another_linked_list(
+ $_[0]->a_reverse_linked_list
+ )
+ ->a_reverse_linked_list_with_carriers;
+ }
+
+
+
+ method print {
+ my $p = $head_node;
+ print $p->val;
+ while (defined($p = $p->next)) {
+ print "->", $p->val;
+ }
+ print "\n";
+ }
+
+
+
+}
+
+say "Example 1";
+
+my $ex1_L1 = LinkedList->new();
+my $ex1_L2 = LinkedList->new();
+
+$ex1_L1->create_list(1,2,3);
+$ex1_L2->create_list(3,2,1);
+print " L1: "; $ex1_L1->print;
+print " L2: "; $ex1_L2->print;
+print "sum: ";
+$ex1_L1->wk129_add_another_linked_list($ex1_L2)->print;
+say "";
+
+say "Example 2";
+
+my $ex2_L1 = LinkedList->new();
+my $ex2_L2 = LinkedList->new();
+
+$ex2_L1->create_list(1,2,3,4,5);
+$ex2_L2->create_list(6,5,5);
+
+print " L1: "; $ex2_L1->print;
+print " L2: "; $ex2_L2->print;
+print "sum: ";
+$ex2_L1->wk129_add_another_linked_list($ex2_L2)->print;
+say "";
+
+say "Test case 1";
+
+my $test1_L1 = LinkedList->new();
+my $test1_L2 = LinkedList->new();
+
+$test1_L1->create_list(9, 3);
+$test1_L2->create_list(5);
+$test1_L2->append_element(5);
+
+print " L1: "; $test1_L1->print;
+print " L2: "; $test1_L2->print;
+print "sum: ";
+$test1_L1->wk129_add_another_linked_list($test1_L2)->print;
+
+say "";
+
+say "Test case 2";
+
+my $test2_L1 = LinkedList->new();
+my $test2_L2 = LinkedList->new();
+
+$test2_L1->create_list(1, 4, 2, 8, 5, 7);
+$test2_L2->create_list(8, 5, 7, 1, 4, 2);
+
+print " L1: "; $test2_L1->print;
+print " L2: "; $test2_L2->print;
+print "sum: ";
+$test2_L1->wk129_add_another_linked_list($test2_L2)->print;
+
+say "";