[Spoiler] QotW 23
José Alves de Castro <[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
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...
:-)
--
José Alves de Castro <[email protected]>
http://natura.di.uminho.pt/~jac
parens
(text/plain, 400 B)
#!/usr/bin/perl -wl
use strict;
=head1
A little bit of recursion and here it is.
The program dies if no argument or a negative one is supplied.
This file has precisely 400 bytes :-)
=cut
( my $n = shift ) > 0 || die;
print for uniqs( parens($n) );
sub parens {
$_[0]
? map { ( "$_()", "()$_", "($_)" ) } parens( $_[0] - 1 )
: ('');
}
sub uniqs {
my %v;
grep { !$v{$_}++ } @_;
}