in reply to compute paths in Pascal's triangle (aka Tartaglia's one)
I was playing with this a bit, since it's such a fun little problem. Here's a version that returns the answer as the value of the function.
#!/usr/bin/perl # http://perlmonks.org/?node_id=1211497 use strict; use warnings; sub up { my ($row, $col) = split /-/, $_[0]; return $_[0] eq '0-0' ? "@_\n" : ($row * $col > 0 && up( ~-$row . '-' . ~-$col, @_ ) ) . ($row > $col && up( ~-$row . '-' . $col, @_ ) ); } print up( '3-1' );
It's only a two statement function (of course one statement is very long :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: compute paths in Pascal's triangle (aka Tartaglia's one) -- the winner is..
by Discipulus (Canon) on Mar 25, 2018 at 20:48 UTC | |
I got back many interesting replies. For sure I was not there when recursive thinking was ditributed: infact I had a lot of trouble understanding your solutions, oh wise monks. LanX: thanks! you was the first giving a working solution and also some variations around it. They are also almost easy to understand as, you said, you proceded by baby steps. Anyway, if I'm permitted, I find your approach convoluted: your sub needed the top tile to passed in and goal's coordinates are external global variables. I tried to use your solution in my program but, I must admit, I discarded it soon. You are a genial problem solver but I do not want to be the man who has to debug your code ;=) hdb: I never had a doubt about you. Your is a scientific approach, where the use of Algorithm::Permute gives an idea of your stence to the problem, is clean and effective. I will not use your function just for the mere (somehow idiotic) motivation that I do not want to add a dependency to my program. I'd like to see your code explained by you. The only remark I can do to your code is that the plural form of path is paths and not pathes.. paté? ;=) I plan to use your spoken prove of the property in the help text of my project. VinsWorldcom what to say? Intelligence is using his own schemas to understand the reality. You are using nowadays the Graph module and you immediately perceived a possibility to use it to solve my question. TIMTOWTDI vr: thanks to you too; an iterative solution! I forgot to mention that I was not there also when iterative intelligence was distributed.. You were there, obviously. roboticus: you built not only the answer but the entire frame where the problem lies: a package with six methods draw the whole context. Your code is easy to read and to expand. Thanks! oiskuu you was the first to address me to a correct solution in the CB: your scientific approach using Algorithm::Combinatorics is what I suspected for the first moment it had to exists. Unfurtunately I have not a mind to imagine nor implement it tybalt89 you are born in a golf field? your solutions are sharpened as a razor: clean and straight to the goal. As Eily anticipated in the chatter box, one good option is to recurse upward. You get involved in this fun little problem as you said and you produced this:
Apart for the cleverness of the overall structure, you recurse if $row * $col > 0 and if $row > $col can you explain why? I suppose you are checking you are still within the boudaries of the triangle. Right? Anyway I tried to analize your code, just as a medieval jewish doctor dissecting a corpse watching what passed, and I arrived to:
and if I run this sub calling using Data::Dump method dd as in dd up(3-1) it produces:
I hope the above shows well what is happening and the recursion level. What I can say? genial! Now I plan (but see update below..) to use a modified version that uses AoA as input and output and not the stringy form 3-1 and, even if it is uglier to see it respect more my original intention:
The above modified version is uglier because it must to be called as up_modified ([[(3,1)]]); that is nothing good to see.. but it works returning AoA ie an array of coordinates pairs that is what I need in my project to colorize them. It produces, in this verbose version, the following output:
final words thanks you all wise monks to have contributed to this thread: I'm speachless seeing how many different approaches were proposed, each one valid and intresting on it's own. L* update March 26 it was to late.. this is simpler in construct and in what receives/returns:
There are no rules, there are no thumbs.. Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS. | [reply] [d/l] [select] |
by tybalt89 (Monsignor) on Mar 26, 2018 at 02:21 UTC | |
AoA with a less ugly call
Outputs:
And yes, the row and col checks are to stay within the triangle.
| [reply] [d/l] [select] |
by LanX (Saint) on Mar 25, 2018 at 22:08 UTC | |
Easy... ;-)
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
| [reply] [d/l] [select] |
by LanX (Saint) on Mar 26, 2018 at 08:52 UTC | |
Cheers Rolf
| [reply] |
by LanX (Saint) on Mar 25, 2018 at 21:13 UTC | |
> you proceded by baby steps. I call this maintainable. > I find your approach convoluted: your sub needed the top tile to passed in I call this flexible, it allows differing starting points > and goal's coordinates are external global variables. no I used lexicals, > I do not want to be the man who has to debug your code My fault, I expected the use of closures to be elementary. xD Put a block around it or an extra sub ... > You are a genial problem solver I'm ... running out of arguments. ;-)
Cheers Rolf
| [reply] |