diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-10 06:59:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-10 06:59:45 +0000 |
| commit | e8b480311ae3d797ff3a4b3d90a5b620fcb126bc (patch) | |
| tree | 839a367dfc31fc69fc4dc4a0c9ce5da176e6e5fc /challenge-094 | |
| parent | a6823a41d7419c1ee4bc9d24d254e834c7451141 (diff) | |
| parent | d85e4dbd7c27346980d4bb81e6eab6b057652c2d (diff) | |
| download | perlweeklychallenge-club-e8b480311ae3d797ff3a4b3d90a5b620fcb126bc.tar.gz perlweeklychallenge-club-e8b480311ae3d797ff3a4b3d90a5b620fcb126bc.tar.bz2 perlweeklychallenge-club-e8b480311ae3d797ff3a4b3d90a5b620fcb126bc.zip | |
Merge pull request #3199 from E7-87-83/master
2 Perl Scripts, and a Java solution for task 2, a blogpost
Diffstat (limited to 'challenge-094')
| -rw-r--r-- | challenge-094/cheok-yin-fung/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-094/cheok-yin-fung/java/BT2LL.java | 62 | ||||
| -rw-r--r-- | challenge-094/cheok-yin-fung/java/BinaryTree.java | 46 | ||||
| -rw-r--r-- | challenge-094/cheok-yin-fung/java/LinkedList.java | 58 | ||||
| -rw-r--r-- | challenge-094/cheok-yin-fung/perl/ch-1.pl | 68 | ||||
| -rw-r--r-- | challenge-094/cheok-yin-fung/perl/ch-2.pl | 153 |
6 files changed, 388 insertions, 0 deletions
diff --git a/challenge-094/cheok-yin-fung/blog.txt b/challenge-094/cheok-yin-fung/blog.txt new file mode 100644 index 0000000000..01dc4ccbb4 --- /dev/null +++ b/challenge-094/cheok-yin-fung/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/c_y_fung/2021/01/cys-take-on-pwc094.html diff --git a/challenge-094/cheok-yin-fung/java/BT2LL.java b/challenge-094/cheok-yin-fung/java/BT2LL.java new file mode 100644 index 0000000000..de1470ccf2 --- /dev/null +++ b/challenge-094/cheok-yin-fung/java/BT2LL.java @@ -0,0 +1,62 @@ +// The Weekly Challenge #094 Task 2 +// ref:* hhttps://www.includehelp.com/data-structure-tutorial/ +// preorder-traversal-in-binary-tree-iteratively-without-recursion.aspx +// * https://www.journaldev.com/13401/java-stack + +import java.util.Scanner; +import java.util.Stack; + +public class BT2LL +{ + public static LinkedList traversal (BinaryTree t) + { + + LinkedList.LLNode h = new LinkedList.LLNode(0); + LinkedList target = new LinkedList(); + target.setHead(h); + + Stack<BinaryTree.BTNode> ttStack = new Stack<>(); + ttStack.push(t.root); + + LinkedList.LLNode pretemp = h; + BinaryTree.BTNode cur = null; + LinkedList.LLNode temp = null; + + while (!ttStack.isEmpty()) { + cur = ttStack.pop(); + temp = new LinkedList.LLNode(cur.value); + LinkedList.NewLink(pretemp, temp); + pretemp = temp; + if (cur.rightChild != null) { + ttStack.push(cur.rightChild); + } + if (cur.leftChild != null) { + ttStack.push(cur.leftChild); + } + } + + return target; + } + + public static void main(String[] args) + { + BinaryTree ninefour = new BinaryTree(); + BinaryTree.BTNode first = new BinaryTree.BTNode(1); + BinaryTree.BTNode second = new BinaryTree.BTNode(2); + BinaryTree.BTNode third = new BinaryTree.BTNode(3); + BinaryTree.BTNode fourth = new BinaryTree.BTNode(4); + BinaryTree.BTNode fifth = new BinaryTree.BTNode(5); + BinaryTree.BTNode sixth = new BinaryTree.BTNode(6); + BinaryTree.BTNode seventh = new BinaryTree.BTNode(7); + BinaryTree.setLeftChild(first, second); + BinaryTree.setRightChild(first, third); + BinaryTree.setLeftChild(second, fourth); + BinaryTree.setRightChild(second, fifth); + BinaryTree.setLeftChild(fifth, sixth); + BinaryTree.setRightChild(fifth, seventh); + + ninefour.setRoot(first); + LinkedList ans = traversal(ninefour); + LinkedList.printLL_without_h(ans); + } +} diff --git a/challenge-094/cheok-yin-fung/java/BinaryTree.java b/challenge-094/cheok-yin-fung/java/BinaryTree.java new file mode 100644 index 0000000000..5bc31f3c8c --- /dev/null +++ b/challenge-094/cheok-yin-fung/java/BinaryTree.java @@ -0,0 +1,46 @@ +// The Weekly Challenge #094 +// ref: https://codereview.stackexchange.com/ +// questions/37944/constructing-a-binary-tree-in-java + + +import java.util.Scanner; + +public class BinaryTree +{ + public static BTNode root; + + public static class BTNode + { + BTNode leftChild; + BTNode rightChild; + int value; + + BTNode(int d) + { + value = d; + leftChild = null; + rightChild = null; + } + } + + public static void setLeftChild (BTNode p, BTNode kid) + { + p.leftChild = kid; + } + public static void setRightChild (BTNode p, BTNode kid) + { + p.rightChild = kid; + } + + public static void setRoot (BTNode n) + { + root = n; + } + + public int getValue(BTNode n) + { + return n.value; + } + + +} diff --git a/challenge-094/cheok-yin-fung/java/LinkedList.java b/challenge-094/cheok-yin-fung/java/LinkedList.java new file mode 100644 index 0000000000..0b58aaef6f --- /dev/null +++ b/challenge-094/cheok-yin-fung/java/LinkedList.java @@ -0,0 +1,58 @@ +// The Weekly Challenge #094 +// ref: https://www.geeksforgeeks.org/ +// implementing-a-linked-list-in-java-using-class/ +import java.util.Scanner; + +public class LinkedList +{ + static LLNode head; + + static class LLNode + { + LLNode next; + int value; + + LLNode(int d) + { + value = d; + next = null; + } + } + + public static void NewLink ( LLNode front, LLNode back) + { + front.next = back; + } + + public static void printLL(LinkedList list) + { + LLNode node = list.head; + while (node.next != null) { + System.out.print(node.value + " -> "); + node = node.next; + } + System.out.println(node.value); //final value + } + + public static void printLL_without_h(LinkedList list) + { + LLNode node = list.head; + while (node.next != null) { + node = node.next; + System.out.print(node.value + " -> "); + } + System.out.println("end"); + } + + public static void setHead (LLNode n) + { + head = n; + } + + public int getValue(LLNode n) + { + return n.value; + } + + +} diff --git a/challenge-094/cheok-yin-fung/perl/ch-1.pl b/challenge-094/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..6bc9e76905 --- /dev/null +++ b/challenge-094/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl +# The Weekly Challenge 094 +# Task 1 Group Anagrams +# Usage: ch-1.pl [@S] +use strict; +use warnings; +use utf8; +use open ':std', ':encoding(UTF-8)'; +# unicode info: +# 0. http://blogs.perl.org/users/c_y_fung/ +# 2021/01/from-a-reflection-on-the-weekly-challenge-092-task-1.html +# 1. https://stackoverflow.com/questions/15210532/use-of-use- +# utf8-gives-me-wide-character-in-print +# 2. https://www.reddit.com/r/perl/comments/koahhf/ +# from_a_reflection_on_the_weekly_challenge_092/ + + +my %hash_num; + +sub collect_alphabets { + my @words = @_; + my @alphabets = split //, join("" , @words); + my $i = 0; + for (@alphabets) { + if (!exists $hash_num{$_}) { + $hash_num{$_} = $i++; + } + } +} + +sub learn_atoms { + my $word = $_[0]; + my @alphabets = split //, $word; + my @coord = map {$hash_num{$_}} @alphabets; + @coord = sort @coord; + return join ",", @coord; +} + +sub compare_two_words { # a testing function + return learn_atoms($_[0]) eq learn_atoms($_[1]) ? 1 : 0; +} + +collect_alphabets(@ARGV); + +my %hash_compounds; +my @arr; +my $k = 0; + +for my $w (@ARGV) { + my $l = learn_atoms($w); + if (!exists $hash_compounds{$l}) { + $hash_compounds{$l} = $k; + $arr[$k] = [$w]; + $k++; + } + else { + push @{$arr[ $hash_compounds{$l} ]}, $w; + } +} + +$k--; + +for my $j (0..$k) { + print "(\""; + print join "\",\"", @{$arr[$j]}; + print "\")\n"; +} + diff --git a/challenge-094/cheok-yin-fung/perl/ch-2.pl b/challenge-094/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..bf86a5f015 --- /dev/null +++ b/challenge-094/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,153 @@ +#!/usr/bin/perl +# The Weekly Challenge 094 +# Task 2: Binary Tree to Linked List +use strict; +use warnings; +use Data::Dumper; + +{ +use strict; +package SLL::Node; + +sub new { + my ($class) = @_; + bless { + _value => $_[1], + _nextnode => $_[2], + }, $class; +} + + +sub value { $_[0]->{_value} } + +sub nextnode { $_[0]->{_nextnode} } + +sub set_nextnode { $_[0]->{_nextnode} = $_[1]; } + +sub print_linked_list { + my $node = $_[0]; + while (defined $node->nextnode) { + print $node->value, " -> "; + $node = $node->nextnode; + } + print $node->value; #final value + print "\n"; +} + + +} # END of package SLL::Node + + +{ +use strict; +package BinaryTreeNode; + +our @ISA = qw/ SLL::Node /; + +sub new { + my ($class) = @_; + bless { + _value => $_[1], + _leftchild => $_[2], + _rightchild => $_[3], + }, $class; +} + +sub value { $_[0]->{_value} } + +sub leftchild { $_[0]->{_leftchild} } + +sub rightchild { $_[0]->{_rightchild} } + +sub create_tree_from_list { + my ($class,@arr) = @_; + my $lastleaf = $#arr; + my @tree; + + for my $k (reverse 0..$lastleaf) { + if (defined($arr[$k])) { + if (defined($arr[$k*2+1]) and defined($arr[$k*2+2])) { + $tree[$k] = BinaryTreeNode->new( + $arr[$k], \$tree[$k*2+1], \$tree[$k*2+2]) + } + if (defined($arr[$k*2+1]) and !defined($arr[$k*2+2])) { + $tree[$k] = BinaryTreeNode->new( + $arr[$k], \$tree[$k*2+1], undef) + } + if (!defined($arr[$k*2+1]) and defined($arr[$k*2+2])) { + $tree[$k] = BinaryTreeNode->new( + $arr[$k], undef, \$tree[$k*2+2]) + } + if (!defined($arr[$k*2+1]) and !defined($arr[$k*2+2])) { + $tree[$k] = BinaryTreeNode->new( + $arr[$k], undef, undef) + } + } + + } + + return $class, $tree[0]; #return tree root +} + +my @tt_stack = (); + +sub to_llnode { + return SLL::Node->new(shift, undef); +} + +sub tree_travel { + my $self = shift; + my $t = shift; + my $baby = to_llnode($t->value); + my $preserve = \$baby; + + if (defined $t->leftchild) { + push @tt_stack, $t->rightchild if defined $t->rightchild ; + $baby->set_nextnode(tree_travel($self,${$t->leftchild})); + } elsif (defined $t->rightchild) { + $baby->set_nextnode(tree_travel($self,${$t->rightchild})); + + } elsif (scalar @tt_stack != 0) { + my $n = pop @tt_stack; + $baby->set_nextnode(tree_travel($self,${$n})); + } + + return $$preserve; +} + + +} #END of package BinaryTreeNode + + + +my $root = BinaryTreeNode-> + create_tree_from_list(1,2,3,4,5, undef, + undef, undef, undef,6,7); # Example + +my $rootnode = BinaryTreeNode->tree_travel($root); + +print "Example\n"; +$rootnode->print_linked_list; + +print "\n"; + + + + +print "A Test Case\n"; +=pod + 1 + |---| + / \ + 2 3 + / \ / \ + 4 5 6 7 + / \ /\ + 8 9 10 11 +=cut + +my $treeroot2 = BinaryTreeNode-> + create_tree_from_list(1, 2, 3, 4, 5, 6,7,8,9,10,11); # test case + +my $rootnode2 = BinaryTreeNode->tree_travel($treeroot2); +$rootnode2->print_linked_list; |
