diff options
| author | drbaggy <js5@sanger.ac.uk> | 2020-12-28 14:49:49 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2020-12-28 14:49:49 +0000 |
| commit | ae740a7740aeed36d5cb37e342bc0fc444d4beb8 (patch) | |
| tree | 2225a44f5d9497fcc75bc1d0a9ddaf2fa9d9a6ce | |
| parent | 92d840cde125c6c7556cc529cfa737104f32258e (diff) | |
| download | perlweeklychallenge-club-ae740a7740aeed36d5cb37e342bc0fc444d4beb8.tar.gz perlweeklychallenge-club-ae740a7740aeed36d5cb37e342bc0fc444d4beb8.tar.bz2 perlweeklychallenge-club-ae740a7740aeed36d5cb37e342bc0fc444d4beb8.zip | |
pushing solns to 93....
| -rw-r--r-- | challenge-093/james-smith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-093/james-smith/perl/ch-1-gcd.pl | 58 | ||||
| -rw-r--r-- | challenge-093/james-smith/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-093/james-smith/perl/ch-2.pl | 31 |
4 files changed, 130 insertions, 0 deletions
diff --git a/challenge-093/james-smith/blog.txt b/challenge-093/james-smith/blog.txt new file mode 100644 index 0000000000..632a590f10 --- /dev/null +++ b/challenge-093/james-smith/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/james_curtis-smith/2020/12/perl-weekly-challenge-93.html diff --git a/challenge-093/james-smith/perl/ch-1-gcd.pl b/challenge-093/james-smith/perl/ch-1-gcd.pl new file mode 100644 index 0000000000..632f185f5a --- /dev/null +++ b/challenge-093/james-smith/perl/ch-1-gcd.pl @@ -0,0 +1,58 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +## We represent the tree as a nested array. +## Each "tree" is an arrayref with up to 3 values +## * value of node {scalar} +## * left sub-tree {arrayref} +## * right sub-tree {arrayref} +## note we don't have a right sub-tree without a left subtree. +is( most_points_in_line( [1,1],[2,2],[3,3] ), 3 ); +is( most_points_in_line( [1,1],[2,2],[3,1],[1,3],[5,3] ), 3 ); + +done_testing( ); + +use Data::Dumper qw(Dumper); + +sub most_points_in_line { + my @nodes = @_; + my %lines; + foreach my $i (0..(@nodes-2)) { + foreach my $j (($i+1)..(@nodes-1)) { + my $dir = '-'; + my( $dx,$dy) = ( $nodes[$i][0]-$nodes[$j][0], $nodes[$i][1]-$nodes[$j][1] ); + if( $dx && $dy ) { + my $gcd = gcd( $dx,$dy ); + $dir = $dx/$gcd.'-'.$dy/$gcd; + } else { + $dir = $dx ? '-' : '|'; + } + $lines{$i.':'.$dir}++; + $lines{$j.':'.$dir}++; + } + } + my $max = 0; + foreach (values %lines) { + $max = $_ if $_ > $max; + } + return $max+1; +} + + +sub gcd { + my( $n,$m,$s ) = (@_,1); + ($n,$m) = (-$n,-$m) if $n < 0; + if( $m < 0 ) { + $s = -1; + $m = -$m; + } + ($n,$m) = ($m,$n) if $m>$n; + ($n,$m) = ( $m, $n % $m ) while $n % $m; + return $m*$s; +} + diff --git a/challenge-093/james-smith/perl/ch-1.pl b/challenge-093/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..0db2bbc574 --- /dev/null +++ b/challenge-093/james-smith/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +## We represent the tree as a nested array. +## Each "tree" is an arrayref with up to 3 values +## * value of node {scalar} +## * left sub-tree {arrayref} +## * right sub-tree {arrayref} +## note we don't have a right sub-tree without a left subtree. +is( most_points_in_line( [1,1],[2,2],[3,3] ), 3 ); +is( most_points_in_line( [1,1],[2,2],[3,1],[1,3],[5,3] ), 3 ); + +done_testing( ); + +use Data::Dumper qw(Dumper); + +sub most_points_in_line { + my @nodes = @_; + my %lines; + foreach my $i (0..(@nodes-2)) { + foreach my $j (($i+1)..(@nodes-1)) { + my $dir = $nodes[$i][1] == $nodes[$j][1] + ? '-' + : ($nodes[$i][0]-$nodes[$j][0])/($nodes[$i][1]-$nodes[$j][1]); + $lines{$i.':'.$dir}++; + $lines{$j.':'.$dir}++; + } + } + my $max = 0; + foreach (values %lines) { + $max = $_ if $_ > $max; + } + return $max+1; +} + diff --git a/challenge-093/james-smith/perl/ch-2.pl b/challenge-093/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..5f50e6bec8 --- /dev/null +++ b/challenge-093/james-smith/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +## We represent the tree as a nested array. +## Each "tree" is an arrayref with up to 3 values +## * value of node {scalar} +## * left sub-tree {arrayref} +## * right sub-tree {arrayref} +## note we don't have a right sub-tree without a left subtree. +is( tree_sum( [1,[2,[3],[4]]] ), 13 ); +is( tree_sum( [1,[2,[4]],[3,[5],[6]]] ), 26 ); + +done_testing( ); + +use Data::Dumper qw(Dumper); + +sub tree_sum { + my ( $node,$sum ) = @_; + $sum||=0; + return $node->[0] + $sum if @{$node} < 2; ## We have a leaf so return the sum... + ## We have a branch - so return the sum foreach branch. for reach branch we need to add + ## the current value to the sum from ancestors... + return tree_sum( $node->[1], $node->[0] + $sum ) + + ( @{$node} == 3 ? tree_sum( $node->[2], $node->[0] + $sum ) : 0 ); +} + |
