aboutsummaryrefslogtreecommitdiff
path: root/challenge-113
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-05-19 12:59:31 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-05-19 12:59:31 +0100
commit7d19752068cca016bfa8417b3c22a2cece0ffa32 (patch)
tree2c930a29f5aaabd85da0036566f0afe4abee363b /challenge-113
parent434eda509d56beaebb3c0aa6c4aea3cc8d1f83c3 (diff)
downloadperlweeklychallenge-club-7d19752068cca016bfa8417b3c22a2cece0ffa32.tar.gz
perlweeklychallenge-club-7d19752068cca016bfa8417b3c22a2cece0ffa32.tar.bz2
perlweeklychallenge-club-7d19752068cca016bfa8417b3c22a2cece0ffa32.zip
added accessors + blog link
Diffstat (limited to 'challenge-113')
-rw-r--r--challenge-113/james-smith/blog.txt1
-rw-r--r--challenge-113/james-smith/perl/BinaryTree.pm44
-rw-r--r--challenge-113/james-smith/perl/ch-2.pl15
3 files changed, 48 insertions, 12 deletions
diff --git a/challenge-113/james-smith/blog.txt b/challenge-113/james-smith/blog.txt
new file mode 100644
index 0000000000..0b9747f718
--- /dev/null
+++ b/challenge-113/james-smith/blog.txt
@@ -0,0 +1 @@
+https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-113/james-smith
diff --git a/challenge-113/james-smith/perl/BinaryTree.pm b/challenge-113/james-smith/perl/BinaryTree.pm
index be0b5a88c8..e28307765a 100644
--- a/challenge-113/james-smith/perl/BinaryTree.pm
+++ b/challenge-113/james-smith/perl/BinaryTree.pm
@@ -21,6 +21,37 @@ sub new {
bless $self, $class;
}
+sub value {
+ my $self = shift;
+ return $self->[0];
+}
+
+sub left {
+ my $self = shift;
+ return $self->[1];
+}
+
+sub right {
+ my $self = shift;
+ return $self->[2];
+}
+
+sub has_left {
+ my $self = shift;
+ return defined $self->[1];
+}
+
+sub has_right {
+ my $self = shift;
+ return defined $self->[2];
+}
+
+sub update {
+ my( $self, $val ) = @_;
+ $self->[0] = $val;
+ return $self;
+}
+
sub add_child_left {
my( $self,$child ) = @_;
$self->[1] = $child;
@@ -35,10 +66,9 @@ sub add_child_right {
sub walk {
my( $self, $fn, $global, $local, $dir ) = @_;
- my ($v,$l,$r) = @{$self};
$local = $fn->( $self, $global, $local, $dir||'' );
- $l->walk( $fn, $global, $local, 'left' ) if defined $l;
- $r->walk( $fn, $global, $local, 'right' ) if defined $r;
+ $self->left->walk( $fn, $global, $local, 'left' ) if $self->has_left;
+ $self->right->walk( $fn, $global, $local, 'right' ) if $self->has_right;
return;
}
@@ -48,7 +78,7 @@ sub flatten {
my $arrayref = [];
$self->walk( sub {
my($node,$global) = @_;
- push @{$global}, $dump_fn->( $node->[0] );
+ push @{$global}, $dump_fn->( $node->value );
}, $arrayref );
return @{$arrayref};
}
@@ -58,7 +88,7 @@ sub dump {
$dump_fn ||= sub { $_[0] };
$self->walk( sub {
my( $node, $global, $local, $dir ) = @_;
- say join '', $local||'', $dir eq 'left' ? '<' : $dir eq 'right' ? '>' : ' ', ' ', $dump_fn->($node->[0]);
+ say join '', $local||'', $dir eq 'left' ? '<' : $dir eq 'right' ? '>' : ' ', ' ', $dump_fn->($node->value);
return $local .= ' ';
}, {}, '', '' );
return;
@@ -70,11 +100,11 @@ sub clone {
my $clone = {};
$self->walk( sub { my( $node, $global, $local, $dir ) = @_;
if(exists $global->{'tree'} ) {
- my $child = BinaryTree->new( $clone_fn->( $node->[0] ) );
+ my $child = BinaryTree->new( $clone_fn->( $node->value ) );
$dir eq 'left' ? $local->add_child_left( $child ) : $local->add_child_right( $child );
return $child;
}
- $global->{'tree'} = BinaryTree->new( $clone_fn->( $node->[0] ) );
+ $global->{'tree'} = BinaryTree->new( $clone_fn->( $node->value ) );
return $global->{'tree'};
}, $clone );
return $clone->{'tree'};
diff --git a/challenge-113/james-smith/perl/ch-2.pl b/challenge-113/james-smith/perl/ch-2.pl
index 53a05bf5d7..bb453db985 100644
--- a/challenge-113/james-smith/perl/ch-2.pl
+++ b/challenge-113/james-smith/perl/ch-2.pl
@@ -59,6 +59,11 @@ is( "@{[ $x->flatten ]}", '1 2 4 7 3 5 6' );
is( "@{[ $y->flatten ]}", '27 26 24 21 25 23 22' );
say '';
+## I've also implemented a true binary tree - the difference
+## is that we explicitly have a left and right node.
+## The "add_child" has been split into add_child_left &
+## add_child_right.
+
$x = BinaryTree->new(1)
->add_child_left(
BinaryTree->new(2)
@@ -90,20 +95,20 @@ added into the class implemented by walk
';
say 'Dump $x';
-$x->dump( sub { "[$_[0]]"; } );
+$x->dump;
say '';
say 'Clone $x as $y';
$y = $x->clone;
say 'Dump $y (clone of $x)';
-$y->dump( sub { "[$_[0]]"; } );
+$y->dump;
say '';
say 'Now get total value and adjust each node... for $y';
my $glob = { 'total' => 0 };
-$y->walk( sub { my( $node, $global ) = @_; $global->{'total'} += $node->[0]; }, $glob );
-$y->walk( sub { my( $node, $global ) = @_; $node->[0] = $global->{'total'} - $node->[0]; }, $glob );
+$y->walk( sub { my( $node, $global ) = @_; $global->{'total'} += $node->value; }, $glob );
+$y->walk( sub { my( $node, $global ) = @_; $node->update( $global->{'total'} - $node->value ); }, $glob );
say '';
say 'Dump $y (clone of $x)';
-$y->dump( sub { "[$_[0]]"; } );
+$y->dump;
say '';
say 'Running tests';