Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: compute paths in Pascal's triangle (aka Tartaglia's one) -- the winner is..

by LanX (Saint)
on Mar 25, 2018 at 22:08 UTC ( [id://1211707]=note: print w/replies, xml ) Need Help??


in reply to Re^2: compute paths in Pascal's triangle (aka Tartaglia's one) -- the winner is..
in thread compute paths in Pascal's triangle (aka Tartaglia's one)

> I tried to use your solution in my program but, I must admit, I discarded it soon.

Easy... ;-)

use strict; use warnings; use Data::Dump qw/pp dd/; my @paths = find_paths ( [0,0,'start'], [3,1,'goal'] ); pp @paths; sub find_paths { my ($start,$goal)=@_; # --- transform to easier coordinates ($start,$goal) = map old2new($_), ($start,$goal); # --- define closure my @results; my ($gl,$gr) = @$goal; my $pathfinder; $pathfinder = sub { my ( $last ) = @_; # pp \@_ ;# track recursion path my ( $l, $r ) = @$last ; if ( $gl == $l and $gr == $r) { push @results, [ map new2old($_), reverse @_ ]; return; } $pathfinder->( [$l+1,$r ,"left" ], @_ ) if $l < $gl; $pathfinder->( [$l ,$r+1 ,"right"], @_ ) if $r < $gr; }; # --- init recursion $pathfinder->($start); return \@results; } # -------------------------------------------------- # coordinate transformations sub old2new { # left = level - right my ($a_old)=@_; my @new = @$a_old; $new[0] = $new[0] - $new[1]; return \@new; } sub new2old { # level = left + right my ($a_new)=@_; my @old = @$a_new; $old[0] = $old[0] + $old[1]; return \@old; }

( "Result:", [ [ [0, 0, "start"], [1, 0, "left"], [2, 0, "left"], [3, 1, "right"], ], [ [0, 0, "start"], [1, 0, "left"], [2, 1, "right"], [3, 1, "left"], ], [ [0, 0, "start"], [1, 1, "right"], [2, 1, "left"], [3, 1, "left"], ], ], )

Please note, any recursion can be written as iteration. If speed matters this might be worth it.

tybalt is using the same algorithm, just starting from the end (so he doesn't need to reverse the path) and more golfy.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: compute paths in Pascal's triangle (aka Tartaglia's one) -- the winner is..
by LanX (Saint) on Mar 26, 2018 at 08:52 UTC
    Other footnotes:
    • If speed matters: the recursive approach can be speed up by memoizing the tails of a path to the last point.
    • An easy iterative approach ( now Breadth-first_search) can be constructed from this code.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1211707]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found