Golf a sub that given a sorted list of integers as input, produces (correct) output as required by the OP in Problems with looping through an array including the grouping.

Rules: No strict, No warnings, $/="\n" (-l implied).

Can you beat 77?


sub ject{
#        1         2         3         4         5         6         7         8
#234567890123456789012345678901234567890123456789012345678901234567890123456789
$l=$n=$c=0;map{($l eq$_ or$n=0),$l=$_ and print!$n?$/:'',++$c,") $_-",++$n}@_
}


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
  • Comment on GOLF: Input sort list of integers, Output as [356853]

Replies are listed 'Best First'.
Re: GOLF: Input sort list of integers, Output as [356853]
by EvdB (Deacon) on May 27, 2004 at 14:48 UTC
    After much fun down to 44.
    # 1 2 3 4 5 #1234567890123456789012345678901234567890123456789012345678 #map{$j=0 if$l!=$_&&print'';print++$i.") $_-".++$j;$l=$_}@_ #map{print$j=''if$l!=$_;print++$i.") $_-".++$j;$l=$_}@_ #map{print''if!$A{$_};print++$i.") $_-".++$A{$_}}@_ print$A{$_}?'':$/,++$i.") $_-".++$A{$_}for@_
    Update 1: Now down to 43:
    print!$A{$_}&&$/,++$i.") $_-".++$A{$_}for@_
    Update 2: Now down to 42:
    print!$A{$_}++&&$/,++$i.") $_-$A{$_}"for@_
    Update 3: Now 41 (Inspired by Abigail-II):
    print$/x!$A{$_}++,++$i.") $_-$A{$_}"for@_

    --tidiness is the memory loss of environmental mnemonics

Re: GOLF: Input sort list of integers, Output as [356853]
by Abigail-II (Bishop) on May 27, 2004 at 13:46 UTC
    Down to 48:
    # 1 2 3 4 #23456789012345678901234567890123456789012345678 print$/x($:ne$_),++$c,") $_-",++$;xor$:=$_ for@_

    Abigail

      42:
      # 1 2 3 4 #23456789012345678901234567890123456789012 print$/x!$;{$_}++,++$;,") $_-",$;{$_}for@_

      Abigail

        I just tried running several of your solutions with the input asked for in the original question. You are missing a newline per number input.

        Look at Problems with looping through an array and you'll see that when presented with a string of 1's you should separate them by returns, and there should be a blank line between groups of numbers. Fixing this output issue adds 3 more characters:

        print$/x!$;{$_}++,++$;,") $_-",$;{$_},$/for@_

        Obviously I need to learn to read better...

Re: GOLF: Input sort list of integers, Output as [356853]
by demerphq (Chancellor) on May 27, 2004 at 14:12 UTC

    First try is 63 chars.

    #!perl -l # 0 1 2 3 4 5 6 6 # 012345678901234567890123456789012345678901234567890123456789012 sub g {printf"%s%d) %d-%d\n",$a&&!$_{$_}&&$\||'',++$a,$_,++$_{$_}for@_ +} g(0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,5,5,5);

    :-)

    Update: 58 chars.

    #!perl -l # 0 1 2 3 4 5 5 # 0123456789012345678901234567890123456789012345678901234567 sub g {printf"%s%d) $_-%d\n",$a&&!$_{$_}?$\:'',++$a,++$_{$_}for@_} g(0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,5,5,5);

    Update2: 49 chars.

    #!perl -l # 0 1 2 3 4 4 # 0123456789012345678901234567890123456789012345678 sub g {print$a&&!$_{$_}?$\:'',++$a,") $_-",++$_{$_}for@_} g(0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,5,5,5);

    And none of my solutions screw up any special global vars. :-) ($a doesnt count IMo becuase this routine wont be called from within a sort call.)


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Re: GOLF: Input sort list of integers, Output as [356853]
by Abigail-II (Bishop) on May 27, 2004 at 12:27 UTC
    Just munging your try, I'm down to 53 chars (and it deals with 0's as well):
    # 1 2 3 4 5 #2345678901234567890123456789012345678901234567890123 $;*=$:eq$_,$:=$_,print$;?'':$/,++$c,") $_-",++$;for@_

    Abigail

Re: GOLF: Input sort list of integers, Output as [356853]
by Abigail-II (Bishop) on May 27, 2004 at 12:16 UTC
    Your program isn't working if the input contains 0's, which I think wasn't ruled out. But for the program as given, there's an obvious save of 11 characters by leaving the "$l=$n=$c=0;" off. It's no warnings after all.

    Abigail

      Well and good, but you kinda ruined the challenge...

      1. There is no point golfing the problem to some impossible target before you post it.
      2. The whole point of obscuring my solution is to encourage people to have a crack at it themselves before they look. That tends to find alternative approaches rather than everyone (or just you!) squeaking out an extra point here and there.

      But then, you know all that.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
        The whole point of obscuring my solution
        Eh, I don't know what you tried, but there was nothing obscured about it. At least, not for me.

        Abigail