Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
First all, apologies for ruining some nice code. I have added a print statement and 4 comment points (A .. D).
#!/usr/bin/perl -w use strict; use warnings; my %piles = (A => [reverse 1..3], B => [], C => []); dumpPiles (); hanoi (scalar @{$piles{A}}, keys %piles); #A sub hanoi { my ($n, $start, $end, $extra, $recursion) = @_; print "Hanoi: Disk $n\n"; if ($n == 1) { #B report ($start, $end, $recursion); } else { hanoi($n-1, $start, $extra, $end); #C report ($start, $end, $recursion); hanoi($n-1, $extra, $end, $start); #D } } sub report { my ($start, $end) = @_; my $disk = pop @{$piles{$start}}; print "Moved disk $disk from $start to $end.\n"; push @{$piles{$end}}, $disk; dumpPiles (); } sub dumpPiles { for my $pile (sort keys %piles) { print "$pile: @{$piles{$pile}}\n"; } }

The first hanoi call happens at point A, where the first argument (which disk to move) is equal the number of discs on pile A; namely '3'. We then move into the hanoi iterative method where the logical statement at B is false and we move to point C.

At point C we call hanoi again (note we have not called report yet, but the first argument has been decremented to '2'. This means we arrive at point B for the second time and the logical statement is still false.

We arrive at point C again, calling hanoi on disk number 1. Now the statement at B is true and the iterations begin to unravel.

Perhaps, together with the additional print statement in the code, this will help to explain things?

-=( Graq )=-


In reply to Re^3: cannot follow hanoi subroutine by graq
in thread cannot follow hanoi subroutine by convenientstore

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 chilling in the Monastery: (3)
As of 2024-03-29 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found