Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

integer partition golf

by jimt (Chaplain)
on Oct 18, 2006 at 17:52 UTC ( #579156=obfuscated: print w/ replies, xml ) Need Help??

Yes, yes, this has been done before (hasn't everything?), but I think this still comes out to be the most compact method to actually calculate and display partitions of integers. The previous thread had people just trying to calculate them in memory. This one takes a single integer on the command line and goes to town.

sub p{my($n,@e,$o)=@_;print if!$_{$_=join$",sort@_,$/}++;p(++$o,--$n,@ +e)while$n-1}p(shift)

90 characters! woo hoo! I only beat tilly's solution by 1 character, once I appended print@$_,$/for P(shift) to it. A more compact printer added on to it would render that one the winner.

Update: Ya know, one of these days, my golf game'll be good enough that I'll post and someone won't immediately respond and say "I can shave off another character!" heh. Props to liverpole and blazar. I incorporated their suggestions, plus another one I thought of to drop down to 83.

sub p{my($n,@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,$n,@e)wh +ile--$n}p pop

Update 2: And 80

sub p{my(@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,@e)while--$ +e[0]}p pop

Comment on integer partition golf
Select or Download Code
Re: integer partition golf
by liverpole (Monsignor) on Oct 18, 2006 at 20:55 UTC
    <homer simpson voice>Mmmmmmm ... gollllf ....</homer simpson voice>

    Can I play too?

    # 1. Convert "shift" to "pop" to save 2 chars # 2. Convert leading "if" to trailing "&&" to # squeeze out one space char # 3. Convert !(conditional)&&(action) to # (conditional)||(action) for one char # # Savings = 4 chars (86 total chars) sub p{my($n,@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,--$n,@e) +while$n-1}p(pop)

    I've a feeling it can go smaller, though :)

    Update  Oh, and ...

    # 4. Remove parens from subroutine for 1 char # # Total: 85 chars sub p{my($n,@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,--$n,@e) +while$n-1}p pop

    I still think it can get smaller ...


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: integer partition golf
by blazar (Canon) on Oct 18, 2006 at 21:14 UTC

    You can still save 5 chars without changing your logic, but adopting a pair of common golf tricks:

    sub p{my($n,@e,$o)=@_;$_{$_=join$",sort@_,$/}++||print;p(++$o,--$n,@e)while$n-1}p pop
Re: integer partition golf
by liverpole (Monsignor) on Oct 19, 2006 at 00:57 UTC
    Hi jimt,

            Ya know, one of these days, my golf game'll be good enough that I'll post and someone won't immediately respond and say "I can shave off another character!"

    No, my hat's off to you ... and you gotta give yourself a LOT of credit.

    First, you came up with the original challenge, which was fun.  And even though blazar and I stripped off 5 characters, you stripped off 5 more!

    As I'm sure you're aware, that's a much bigger feat, since getting just a single character here and there gets harder and harder.

    I don't think it can go much lower.  I can only see a 1-character savings at this point, for 79:

    sub p{my(@e,$o)=@_;$_{$_=join$",sort@_,$/}||=print;p(++$o,@e)while--$e +[0]}p pop

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      I don't think it can go much lower. I can only see a 1-character savings at this point, for 79:
      sub p{my(@e,$o)=@_;$_{$_=join$",sort@_,$/}||=print;p(++$o,@e)while--$e +[0]}p pop

      I second that.

      I don't think it can go much lower. I can only see a 1-character saving at this point, for 78:

      sub p{my(@e,$o)=@_;$_{$_="@{[sort@_]}\n"}||=print;p(++$o,@e)while--$e[ +0]}p pop

      :-)

      That fixes the leading $/, too.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        Chopped on more char.....
        sub p{my(@e,$o)=@_;${$_="@{[sort@_]}\n"}||=print;p(++$o,@e)while--$e[0]}p pop

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://579156]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2014-10-21 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (99 votes), past polls