MacTech Network:   MacTech Forums  |  MacForge.net  |  Computer Memory  |  Register Domains  |  Cables  |  iPod Deals  | Mac Deals  | Mac Book Shelf


  MacTech Magazine

The journal of Macintosh technology

 
 

Magazine In Print
  About MacTech  
  Home Page  
  Subscribe  
  Archives DVD  
  Submit News  
  MacTech Forums  
  Get a copy of MacTech RISK FREE  
MacTech Only Search:
Community Search:
MacTech Central
  by Category  
  by Company  
  by Product  
MacTech News
  MacTech News  
  Previous News  
  MacTech RSS  
Article Archives
  Show Indices  
  by Volume  
  by Author  
  Source Code FTP  
Inside MacTech
  Writer's Kit  
  Editorial Staff  
  Editorial Calendar  
  Back Issues  
  Advertising  
Contact Us
  Customer Service  
  MacTech Store  
  Legal/Disclaimers  
  Webmaster Feedback  
 Get Netflix

 August 2000 Programmer's Challenge

Longest Word Sort

Mail solutions to: progchallenge@mactech.com
Due Date: 11:59pm ET, Tuesday, 1 August 2000

This month’s problem puts a twist on a well-known computing problem, the common text sort. Your Challenge is to sort a block of text into ascending or descending order. Yawn.... Except that the comparison function used to do the sort is a little unusual….

The prototype for the code you should write is:

void LongestWordSort(
  char *textToSort,      /* the text to be sorted */
  long numChars,         /* the length of the text in bytes */
  Boolean descending,    /* sort in descending order if true */
  Boolean caseSensitive  /* sort is case sensitive if true */
);

The textToSort provided to your LongestWordSort routine consists of a sequence of lines of text, each terminated by a return character (0x0D). You are to sort the lines of text in place into either ascending or descending order, based on the value of the descending flag, considering the text as either case sensitive or not case sensitive, based on the caseSensitive flag. The twist is that the primary sort criterion is the length of the words in the line, regardless of the position of the word in the line.

Words are a sequence of alphanumeric characters, separated by anything else (spaces, punctuation, etc.). A line with a longest word of N characters is considered to be "greater" than any line with a longest word of fewer characters. Among lines with longest words of the same length, the line with the larger number of words of that length is considered "greater". If two lines have the same number of longest words of the same length, the sort order is based on the length of the next-longest word, and so on. Finally, among lines with words of exactly the same length, the sort order is based on text order of the individual words, compared in order of decreasing length, and then in order of occurrence. Whether the sort is done in ascending or descending order, the determination of which line is "greater" is always based on the longest word first, then the next longest, etc.

As an example, examine the following lines:

        the earliest birds
        quick early birds
        early over quick
        the quick foxes
        early birds win

These lines are correctly sorted in descending order. The first line has the longest word. The remaining lines all have a longest word of 5 letters, but the second line has more of them. The last 3 lines all have 2 words of 5 letters, but the 3rd line has a second-longest word of 4 letters. Finally, the last line is smaller than the one before it, based on a comparison of "early" and "quick".

The text may include non-alphanumeric characters, which should be ignored for purposes of comparison.

This will be a native PowerPC Challenge, using the CodeWarrior Pro 5 environment. Solutions may be coded in C, C++, or Pascal. This month, we’ll also allow solutions that are completely or partially coded in assembly language. The winner will be the solution that correctly sorts a number of blocks of text in the least amount of execution time.

This problem was suggested by Ernst Munter, who earns two Challenge points for the suggestion.


Test code for this Challenge is now available.


You can get a head start on the Challenge by reading the Programmer's Challenge mailing list. It will be posted to the list on or before the 12th of the preceding month. To join, send an email to listserv@listmail.xplain.com with the subject "subscribe challenge-A". You can also join the discussion list by sending a message with the subject "subscribe challenge-D".





Generate a short URL for this page:


Click on the cover to
see this month's issue!

TRIAL SUBSCRIPTION
Get a RISK-FREE subscription to the only technical Mac magazine!

Today's Deal


Apple Special

Order
Snow Leopard,
Mac Box Set, Family Pack,
and Snow Leopard Server
at a discount.
 


MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797

Register Low Cost (ok dirt cheap!) Domain Names in the MacTech Domain Store. As low as $1.99!
Save on long distance * Upgrade your Computer. See local info about Westlake Village
appleexpo.com | bathjot.com | bathroomjot.com | bettersupplies.com | comclothing.com | computerlunatics.com | dotcomclothing.com | explainit.com | exposinternational.com | homeismycastle.com | hoodcards.com | intlexpo.com | keyinfocard.com | kosheru.com | learnmorsels.com | localdealcards.com | lvschools.com | macjobsearch.com | mactechjobs.com | mactechmonitor.com | mactechsupplies.com | macwishbook.com | movie-depot.com | netprofessional.com | nibblelearning.com | notesintheshower.com | officejot.com | onlinebigbox.com | palmosdepot.com | peopleslineitemveto.com | showerjot.com | snapestore.com | snapishop.com | snapistore.com | snaptradingpost.com | stimulusmap.com | stimulusroadmap.com | triunfoguides.com | video-depot.com
Staff Site Links



All contents are Copyright 1984-2008 by Xplain Corporation. All rights reserved.

MacTech is a registered trademark of Xplain Corporation. Xplain, Video Depot, Movie Depot, Palm OS Depot, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, NetProLive, JavaTech, WebTech, BeTech, LinuxTech, Apple Expo, MacTech Central and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.