Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Based on the comments from this node on sorting, here's a golf challenge for you.

Given an array of "words" that are to be sorted, and an array of characters which make up those words (aka the 'alphabet'. This second array is already sorted in 'alphabetical' order. Assume that no character is repeated in this second array. Also assume that no other characters are present in the words. Also assume that everything is already in the 'lower case' form. Note that the number of characters in the alphabet is arbitary.

Find the perl golf (fewest # of characters in the code) that will sort the words based on this alphabetic order. Some test cases that you should test against would be:

@alpha = ('a'..'z'); @l = o( \@alpha, \@words ); # should give same results as @l = sort { $a cmp $b } @words; @alpha2 = reverse ('a'..'z'); @l2 = o( \@alpha2, \@words ); # should give same results as @l2 = reverse sort { $a cmp $b } @words; + or @l2 = sort { $b cmp $a };

Update to clear up the confusion as merlyn's replies below indicate, the 'alphabet' is not necessarily the typcial 26-character english alphabet, and may consider of any character including and not limited to numbers, punctuation, accented characters, and so forth. For example, one could call the 'alphabet' for hexadecimal numbers as (0..9A..F).


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Golf: Arbitrary Alphabetical Sorting by Masem

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found