aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-04 22:33:00 +0000
committerGitHub <noreply@github.com>2021-01-04 22:33:00 +0000
commit4db96d264346783d2780a93b699c0b07399aa7cd (patch)
tree1e34c84d99e74462911cec00dbad9a27a82317e2
parentdc3a7a449f2fd43b361ff7a9871e5b3da89e28bc (diff)
parentf356163bd5fcb44ddf9b6e8989ab93f898858903 (diff)
downloadperlweeklychallenge-club-4db96d264346783d2780a93b699c0b07399aa7cd.tar.gz
perlweeklychallenge-club-4db96d264346783d2780a93b699c0b07399aa7cd.tar.bz2
perlweeklychallenge-club-4db96d264346783d2780a93b699c0b07399aa7cd.zip
Merge pull request #3155 from choroba/ech094
Add solutions to 094 by E. Choroba
-rwxr-xr-xchallenge-094/e-choroba/perl/ch-1.pl28
-rwxr-xr-xchallenge-094/e-choroba/perl/ch-2.pl57
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-094/e-choroba/perl/ch-1.pl b/challenge-094/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..38ad829fa2
--- /dev/null
+++ b/challenge-094/e-choroba/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub group_anagrams {
+ my @words = @_;
+ my %groups;
+ for my $word (@words) {
+ my %f;
+ ++$f{$_} for split //, $word;
+ push @{ $groups{ join '|', map "$_$f{$_}", sort keys %f } }, $word
+ }
+ return [ values %groups ]
+}
+
+use Test::More tests => 3;
+use Test::Deep;
+
+cmp_deeply group_anagrams(qw( opt bat saw tab pot top was )),
+ bag(bag(qw( bat tab )),
+ bag(qw( saw was )),
+ bag(qw( top pot opt ))), 'Example 1';
+
+cmp_deeply group_anagrams('x'), [['x']], 'Example 2';
+
+cmp_deeply group_anagrams(qw( pool loop lop poll plop pool )),
+ bag(bag(qw( pool loop pool )),
+ ['lop'], ['poll'], ['plop']), 'Duplicate letters and words';
diff --git a/challenge-094/e-choroba/perl/ch-2.pl b/challenge-094/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..1dc286bee3
--- /dev/null
+++ b/challenge-094/e-choroba/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+=head1 Binary Tree to Linked List
+
+As I understand it, the linked list should correspond to the
+depth-first traversal of the tree.
+
+Let's represent each node in a tree by an array reference where the
+elements are VALUE, FIRST CHILD, SECOND CHILD, only the first one is
+required. A tree is represented by its root.
+
+Let's represent each element of a linked list by an array reference
+where the elements are VALUE and NEXT. A linked list is represented by
+its first element.
+
+=cut
+
+use enum qw( VALUE FIRST_CHILD SECOND_CHILD NEXT=1 );
+
+sub bt2l {
+ my ($tree) = @_;
+ return unless defined $tree;
+
+ return $tree->[VALUE],
+ bt2l($tree->[FIRST_CHILD]),
+ bt2l($tree->[SECOND_CHILD])
+}
+
+sub bt2ll {
+ my ($tree) = @_;
+ my @list = bt2l($tree);
+ my $ll = my $element = [];
+ for my $i (0 .. $#list) {
+ $element->[VALUE] = $list[$i];
+ $element = $element->[NEXT] = [] unless $i == $#list;
+ }
+ return $ll
+}
+
+sub to_string {
+ my ($ll) = @_;
+ return unless $ll;
+
+ return join ' -> ', $ll->[VALUE], to_string($ll->[NEXT])
+}
+
+use Test::More tests => 2;
+is_deeply bt2ll([1, [2, [4], [5, [6], [7]]], [3]]),
+ [1, [2, [4, [5, [6, [7, [3]]]]]]],
+ 'Example 1';
+
+is to_string(bt2ll([1, [2, [4], [5, [6], [7]]], [3]])),
+ '1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3',
+ 'String representation';