[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] The Powers of Annoy



miku wrote:

> Sure it1s a classic. But as sure as my first program is helloing the
> world, my second program as a learner (in this case, MacPerl)
> rearranges disks on sticks. Now I would like to "improve" :) my
> programming style, i. e. learn how make the program even *more
> compact* and shorter, or even *trickier*. Do you have any suggestions?
>
> B.T.W. In this program, the disks are represented as numbers; the
> higher the number, the bigger the disk. You can enter the initial disk
> height on stick one in line one, right at the beginning of the code.
> For instance, if you want only four disks to be carried to another
> stick, omit the "5". Or, if you want a higher initial tower, add
> "876". If you want to get higher than "9", add letters, for instance
> "BA987654321".
>
> B.T.W.2: How can I stop a running script under MacPerl? Command-. does
> not seem to work, neither does the Escape key.
>
> But now the script for the "Powers of Annoy":
>
> $f = "54321"; $fl= length($f); $e = 2**$fl-1; $" = ", ";
> @o = ($f, "", ""); print "\n\nThe Towers of Annoy\n\nInitial state:
> @o\n";
> $y=0; $n=0; until ($n >= $e) {chop(@o[$y]); $u = $y; $y=(($y+1)%3);
> $o[$y] .= "1";
> $n++; print "Move #$n: @o\n"; unless ($n >= $e) {
> $p = @o[$u]; $s = length($p); $g = substr($p,$s-1,1);
> $v = (3-($y+$u));  $r = @o[$v]; $t = length($r); $h =
> substr($r,$t-1,1);
> if ($h eq "") {chop(@o[$u]); @o[$v] .= $g}
> elsif (($g eq "") || ($h lt $g)) {chop(@o[$v]); @o[$u] .= $h}
> else {chop(@o[$u]); @o[$v] .= $g} $n++; print "Move #$n: @o\n";}}
>
> Have fun!
> Michael

Lieber Michael,

Vorschläge für Verbesserungen:
          (Suggestions for improvements:)
1. Deine "ärgerlichen" Türme stehen naürlich in >>Hanoi<<.
          (Towers of Hanoi (Capital of Viet Nam)).
2. Nur 1 Anweisung je Zeile
         (1 statement per line)
3. Je Stufe 2 Zeichen einrücken.
          (indent 2 spaces per level)
4. Die Veränderlichen sollten anschaulichere NAmen hab en als nur a, b,
c, d usw.
          (find identiers for your scalars  that are more descriptive
than a, b, c, d, ...)
4. Erläutere, was die einzelnen Schritte sollen.
          (add comments of your strategy)
5. $y=0 und $n=0 sind unnötig, da (anders als in Pascal) neue
Veränderliche 0 bzw. "" sind.
          (unlike Pascal,  Perl does not need initialization of scalars)

6. Nach diesen Verbesserungen stell es nochmal vor.
          (After these improvements post it again.)

B.T.W.2: press ctrl-alt-command-escape or PPP (pull power plug).


Detlef Lindenthal, Netzwerkstatt   <detlef@lindenthal.com>


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org