diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-01-09 21:04:58 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-01-09 21:14:38 +0100 |
| commit | d8976104368f4b8fe82f75f29df5ad01287023c5 (patch) | |
| tree | 1e991dc464cc88777b0e5e2ccc89c103bad81033 /challenge-093 | |
| parent | 6c74e8e8d20d4c91b4218f337bde823ec06a175f (diff) | |
| download | perlweeklychallenge-club-d8976104368f4b8fe82f75f29df5ad01287023c5.tar.gz perlweeklychallenge-club-d8976104368f4b8fe82f75f29df5ad01287023c5.tar.bz2 perlweeklychallenge-club-d8976104368f4b8fe82f75f29df5ad01287023c5.zip | |
Command line handling for task 2
Diffstat (limited to 'challenge-093')
| -rwxr-xr-x | challenge-093/jo-37/perl/ch-1.pl | 11 | ||||
| -rwxr-xr-x[-rw-r--r--] | challenge-093/jo-37/perl/ch-2.pl | 247 |
2 files changed, 159 insertions, 99 deletions
diff --git a/challenge-093/jo-37/perl/ch-1.pl b/challenge-093/jo-37/perl/ch-1.pl index 36d0294de8..b4d603aa6e 100755 --- a/challenge-093/jo-37/perl/ch-1.pl +++ b/challenge-093/jo-37/perl/ch-1.pl @@ -44,11 +44,6 @@ point ... EOS -# convert points given as x,y,... to a list of array references -sub get_points { - map {[split ',']} @_ -} - # convert @ARGV to point list my @points = get_points(@ARGV); say 'points: ', pp @points if $verbose; @@ -56,6 +51,10 @@ say 'points: ', pp @points if $verbose; # Process input data say scalar max_points_in_line(@points); +# convert points given as x,y,... to a list of array references +sub get_points { + map {[split ',']} @_ +} # Get the "canonical direction" between two integral points: # - nonzero components have no common divisor > 1 @@ -131,8 +130,6 @@ sub max_points_in_line { @points_in_line; } -### main ### - sub run_tests { SKIP: { skip "self test", 5 unless $selftest; diff --git a/challenge-093/jo-37/perl/ch-2.pl b/challenge-093/jo-37/perl/ch-2.pl index 69e9bc34be..044fcaf9e1 100644..100755 --- a/challenge-093/jo-37/perl/ch-2.pl +++ b/challenge-093/jo-37/perl/ch-2.pl @@ -1,10 +1,57 @@ -#!/usr/bin/perl +#!/usr/bin/perl -s use v5.16; use Test2::V0; use experimental qw(signatures postderef); - -$::verbose = 1; +use Data::Dump qw(dd pp); + +our ($verbose, $examples, $tests); + +run_tests() if $examples || $tests; + +say(<<EOS), exit unless @ARGV; +Usage: $0 [-examples] [-tests] [-verbose] [node ...] + +-examples + run examples from challenge + +-tests + run tests + +-verbose + enable trace output + +node ... + build binary tree from given nodes. Each node has the form: + id:value[:left[:right]] + where + id is a unique node identifier + value is the node's value + left is the id of the left child node (may be missing or empty) + right is the id of the right child node (may be missing) + The nodes may be specified in any order, but the root node must have + an id of 'ROOT'. The given example 1 could be written as + ROOT:1:n2 n2:2:n3:n4 n3:3 n4:4 +EOS + +my $nodes = capture_nodes(@ARGV); +dd $nodes if $verbose; + +my $tree = [undef, undef, 'ROOT']; +build_tree($tree, $nodes); +dd $tree if $verbose; + +say sum_tree($tree); + +# Backported from challenge 094 +sub capture_nodes { + # capture nodes + my %nodes = map { + my ($key, %val); ($key, @val{qw(val left right)}) = split /:/; + ($key => \%val) + } @_; + \%nodes; +} # Simplistic binary tree implementation: # Each node $n is an array ref with @@ -12,6 +59,16 @@ $::verbose = 1; # - $n->[1] pointing to the right sub tree # - $n->[2] holding the node data +# Backported from challenge 094 +sub build_tree ($node, $nodes) { + my %node = $nodes->{$node->[2]}->%*; + $node->[0] = [undef, undef, $node{left}] if $node{left}; + $node->[1] = [undef, undef, $node{right}] if $node{right}; + $node->[2] = $node{val}; + build_tree($node->[0], $nodes) if $node->[0]; + build_tree($node->[1], $nodes) if $node->[1]; +} + # Sum node values over the paths from the start node to every leaf node. # The total should be modified at leaf nodes only, therefore it is # passed as a reference. $path holds the sum of the node values in the @@ -45,92 +102,98 @@ sub sum_tree ($root) { $total; } -### main ### - -pass 'init'; - -# 1 -# / -# 2 -# / \ -# 3 4 - -is sum_tree( - [ - [ - [undef, undef, 3], - [undef, undef, 4], - 2], - undef, - 1]), - 13, 'Example 1'; - -# 1 -# / \ -# 2 3 -# / / \ -# 4 5 6 - -is sum_tree( - [ - [ - [undef, undef, 4], - undef, - 2], - [ - [undef, undef, 5], - [undef, undef, 6], - 3], - 1]), - 26, 'Example 2'; - -# 1 -# / \ -# / \ -# 2 3 -# / \ / \ -# 4 5 6 7 -is sum_tree( - [ - [ - [undef, undef, 4], - [undef, undef, 5], - 2], - [ - [undef, undef, 6], - [undef, undef, 7], - 3], - 1]), - 36, 'full tree'; - -# 1 -# / -# 2 -# / -# 3 -is sum_tree( - [ - [ - [undef, undef, 3], - undef, - 2], - undef, - 1]), - 6, 'left rod'; - -# 1 -# \ -# 2 -# \ -# 3 -is sum_tree( - [ - undef, - [ - undef, - [undef, undef, 3], - 2], - 1]), - 6, 'right rod'; - -done_testing; +sub run_tests { + pass 'init'; + + SKIP: { + skip 'examples' unless $examples; + # 1 + # / + # 2 + # / \ + # 3 4 + + is sum_tree( + [ + [ + [undef, undef, 3], + [undef, undef, 4], + 2], + undef, + 1]), + 13, 'Example 1'; + + # 1 + # / \ + # 2 3 + # / / \ + # 4 5 6 + + is sum_tree( + [ + [ + [undef, undef, 4], + undef, + 2], + [ + [undef, undef, 5], + [undef, undef, 6], + 3], + 1]), + 26, 'Example 2'; + } + SKIP: { + skip 'tests' unless $tests; + # 1 + # / \ + # / \ + # 2 3 + # / \ / \ + # 4 5 6 7 + is sum_tree( + [ + [ + [undef, undef, 4], + [undef, undef, 5], + 2], + [ + [undef, undef, 6], + [undef, undef, 7], + 3], + 1]), + 36, 'full tree'; + + # 1 + # / + # 2 + # / + # 3 + is sum_tree( + [ + [ + [undef, undef, 3], + undef, + 2], + undef, + 1]), + 6, 'left rod'; + + # 1 + # \ + # 2 + # \ + # 3 + is sum_tree( + [ + undef, + [ + undef, + [undef, undef, 3], + 2], + 1]), + 6, 'right rod'; + } + + done_testing; + exit; +} |
