[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] perlgolf - vowel sort



I've beaten the current best hole 6 solution by two strokes, 
unless I've missed something:

: Write a subroutine that accepts a list of words, and returns 
: the list sorted by the first vowel that appears in each word. 
: If two words have the same first vowel, it does not matter 
: which is sorted first.
:
: Words will be in lowercase and will always contain at least 
: one vowel. (Vowels are a, e, i, o, and u.)
:
: e.g.:  hole('dog', 'cat', 'fish', 'duck', 'lemur')
: returns:  ('cat', 'lemur', 'fish', 'dog', 'duck')

My initial answer, which uses the empty regex pattern:

    sub hole {sort{($a=~/[aeiou]/g)[0]cmp($b=~//g)[0]}@_}

After looking at the solutions (which didn't help much), I 
refined it down to 36 strokes:

    sub hole {sort{$a=~/[aeiou]/.$&cmp$b=~//.$&}@_}

-- 
Keith C. Ivey <kcivey@cpcug.org>
Washington, DC

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe