diff options
| -rw-r--r-- | challenge-094/gugod/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-094/gugod/raku/ch-1.raku | 26 | ||||
| -rw-r--r-- | challenge-094/gugod/raku/ch-2.raku | 77 |
3 files changed, 104 insertions, 0 deletions
diff --git a/challenge-094/gugod/blog.txt b/challenge-094/gugod/blog.txt new file mode 100644 index 0000000000..954f9df0c2 --- /dev/null +++ b/challenge-094/gugod/blog.txt @@ -0,0 +1 @@ +https://gugod.org/2021/01/pwc-094-en/ diff --git a/challenge-094/gugod/raku/ch-1.raku b/challenge-094/gugod/raku/ch-1.raku new file mode 100644 index 0000000000..78b8938dd6 --- /dev/null +++ b/challenge-094/gugod/raku/ch-1.raku @@ -0,0 +1,26 @@ +sub MAIN { + my %g = group-anagrams( ['x'] ); + say %g.gist; + + %g = group-anagrams( ["opt", "bat", "saw", "tab", "pot", "top", "was"] ); + say %g.gist; +} + +sub group-anagrams (@S) { + my %groups; + for @S -> $s { + my $group = %groups.keys.first(-> $it { is-anagram($s, $it) }); + unless $group.defined { + $group = $s; + %groups{$group} = []; + } + %groups{$group}.push($s); + } + return %groups; +} + +sub is-anagram (Str $a, Str $b) { + return False unless $a.chars == $b.chars; + + return $a.comb.sort eqv $b.comb.sort +} diff --git a/challenge-094/gugod/raku/ch-2.raku b/challenge-094/gugod/raku/ch-2.raku new file mode 100644 index 0000000000..73ed60400d --- /dev/null +++ b/challenge-094/gugod/raku/ch-2.raku @@ -0,0 +1,77 @@ +class IntBinaryTree { + has Int $.payload; + has IntBinaryTree $.left-child; + has IntBinaryTree $.right-child; +} + +class IntLinkedList { + has Int $.payload; + has IntLinkedList $.next; + + method set-next (IntLinkedList $n) { + $!next = $n; + } +} + +sub MAIN { + my $tree = IntBinaryTree.new( + :payload(1), + :left-child( + IntBinaryTree.new( + :payload(2), + :left-child(IntBinaryTree.new(:payload(4))), + :right-child( + IntBinaryTree.new( + :payload(5), + :left-child(IntBinaryTree.new(:payload(6))), + :right-child(IntBinaryTree.new(:payload(7))), + ) + ) + ) + ), + :right-child( + IntBinaryTree.new(:payload(3)) + ) + ); + + my $x = binary-tree-to-linked-list($tree); + say preview($x); +} + +sub binary-tree-to-linked-list (IntBinaryTree $tree) { + my $head = IntLinkedList.new(:payload( $tree.payload )); + my $tail = $head; + + if $tree.left-child { + my $sub-list = binary-tree-to-linked-list($tree.left-child); + $tail.set-next: $sub-list; + + while $tail.next.defined { + $tail = $tail.next; + } + } + + if $tree.right-child { + my $sub-list = binary-tree-to-linked-list($tree.right-child); + $tail.set-next: $sub-list; + + while $tail.next.defined { + $tail = $tail.next; + } + } + + return $head; +} + +sub preview (IntLinkedList $s) { + my $it = $s; + + my $out = $it.payload; + $it = $it.next; + while $it.defined { + $out ~= "->" ~ $it.payload; + $it = $it.next; + } + return $out; +} + |
