aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-11 21:00:56 +0100
committerGitHub <noreply@github.com>2021-08-11 21:00:56 +0100
commit5552835c39c5d42689deb0ac0d41542660ffd2b3 (patch)
tree478320bbf5c2c95dd6976ffce512c4fa00390b29
parent5c01e5e636203aa19e363b240e9d224b621571b4 (diff)
parent835a0142c6c6c9ae6988c9417f6de38f9cd84df1 (diff)
downloadperlweeklychallenge-club-5552835c39c5d42689deb0ac0d41542660ffd2b3.tar.gz
perlweeklychallenge-club-5552835c39c5d42689deb0ac0d41542660ffd2b3.tar.bz2
perlweeklychallenge-club-5552835c39c5d42689deb0ac0d41542660ffd2b3.zip
Merge pull request #4686 from drbaggy/master
first challenge - will need to dig out binary tree code to get diameter
-rw-r--r--challenge-125/james-smith/README.md239
-rw-r--r--challenge-125/james-smith/perl/BinaryTree.pm165
-rw-r--r--challenge-125/james-smith/perl/ch-1.pl48
-rw-r--r--challenge-125/james-smith/perl/ch-2.pl120
4 files changed, 358 insertions, 214 deletions
diff --git a/challenge-125/james-smith/README.md b/challenge-125/james-smith/README.md
index a639f2ace2..b363e138a8 100644
--- a/challenge-125/james-smith/README.md
+++ b/challenge-125/james-smith/README.md
@@ -1,4 +1,4 @@
-# Perl Weekly Challenge #124
+# Perl Weekly Challenge #125
You can find more information about this weeks, and previous weeks challenges at:
@@ -10,233 +10,44 @@ submit solutions in whichever language you feel comfortable with.
You can find the solutions here on github at:
-https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-124/james-smith/perl
+https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-125/james-smith/perl
-# Task 1 - Happy Women's Day
+# Task 1 - Pythagorean Triples
-***Write a script to print the Venus Symbol, international gender symbol for women. Please feel free to use any character.***
+***You are given a positive integer `$N`. Write a script to print all Pythagorean Triples containing `$N` as a member. Print `-1` if it can’t be a member of any. Triples with the same set of elements are considered the same, i.e. if your script has already printed (3, 4, 5), (4, 3, 5) should not be printed.***
## The solution
-We will first look at the symbol defined in the question...
+# Task 2 - Binary Tree Diameter
-```
- ^^^^^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^^^^^
- ^
- ^
- ^
- ^^^^^
- ^
- ^
-```
-
-We note there are 3 types of rows:
-
- * Type I: a line of 5 symbols (centered)
- * Type II: a single symbol in the middle of the row
- * Type III: two symbols either side of the middle at a given distance.
-
-We encode these in an array -1 -> line of 5 symbols; 0 -> a single symbol at the centre; values > 0 - two points at the given distance away from the centre. The code becomes this:
-
-```perl
-my @pts = qw(-1 3 4 5 5 5 5 5 4 3 -1 0 0 0 -1 0 0);
-say $_ < 0 ? ' ^^^^^'
- : !$_ ? ' ^'
- : ' ' x (6-$_) . '^' . ' 'x($_*2-1) .'^'
- foreach @pts;
-```
-
-### Now for a more generic solution!
-
-This symbol is just a circle and cross below. We can use trig to work out the points of the circle. To ensure we don't leave gaps we sweep the arcs away from the cardinal points (N,S,E,W) up to the ordinal points (NE,NW,SE,SW) - 8 different 45deg arcs. This way we just need to compute one point for each line and then compute the other co-ordinate using pythagorus' theorem.
-
-Why do we do this? If we just did 4 arcs of 90 degrees we would find that once we passed 45 degrees we would miss out points...
-
-Our process has 4 steps.
-
- 1. Create a blank canvas
- 2. Draw the circle (note when we compute the y value we take half off the radius - this gives a better circle as we are tracing a line through the centre of the "squares"...
- 3. Draw the cross
- 4. Display the canvas...
-
-```perl
-## Create the canvas..
-my @a = map { ' ' x ($radius*2+1) } 0..$radius*2+$cross;
-
-## Now we draw the circle...
-foreach my $x (0 .. ceil($radius*0.71)) {
- my $y = int sqrt( ($radius-.5)**2 - $x**2 );
- substr $a[ $radius - $x ],$radius-$y,1,'^';
- substr $a[ $radius + $x ],$radius-$y,1,'^';
- substr $a[ $radius - $x ],$radius+$y,1,'^';
- substr $a[ $radius + $x ],$radius+$y,1,'^';
- substr $a[ $radius - $y ],$radius-$x,1,'^';
- substr $a[ $radius + $y ],$radius-$x,1,'^';
- substr $a[ $radius - $y ],$radius+$x,1,'^';
- substr $a[ $radius + $y ],$radius+$x,1,'^';
-}
-
-## And the two parts of the cross...
-substr $a[2*$radius+$_],$radius,1,'^' foreach 0..$cross;
-substr $a[2*$radius+$cross/2],$radius-$cross/2,$cross+1,'^'x($cross+1);
-
-### Finally we render the canvas...
-say $_ foreach @a;
-```
-
-Example output...
-```
- ^^^^^^^^^
- ^^^ ^^^
- ^^ ^^
- ^^ ^^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^ ^
- ^^ ^^
- ^^ ^^
- ^^^ ^^^
- ^^^^^^^^^
- ^
- ^
- ^
- ^
- ^
- ^
- ^^^^^^^^^^^^^
- ^
- ^
- ^
- ^
- ^
- ^
-```
-
-## Alternative languages
-
-As this was a script to generate an image - why not go back to learning languages after we had looked at CESIL, and visit the learning language of the 70s & 80s - LOGO. A graphic language where you drive a "turtle" around the screen.
-
-```logo
-setpensize 4
-pendown
-
-;cross
-back 300
-forward 150
-left 90
-forward 150
-back 300
-forward 150
-right 90
-forward 150
-
-;circle
-right 89
-repeat 180 [
- forward 10
- left 2
-]
-
-penup
-```
-
-You may ask why we only rotate right 89 to start the circle. If we start by rotating right then the circle will be off-set by "5" units to the right - we either have to do `right 90` `forward 5` `left 2` then `repeat` for `179`. And finish with another `right 5`. (This means the centre of one of the sides is at the top of the cross - or we can rotate the shape by 1 degree and have it stand on one of it's points)
-
-# Task 2 - Tug of War
-
-***You are given a set of `$n` integers `(n1, n2, n3, ...)`. Write a script to divide the set in two subsets of `n/2` sizes each so that the difference of the sum of two subsets is the least. If `$n` is even then each subset must be of size `$n/2` each. In case `$n` is odd then one subset must be `($n-1)/2` and other must be `($n+1)/2`.***
+*** Write a script to find the diameter of the given binary tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. It doesn’t have to pass through the root.***
## Solution
-We will use an iterative solution. We start by allocating person 1 to team 1, we then iterate down allocating each person to either team 1 or team 2. If either team gets too big we bomb out (this makes this solution more efficient than the non-iterative solution). As we go we keep a tally of the difference between the two teams weights.
-
-As we do a pre-allocation stage - we need to split the routine into two functions, the first function preps the data for interation and then handles the data at the end. The second does the interative step.
+For any node - we can compute the longest tree which goes through the node itself - this is the sum of the maximum lengths of the left tree and the depth of the right. We do know that there will be trees for which this is not the diameter - there could be another node for which the left and right depths sum to a larger value...
-At each step we need to know:
- 1) What is the max-size of the group;
- 2) Who is in team 1;
- 3) Who is in team 2;
- 4) What the difference in weight is;
- 5) What is the smallest difference we have found;
- 6) The weights of people left to be allocated.
+So to compute the diameter of the tree we just choose the maximum value of the maximum lengths of the left/right sub tree.
-So to start - we allocate person 1 to group 1, and set the difference to his weight. `$best` is an object to collect the information about the best allocation (the members of the two teams and the smallest difference)...
+We will re-use the BinaryTree module from a previous challenge and so need to define walk functions to work out the maximum length of a subtree and consequently diameter...
```perl
-sub match_teams {
- my( $diff, @n ) = @_;
- separate( 1 + int(@n/2), [$diff], [], $diff, my $best = [1e300], @n );
- return "Team 1: [@{$best->[1]}]; Team 2: [@{$best->[2]}]; difference $best->[0]";
+sub max_length {
+ my $self = shift;
+ my $d = 0;
+ $d = $self->left->max_length if $self->has_left;
+ return 1+$d unless $self->has_right;
+ my $t = $self->right->max_length;
+ return $t > $d ? 1+$t : 1+$d;
}
-sub separate {
- my($maxsize,$team1,$team2,$diff,$be,@nums) = @_;
- unless(@nums) {
- @{$be} = ($team1, $team2, abs $diff) if $be->[0]>abs $diff;
- return;
- }
- my $next = shift @nums;
- separate( $maxsize, [@{$team1},$next], $team2, $diff+$next, $be, @nums ) if @{$team1} < $maxsize;
- separate( $maxsize, $team1, [@{$team2},$next], $diff-$next, $be, @nums ) if @{$team2} < $maxsize;
+sub diameter {
+ my $self = shift;
+ my $global = { 'diameter' => 0 };
+ $self->walk( sub {
+ my $d = ($_[0]->has_left ? $_[0]->left->max_length : 0 ) +
+ ($_[0]->has_right ? $_[0]->right->max_length : 0 );
+ $_[1]{'diameter'} = $d if $d > $_[1]->{'diameter'};
+ }, $global );
+ return $global->{'diameter'};
}
```
-### Notes:
- * Notice the yoda inequality `$be->[0]>abs $diff` - it makes it clearer that you are only computing the absolute value of `$diff` not that of `$diff < $be->[0]`.
- * `$team1` / `$team2` are refs - so when we update them we make copies `[@{$team2},$next]` rather than pushing to them.
- * We keep the running total as it avoids the need to do the sum each time.
-
-### Timings
-
-| players | rate/time |
-| ------- | --------: |
-| 10 | 2,273/s |
-| 12 | 598/s |
-| 14 | 157/s |
-| 16 | 41/s |
-| 18 | 10/s |
-| 20 | 2.68/s |
-| 22 | 0.57/s |
-| 24 | ~ 6s |
-| 26 | ~ 23s |
-| 28 | ~ 94s |
-| 30 | ~ 365s |
-
-```
diff --git a/challenge-125/james-smith/perl/BinaryTree.pm b/challenge-125/james-smith/perl/BinaryTree.pm
new file mode 100644
index 0000000000..05703c9b26
--- /dev/null
+++ b/challenge-125/james-smith/perl/BinaryTree.pm
@@ -0,0 +1,165 @@
+package BinaryTree;
+
+use strict;
+use warnings;
+use Data::Dumper qw(Dumper);
+use feature qw(say);
+
+## The tree is stored in an array ref
+# The first element is the value of the node
+# The remainder of the array are child sub-trees
+#
+# Methods:
+# ->add_child( $child_tree )
+# ->flatten -- flatten list to array.
+#
+
+sub new {
+ my $class = shift;
+ my $value = shift;
+ my $self = [ $value, undef, undef ];
+ bless $self, $class;
+}
+
+sub depth {
+ my $self = shift;
+ my $d = 0;
+ $d = $self->left->depth if $self->has_left;
+ return 1+$d unless $self->has_right;
+ my $t = $self->right->depth;
+ return $t > $d ? 1+$t : 1+$d;
+}
+
+sub diameter {
+ my $self = shift;
+ my $global = { 'diameter' => 0 };
+ $self->walk( sub {
+ my $d = ($_[0]->has_left ? $_[0]->left->depth : 0 ) +
+ ($_[0]->has_right ? $_[0]->right->depth : 0 );
+ $_[1]{'diameter'} = $d if $d > $_[1]->{'diameter'};
+ }, $global );
+ return $global->{'diameter'};
+}
+
+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;
+ return $self;
+}
+
+sub add_child_right {
+ my( $self,$child ) = @_;
+ $self->[2] = $child;
+ return $self;
+}
+
+## Define walk method....
+sub walk {
+ my $self = shift;
+ $self->walk_pre( @_ );
+ return;
+}
+
+##
+## Pre-order walk process node then the left and right sub-trees
+##
+
+sub walk_pre {
+ my( $self, $fn, $global, $local, $dir ) = @_;
+ $local = $fn->( $self, $global, $local, $dir||'' );
+ $self->left->walk_pre( $fn, $global, $local, 'left' ) if $self->has_left;
+ $self->right->walk_pre( $fn, $global, $local, 'right' ) if $self->has_right;
+ return;
+}
+
+##
+## In-order walk process left sub-tree, then the node and finally the right sub-tree
+##
+
+sub walk_in {
+ my( $self, $fn, $global, $local, $dir ) = @_;
+ $self->left->walk_in( $fn, $global, $local, 'left' ) if $self->has_left;
+ $local = $fn->( $self, $global, $local, $dir||'' );
+ $self->right->walk_in( $fn, $global, $local, 'right' ) if $self->has_right;
+ return;
+}
+
+##
+## Reverse-order walk process right sub-tree, then the node and finally the left sub-tree
+##
+
+sub walk_reverse {
+ my( $self, $fn, $global, $local, $dir ) = @_;
+ $self->right->walk_reverse( $fn, $global, $local, 'right' ) if $self->has_right;
+ $local = $fn->( $self, $global, $local, $dir||'' );
+ $self->left->walk_reverse( $fn, $global, $local, 'left' ) if $self->has_left;
+ return;
+}
+
+##
+## Post-order walk the left and right subtrees before processing the node...
+##
+
+sub walk_post {
+ my( $self, $fn, $global, $local, $dir ) = @_;
+ $self->left->walk_post( $fn, $global, $local, 'left' ) if $self->has_left;
+ $self->right->walk_post( $fn, $global, $local, 'right' ) if $self->has_right;
+ $local = $fn->( $self, $global, $local, $dir||'' );
+ return;
+}
+
+sub flatten {
+ my( $self,$dump_fn, $method ) = @_;
+ $dump_fn ||= sub { $_[0] };
+ $method = $self->can( 'walk_'.($method||'pre') ) || 'walk';
+ my $arrayref = [];
+ $self->$method( sub {
+ my($node,$global) = @_;
+ push @{$global}, $dump_fn->( $node->value );
+ }, $arrayref );
+ return @{$arrayref};
+}
+
+sub dump {
+ my( $self, $dump_fn ) = @_;
+ $dump_fn ||= sub { $_[0] };
+ $self->walk( sub {
+ my( $node, $global, $local, $dir ) = @_;
+ say join '', $local||'', $dir eq 'left' ? '<' : $dir eq 'right' ? '>' : ' ', ' ', $dump_fn->($node->value);
+ return $local .= ' ';
+ }, {}, '', '' );
+ return;
+}
+
+1;
diff --git a/challenge-125/james-smith/perl/ch-1.pl b/challenge-125/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..c0bf708d10
--- /dev/null
+++ b/challenge-125/james-smith/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use Benchmark qw(cmpthese timethis);
+use Data::Dumper qw(Dumper);
+
+my @TESTS = (
+ [ 0, 1 ],
+);
+
+say $_,' > ', get_triples($_) foreach 1..500;
+
+sub get_triples {
+ my $n = shift;
+ return $n < 3 ? -1 : join '; ', map { sprintf '(%s)', join ', ', @{$_} }
+ (
+ grep { $_->[1] == int $_->[1] } ## Check if all int
+ map { [ $_, sqrt($n**2-$_**2), $n ] } ## Generate triple
+ 3 .. sqrt($n**2/2) ## Shortest side ($n is hypotenuse)
+ ),(
+ map { $_->[0]>$_->[1] ? [@{$_}[1,0,2]] : $_ } ## put in numerical order
+ grep { $_->[1] == int $_->[1] } ## Check all int
+ map { [ $n, sqrt($_**2-$n**2), $_ ] } ## Generate triple
+ ($n+1) .. ($n**2/2+1) ## Hypotenuse ($n is one of other two sides)
+ );
+}
+
+## Notes:
+
+# Except for $n < 3 there is always a solution
+# * If $n is odd then it can always be the short side of a triangle where the
+# other sides are ($n^2-1)/2 or ($n^2+1)/2
+# * If $n has an odd factor - then we can rewrite $n as $o * $m
+# we can use the same trick to get sides of $m($o^2-1)/2 & $m($o^2+1)/2
+# * This only leaves us with numbers of the form 2^n - but we know that 4 can be part of a Pyth.Triple
+# and so any number of the form 2^($n+2) in a triple of the form ( 3.2^$n, 4.2^$n, 5.2^$n ).
+#
+# * We have two cases where $n is the hypotenuse and where it is a shorter side.
+# * With the former we need to look at the shorter sides these can be 3 -> n-1
+# but to avoid dupes we limit our search to sqrt(n^2/2)...
+# * The latter is more complex to work out the range
+# But we note that the difference between two consecutive squares is ($m+1)^2-($m)^2 = 2m + 1
+# So the largest value for the hypotenuse is therefore (n^2+1)/2
+
diff --git a/challenge-125/james-smith/perl/ch-2.pl b/challenge-125/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..71d510b253
--- /dev/null
+++ b/challenge-125/james-smith/perl/ch-2.pl
@@ -0,0 +1,120 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+use lib '.';
+use BinaryTree;
+
+say '';
+
+## Node has both left and right trees - the diameter is 1 + depth of the two child trees.
+
+## 1 -> 2 -> 3
+## | `> 4
+## `> 5 -> 6
+## `> 7 -> 8 -> 9 [ depth 5 ]
+## `> 10
+## -------------------------------
+## 9 -> 8 -> 7 -> 5 -> 1 -> 2 -> 3 [ diameter 7 ]
+
+my $x = BinaryTree->new(1)->add_child_left(
+ BinaryTree->new(2)->add_child_left( BinaryTree->new(3) )->add_child_right( BinaryTree->new(4) )
+ )->add_child_right(
+ BinaryTree->new(5)->add_child_left( BinaryTree->new(6))->add_child_right(
+ BinaryTree->new(7)->add_child_left( BinaryTree->new(8)->add_child_left(BinaryTree->new(9)) )->add_child_right(BinaryTree->new(10))
+ ));
+say '1) Tree with root node having both left and right children!';
+$x->dump;
+say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter;
+say '';
+## No node has 2 children - the diameter is the depth...
+
+## 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 [ depth 7 ]
+## -------------------------------
+## 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 [ diameter 7 ]
+$x = BinaryTree->new(1)->add_child_left(
+ BinaryTree->new(2)->add_child_left(
+ BinaryTree->new(3)->add_child_right(
+ BinaryTree->new(4)->add_child_left(
+ BinaryTree->new(5)->add_child_right(
+ BinaryTree->new(6)->add_child_left(
+ BinaryTree->new(7)
+ ))))));
+
+say '2) Tree with no node having two children';
+$x->dump;
+say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter;
+say '';
+## We have a node with two children - but there is a sequence of nodes
+## leading up to this node which is longer than the depth of the child trees.
+## so diameter is just depth.
+
+## 1 -> 2 -> 3 -> 4 -> 5 -> 6 [ depth 6]
+## `> 7 -> 8
+## --------------------------
+## 6 -> 5 -> 4 -> 3 -> 2 -> 1 [ diameter 6 ]
+$x = BinaryTree->new(1)->add_child_left(
+ BinaryTree->new(2)->add_child_left(
+ BinaryTree->new(3)->add_child_left(
+ BinaryTree->new(4)->add_child_left(
+ BinaryTree->new(5)->add_child_left( BinaryTree->new(6) )
+ )->add_child_right(
+ BinaryTree->new(7)->add_child_left( BinaryTree->new(8) )
+ )
+ )));
+
+say '3) Tree with node further down having two children - but distance from';
+say ' root to node is greater than the depth of either child';
+$x->dump;
+say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter;
+say '';
+
+## 1 -> 2 -> 3 -> 4 -> 5 -> 6
+## `>10 `> 7 -> 8 -> 9
+## --------------------------
+## 9 -> 8 -> 7 -> 3 -> 4 -> 5 -> 6
+
+$x = BinaryTree->new(1)->add_child_left(
+ BinaryTree->new(2)->add_child_left(
+ BinaryTree->new(3)->add_child_left(
+ BinaryTree->new(4)->add_child_left(
+ BinaryTree->new(5)->add_child_left( BinaryTree->new(6) )
+ )
+ )->add_child_right(
+ BinaryTree->new(7)->add_child_left(
+ BinaryTree->new(8)->add_child_left( BinaryTree->new(9) )
+ )
+ )
+ )->add_child_right( BinaryTree->new(10))
+ );
+
+say '4) Tree with node further down having two children - but distance from';
+say ' root to node is greater than the depth of either child';
+$x->dump;
+say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter;
+say '';
+
+## This time both child trees have depths longer than the number of
+## ancestor nodes - so that is used to get the length...
+
+## 1 -> 2 -> 3 -> 4 [ depth 4]
+## `> 5 -> 6
+## ---------------------
+## 6 -> 5 -> 2 -> 3 -> 4 [ diameter 5 ]
+
+say '5) Tree with node further down having two children - but distance from';
+say ' root to node is less than the depth of both children';
+$x = BinaryTree->new(1)->add_child_left(
+ BinaryTree->new(2)->add_child_left(
+ BinaryTree->new(3)->add_child_left( BinaryTree->new(4) )
+ )->add_child_right(
+ BinaryTree->new(5)->add_child_left( BinaryTree->new(6) )
+ )
+ );
+
+$x->dump;
+say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter;
+say '';