diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-09-07 20:50:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-07 20:50:42 +0100 |
| commit | 0106ab6e14c26362a699dc0a545c8a675a76160f (patch) | |
| tree | 73fdd0600a7537a536e5405498ce7d22760298e7 /challenge-129 | |
| parent | 09af9acda817027ed71a107dfeed7d7e45fd343c (diff) | |
| parent | 7ebeb962a8a3126f08b3b21224c5e13a9cbf32b0 (diff) | |
| download | perlweeklychallenge-club-0106ab6e14c26362a699dc0a545c8a675a76160f.tar.gz perlweeklychallenge-club-0106ab6e14c26362a699dc0a545c8a675a76160f.tar.bz2 perlweeklychallenge-club-0106ab6e14c26362a699dc0a545c8a675a76160f.zip | |
Merge pull request #4851 from wlmb/challenges
Solve PWC 129
Diffstat (limited to 'challenge-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; |
