Re: [Spoiler] QotW 23
Rod Adams <[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
José Alves de Castro wrote:
>I haven't got the time to go through all the submissions yet, but I believe
>mine is a new one. Not in the sense that it's better, but in the sense that
>it's worst :-) Really, really worst, I would bet :-)
>
>I haven't made decent tests with it, but I take it as it will probably be one
>of the worse solutions for this quiz.
>
>Why am I posting it then? Because it took me less than a minute to code, and
>because it's good enough for the smaller cases :-) I just hope I got the output
>right (I read something about the output for 0... oops)
>
>#!/usr/bin/perl -wl
>use strict;
>
>( my $n = shift ) > 0 || die;
>
>print for uniqs( parens($n) );
>
>sub parens {
> $_[0]
> ? map { ( "$_()", "()$_", "($_)" ) } parens( $_[0] - 1 )
> : ('');
>}
>
>sub uniqs {
> my %v;
> grep { !$v{$_}++ } @_;
>}
>
>As you can see, recursion was not enough for me, so I had to do the same thing
>more than once and then get the unique elements out of the list :-) (it would
>be easy to take care of that, though).
>
>Nice problem :-)
>
>Solution goes attached, too...
>With a comment or two...
>
>:-)
>
>
>
This is very similar to my Expand/Enclose routine which, while very
speedy, I had to withdraw.
You will notice that at n=4, it does not generate (())(()). At higher n,
there are more and more patterns that it fails to generate.
All of my attempts to remedy the situation degenerated into a form of my
Nested routine.
-- Rod