http://www.perlmonks.org?node_id=78343


in reply to Re: (Golf as well): List of Partitions
in thread (Golf as well): List of Partitions

Cute idea, but your output from P(11) is..ambiguous.

And P(12) has the incorrect "1011" entry for 10, 11.

UPDATE
Nice improvement. It can indeed be compressed. Here is 74 characters:

sub P{ my$n=pop;[$n],map{my$i=$_;grep{!grep$_>$i,@$_}map[$i,@$_],P($n-$i)}1.. +$n-1 }

Replies are listed 'Best First'.
Re: Re (tilly) 2: (Golf as well): List of Partitions
by danger (Priest) on May 07, 2001 at 00:52 UTC

    Hmm, and we can shave that down to 71 chars:

    sub P{ [@_],map{my$i=$_;grep{!grep$_>$i,@$_}map[$i,@$_],P($_[0]-$i)}1..$_[0]- +1 }
      Nice, now slimed down to 64 on the knowledge that the partitions are always sorted from largest number to smallest...
      sub P{ [@_],map{my$i=$_;map$$_[0]>$i?():[$i,@$_],P($_[0]-$i)}1..$_[0]-1 }
      UPDATE
      Saved an additional char because my map could be commified.

      UPDATE 2
      Scraping the barrel for 61:

      sub P{ my$i;[@_],map{map$$_[0]>$i?():[$i,@$_],P($_[0]-++$i)}2..$_[0] }

        Tilly, i would say something, but it would sound clunky compared to the beautiful code above. It would be nice if i could ++ you more than once for that snippet.

        One question though, what's commified? i'm not sure if i've ever heard the term...

        jynx