[SPOILER] QOTW #23 Solution
Shlomi Fish <shlomif-ik1l9ssToec+JF/[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Here's my solution. It's both short and easy to understand:
<<<
#!/usr/bin/perl -w
use strict;
my $N = shift;
sub recurse
{
my ($string, $num_opened, $num_closed) = (@_);
if (($num_opened == $N) && ($num_closed == $N))
{
print "$string\n";
return;
}
elsif ($num_opened < $N)
{
recurse("$string(", $num_opened+1, $num_closed);
}
if ($num_opened > $num_closed)
{
recurse("$string)", $num_opened, $num_closed+1);
}
}
recurse("", 0, 0);
>>>
Best regards,
Shlomi Fish
--
---------------------------------------------------------------------
Shlomi Fish shlomif-ik1l9ssToec+JF/[email protected]
Homepage: http://shlomif.il.eu.org/
Knuth is not God! It took him two days to build the Roman Empire.