Hi All,

Somone posted what seemed like a homework question here. I replied in a golfish format, and mentioned the fact to the CB. Chemboy suggested posting it as a real golf, wherupon CheeseLord stepped in and said that in a similer reply, which also called for Golf, tachyon had provided an even better answer than mine.

Seemed like an interesting problem, so I present the Nth annual rangify function golf contest!

Problem
Given an unsorted list of unique positive integers convert this list into a "rangeified" form, where all consequtive sequences of numbers are coded into a BEGIN-END format.

For instance, the list 1089,3,4,0,5,6,7,99,832,1087,831,1088
would become 0,3-7,99,831-832,1087-1089

In order to keep it interesting answers in three catagories will be allowed, also code _must_ be strict safe.

  1. Returns a stringified version of the list
  2. Returns a list
  3. Does *not* use map()
So to start things off I present my solution, rather big I must say at 105 chars, for catagories 1 and 3 as the following code:
# 1 2 3 4 5 6 # 01234567890123456789012345678901234567890123456789012345678901234567 +890 sub demerphq{ @0=sort{$a<=>$b}@_;$_=":@0";1 while s/:((\d+)(-(\d+))?) (\d+)/ $5-$2-1&&$5-$4-1?"$1,:$5":":$2-$5"/e;y/://d;$_ }
Thanks and apologies (for mispelling his name) to CheeseLord for a couple of tips. But from what I've heard it wont help me much....

:-)

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)


In reply to (Golf) Rangify Array by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.