jynx has asked for the wisdom of the Perl Monks concerning the following question:
Let's start with some code:
So firstly, the problem i'm having with the algorithm is that it doesn't return the else lists correctly. In particular it doesn't return the original [@_] part of the list. This is made obvious by running it and finding that the code returns a list including (for 5):sub P { # This is so that we don't go off the deep end with recursion : ) return [1,1] if $_[0]==1&&$_[1]==1; return [1] if $_[0]==1; # If there's only one argument, return a list of lists containing # the argument and it's partitions. if (@_==1) { return [@_], P($_[0]-1,1) # Otherwise return a list of lists containing the arguments and # the partitions of the opposites. } else { return [@_], map({ [@$_, $_[1]] } P($_[0])), map({ [$_[0], @$_] } P($_[1])) } }
Note how <bold>3 2</bold> did not show up. Although Part(2) gets called, it's not returning correctly, or so it seems. i've tried to loop trace on paper, stepping through each recruse, but i think i missed something, because it should work.5 4 1 3 1 1 2 1 1 1 1 1 1 1 1 And many duplicates (which i handle seperately)
Any enlightenment would be most helpful with this, yet again, after days of work, i'm blinded...
My entry (which is rather long) is above if included with this snippet for removing duplicates (which could probably also be optimized). Currently i weigh in at a whopping 235 characters (without whitespace) if the following is included:
nuf evah,sub u{map{[split//,$_]}sort{$b cmp$a}keys%{{map{my$t=join'',@$_;$t=>1} +@_}}}
|
---|
Replies are listed 'Best First'. | |
---|---|
Re (tilly) 1: (Golf as well): List of Partitions
by tilly (Archbishop) on May 06, 2001 at 16:32 UTC | |
Re: (Golf as well): List of Partitions
by danger (Priest) on May 06, 2001 at 13:15 UTC | |
by tilly (Archbishop) on May 06, 2001 at 16:06 UTC | |
by danger (Priest) on May 07, 2001 at 00:52 UTC | |
by tilly (Archbishop) on May 07, 2001 at 01:04 UTC | |
by jynx (Priest) on May 07, 2001 at 09:39 UTC | |
| |
Re: (Golf as well): List of Partitions
by Masem (Monsignor) on May 06, 2001 at 16:54 UTC | |
by tilly (Archbishop) on May 06, 2001 at 20:39 UTC | |
(tye)Re: (Golf as well): List of Partitions
by tye (Sage) on May 07, 2001 at 19:12 UTC |