diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-09-07 14:05:02 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-09-07 14:05:02 -0500 |
| commit | 7ebeb962a8a3126f08b3b21224c5e13a9cbf32b0 (patch) | |
| tree | 58f4dce98fd0f2585bdfa3cfda1d613709f14a9d | |
| parent | 7e18b8378d8e1dacaba741df7f50c7660770ca1c (diff) | |
| download | perlweeklychallenge-club-7ebeb962a8a3126f08b3b21224c5e13a9cbf32b0.tar.gz perlweeklychallenge-club-7ebeb962a8a3126f08b3b21224c5e13a9cbf32b0.tar.bz2 perlweeklychallenge-club-7ebeb962a8a3126f08b3b21224c5e13a9cbf32b0.zip | |
Solve PWC 129
| -rw-r--r-- | challenge-129/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-129/wlmb/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-129/wlmb/perl/ch-2.pl | 22 |
3 files changed, 70 insertions, 0 deletions
diff --git a/challenge-129/wlmb/blog.txt b/challenge-129/wlmb/blog.txt new file mode 100644 index 0000000000..083e05156d --- /dev/null +++ b/challenge-129/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2021/09/06/PWC129/ diff --git a/challenge-129/wlmb/perl/ch-1.pl b/challenge-129/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..dc3b514ab0 --- /dev/null +++ b/challenge-129/wlmb/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +# Perl weekly challenge 129 +# Task 1: root distance +# +# See https://wlmb.github.io/2021/09/06/PWC129/#task-1-root-distance +use warnings; +use strict; +use v5.12; +# use Data::Dumper; + +my %root_distance; # hash of depths indexed by the node values. +# my $tree= # Throw away the actual tree, as it is not used +analyze_tree(-1,-1); # initialize indentation and depth. +# say Dumper($tree); # For debugging +say "Value: $_ Depth: ", $root_distance{$_}//"Not defined" for @ARGV; + +sub analyze_tree { + my ($previous_indent, $depth)=@_; + ++$depth; + my ($current_indent, $value)=analyze_line($previous_indent); + # If I found a node at the correct indentation, build a tree structure + $root_distance{$value}=$depth, # + return {value=>$value, + left=>analyze_tree($current_indent,$depth), + right=>analyze_tree($current_indent,$depth)} + if defined $current_indent && defined $value && $value ne ''; + return undef; +} + +sub analyze_line { +# Read and analyze lines. Keep them until their turn if outdented + state ($line, $current_indent, $value); + my $previous_indent=shift; + if(!defined $current_indent){ + $line=<STDIN>; + return () if !defined $line || $line=~m/^\s*$/; + die "Malformed tree: $line" unless $line=~m/^(\s*)-\s*(\d*)\s*$/; + $current_indent=length $1; + $value=$2; + } + if($current_indent>$previous_indent){ + my $result_indent=$current_indent; + $current_indent=undef; + return ($result_indent, $value); + } + return (); +} diff --git a/challenge-129/wlmb/perl/ch-2.pl b/challenge-129/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..15f9b79658 --- /dev/null +++ b/challenge-129/wlmb/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 129 +# Task 2: add linked lists +# +# See https://wlmb.github.io/2021/09/06/PWC129/#task-2-add-linked-lists +use warnings; +use strict; +use v5.12; +use List::Util qw(all); +use bigint; # to allow large lists +my @A=split /\s*->\s*/, shift @ARGV; +my @B=split /\s*->\s*/, shift @ARGV; +die "Usage: ./ch-2.pl l1 l2 with list arguments of the form \"dN...d2->d1->d0\"" + unless all {m/^\d$/} @A, @B; +my $A=join '', @A; +my $B=join '', @B; +my $length=@A-@B; +my $indent_A=$length<0?" "x(-$length):""; +my $indent_B=$length>0?" "x $length :""; +say "Input: L1=$indent_A", join "->", @A; +say " L2=$indent_B", join "->", @B; +say "Output: ", join "->", split '', $A+$B; |
