Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
> in hindsight it's probably better implemented in the new coordinate system and only the results are transformed back.

> The code is much easier then.

Yep!

use strict; use warnings; use Data::Dump qw/pp dd/; my $goal = [3,1,'goal']; my $start = [0,0,'start']; pp "OLD: ",[$start,$goal]; ($start,$goal) = map old2new($_), ($start,$goal); pp "NEW: ",[$start,$goal]; my ($gl,$gr) = @$goal; my @results; pathfinder( $start ); pp \@$_ for @results; sub pathfinder { my ( $last ) = @_; 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; } # -------------------------------------------------- # 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; }

Compilation started at Thu Mar 22 16:09:33 C:/Perl_64/bin\perl.exe d:/tmp/pascale_path.pl ("OLD: ", [[0, 0, "start"], [3, 1, "goal"]]) ("NEW: ", [[0, 0, "start"], [2, 1, "goal"]]) [ [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"], ] Compilation finished at Thu Mar 22 16:09:33

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


In reply to Re^2: compute paths in Pascal's triangle (aka Tartaglia's one) by LanX
in thread compute paths in Pascal's triangle (aka Tartaglia's one) by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-23 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found