Ok... I have decided to try creating a golf hole...

Given an alphabet and a list of letters create a comma separated ranged list that represents that list.

Since that probably makes little sense, write a subroutine that takes two array refs, the first array ref contains an alphabet, in it's proper order. To constrain it the alphabet will contain ther letters

0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R +S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
that is the range of chars 0x30..0x7e. The alphabet will contain some or all of these letters, and the order they are in the array is their order in the alphabet. There will be no duplicates, case matters

The second array ref will contain any number of these characters, possibly duplicated. You're subroutine should compile them into a list, comma separated and in the order of the alphabet. Anywhere there is a run of characters in a row, you should replace it with a range. Squash any duplicates.

Here is an example.

#alphabet @alpha = split '', 'G;_0x}snafuU='; @list = split '', 'snafu_G0}U'; s(\@alpha,\@list); # prints # G,_..0,}..U # or if it is easier to , a run of 2 # G,_,0,}..U
subroutine should print the output or return it as a string

This is my first attept to create a golf hole, so forgive me if snags occur :)

Update Doh, japhy caught an error in my example... should be good now.

Update2 to be nice to japhy, your golf may return the value instead of print it...

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
Re: (Golf) ranging on alphabet
by japhy (Canon) on Sep 27, 2001 at 07:59 UTC
    Given your input, there should be a range of "_" to "U", shouldn't there? "0" and "}" are right next to each other in your alphabet. Anyway, here's my response. I adapted my number-range regex.
    # 48 * 3 = 144 chars sub GOLF { #23456789012345678901234567890123456789012345678 my$l=join'',@{+pop};my$a=pop;$l=join',',grep$l=~ s/\Q$_//g,@$a;$a={map{$$a[$_],$$a[$_+1]}0..@$a-2 };$l=~s/(.)(?:,((??{"\Q$$a{$+}"})))+/$1..$+/g;$l }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;


      We can rework that slightly to remove characters, i count 136 with the same functionality (please tell me if my testing missed something :)
      #23456789012345678901234567890123456789012345678 $b=join'',@{+pop};@_=@{+pop};$a={map{$_[$_],$_ [$_+1]}0..@_-2};$_=join',',grep$b=~s/\Q$_//g, @_;s/(.)(?:,((??{"\Q$$a{$+}"})))+/$1..$+/g;$_
      Put a print instead of a $_ for that last bit and it's 139 characters...

      jynx

        This one is 127 chars. I hope it still works - if not correct me. :)
        #23456789012345678901234567890123456789012345678 $b=join'',@_={+pop};$a={map{$_[$_],$_[$_+1]}0.. @_-2};$_=join',',grep$b=~s/\Q$_//g,@_;s/(.)(?:,( (??{"\Q$$a{$+}"})))+/$1..$+/g;$_


        $§=lc(sub{}^" \6 \n");$§=~s/\(.*\)//;for(split
        ('',$§)){$/=$_;for(5..7){$/++}$:.=$/}print$:;1