### Re: Number from given digits puzzle

by pKai (Priest)
 on Mar 30, 2007 at 21:06 UTC ( #607586=note: print w/replies, xml ) Need Help??

in reply to Number from given digits puzzle

No idea what the desired output is (No ruby here).

Making 1 change to the perl code, gives me 3 lines of output:

```24 = (3 * (5 - 6))
24 = (6 / (1 - (3 / 4)))
24 = ((5 - 6) * 3)
While the 1st and the 3rd line are junk the 2nd is at least one (the?) solution. I got there by substituting the code construction (sub {...}) in the recursive poss2 call by a mere \$f, reasoning that what is constructed as
```sub {
my(\$v3, \$s3) = @_;
&\$f(\$v3, \$s3);
}
just looks like a wrapper to the input sub.

Does that help in any way?

Of course, I have to admit, I do not understand the algorithm (at the moment; but still trying to figure that part out), nor do I know ruby and can comment on your transforming the iterators ("yield") to perl closures :-/

Replies are listed 'Best First'.
Re^2: Number from given digits puzzle
by ambrus (Abbot) on Mar 31, 2007 at 11:36 UTC

Thanks. Making that change and another trivial one appears to fix the code. I still don't know what problem your change fixes, but I'll try to find it out (it might be something related to aliasing.

The trivial change is that the second addittion operator in line 28 has to be replaced by a concatenation operator:

```                &\$f(\$v1 + 10 * \$v2, \$s1 . \$s2);

Here's the fixed code for convenience:

With one more change, the code works fine:
```27,28c27,28
<       \$s1 =~ /^\d+\$/ and \$s2 =~ /^\d\$/ and
<               &\$f(10*\$v1 + \$v2, \$s1 . \$s2);
---
>       \$s1 =~ /^\d+\$/ && \$s2 =~ /^\d\$/ and
>               &\$f(\$v1 + 10 * \$v2, \$s1 . \$s2);
and the output I see is
```24 = (3 * (14 - 6))
24 = (6 / (1 - (3 / 4)))
24 = ((14 - 6) * 3)
which seems quite OK.

Now I haven't looked yet if that's an oneliner in some Perl Golf competition, but I'd bet there is something :)

Would be nice to let us now if in the end you had some personal conclusions about your attempted Ruby/Perl comparison.

Thanks,
Krambambuli

Oh yes. Even the ruby code has that bug.

(Take care because you're giving a reverse patch.)

Create A New User
Node Status?
node history
Node Type: note [id://607586]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2021-04-14 17:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?