TweetFollow Us on Twitter

Apr 94 Challenge
Volume Number:10
Issue Number:4
Column Tag:Programmers’ Challenge

Related Info: Memory Manager

Programmers’ Challenge

By Mike Scanlin, MacTech Magazine Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

The rules

Here’s how it works: Each month there will be a different programming challenge presented here. First, you must write some code that solves the challenge. Second, you must optimize your code (a lot). Then, submit your solution to MacTech Magazine (formerly MacTutor). A winner will be chosen based on code correctness, speed, size and elegance (in that order of importance) as well as the postmark of the answer. In the event of multiple equally desirable solutions, one winner will be chosen at random (with honorable mention, but no prize, given to the runners up). The prize for the best solution each month is $50 and a limited edition “The Winner! MacTech Magazine Programming Challenge” T-shirt (not to be found in stores).

In order to make fair comparisons between solutions, all solutions must be in ANSI compatible C (i.e., don’t use Think’s Object extensions). Only pure C code can be used. Any entries with any assembly in them will be disqualified (except for those challenges specifically stated to be in assembly). However, you may call any routine in the Macintosh toolbox you want (i.e., it doesn’t matter if you use NewPtr instead of malloc). All entries will be tested with the FPU and 68020 flags turned off in THINK C. When timing routines, the latest version of THINK C will be used (with ANSI Settings plus “Honor ‘register’ first” and “Use Global Optimizer” turned on) so beware if you optimize for a different C compiler. All code should be limited to 60 characters wide. This will aid us in dealing with e-mail gateways and page layout.

The solution and winners for this month’s Programmers’ Challenge will be published in the issue two months later. All submissions must be received by the 10th day of the month printed on the front of this issue.

All solutions should be marked “Attn: Programmers’ Challenge Solution” and sent to Xplain Corporation (the publishers of MacTech Magazine) via “snail mail” or preferably, e-mail - AppleLink: MT.PROGCHAL, Internet: progchallenge@xplain.com, CompuServe: 71552,174 and America Online: MT PRGCHAL. If you send via snail mail, please include a disk with the solution and all related files (including contact information). See page 2 for information on “How to Contact Xplain Corporation.”

MacTech Magazine reserves the right to publish any solution entered in the Programming Challenge of the Month and all entries are the property of MacTech Magazine upon submission. The submission falls under all the same conventions of an article submission.

SWAP BLOCKS

This month’s challenge is to swap two adjacent blocks of memory using a finite amount of temporary swap space. This is something the Memory Manager has to do quite often as it shuffles blocks around in the heap.

The prototype of the function you write is:


/* 1 */
void SwapBlocks(p1, p2, swapPtr size1,
  size2, swapSize)
void    *p1;
void    *p2;
void    *swapPtr;
unsigned long  size1;
unsigned long  size2;
unsigned long  swapSize;

p1 and p2 point to the beginnings of the two blocks to swap. size1 and size2 are their respective sizes (in bytes). Both blocks begin on addresses divisible by 4 and have sizes that are divisible by 4. swapPtr points to the scratch area you can use (if you need to) and swapSize is the size of that area (between 256 and 4096 bytes, inclusive). swapPtr and swapSize are also each divisible by 4. If the two blocks look like this on entry:


/* 2 */
12345678ABCDEFGHIJKL
^       ^
p1      p2    size1 = 8   size2 = 12

then the same memory locations will look like this on exit:


/* 3 */
ABCDEFGHIJKL12345678

When measuring performance I will be calling your routine many times. The distribution of the sizes of the blocks is as follows:

4 to 16 bytes 20% of the time

20 to 32 bytes 20% of the time

36 to 64 bytes 20% of the time

68 to 256 bytes 20% of the time

260 to 4096 bytes 10% of the time

4100 or more bytes 10% of the time

You would normally write this kind of routine in assembly, but let’s see how well you can do in pure C (remember, everyone has the same handicap). If you want to submit a pure assembly solution along with your pure C solution then please do so (but the assembly version will NOT be counted as an entry in the challenge and it will not win anything other than a mention in this column).

TWO MONTHS AGO WINNER

Of the 11 entries I received for the We Pry Any Heap (Happy New Year) anagram challenge, only 5 worked correctly. Congrats to Larry Landry (Rochester, NY) for the dual honor of coming in 1st both in terms of speed and smallest code size.

The times for anagramming “programmer” (462 anagrams) and “mactech magazine” (3365 anagrams) with a 19,335 word English dictionary are given here (more weight was given to longer inputs (15-30 characters) when ranking contestants). Numbers in parens after a person’s name indicate how many times that person has finished in the top 5 places of all previous Programmer Challenges, not including this one:

Name code time 1 time 2

Larry Landry (1) 830 20 1048

Stepan Riha (5) 2352 45 1166

Bob Boonstra (6) 1370 52 1688

Allen Stenger (3) 1044 23 1701

Mark Nagel 1134 81 51407

Most of the entrants figured out that the key to speeding up the anagram process was to pare down the size of the dictionary first. Once you have the input characters you can eliminate any word in the dictionary that: (1) contains more characters than the input, (2) contains at least one letter not in the input set or, (3) contains more of any particular character than the input. For instance, if your input is “programmer” then you can remove any word in the dictionary that (1) is more than 10 characters long, (2) is not made up entirely of the letters [p, r, o, g, a, m, e] and, (3) contains more than any one of: 1 p, 3 r’s, 1 o, 1 g, 1 a, 2 m’s, or 1 e.

Stepan Riha (Austin, TX) took this “reduce the dictionary” idea one step further and came up with a way to store words that are permutations of each other (like ‘stop’, ‘post’ and ‘pots’) as one entry in the dictionary (and when it’s time to output an anagram he outputs all permutations for each word in the output).

Several people wrote to me and asked if reordering the words in each output anagram was necessary (i.e. ‘pale rain’ and ‘rain pale’). I admit that it wasn’t clear in the puzzle specification exactly what qualified as a ‘unique’ anagram so I allowed either interpretation. The only one of the 5 correct entries that did count word reorderings as unique anagrams is Mark Nagel (Irvine, CA) and his times above reflect that fact.

Here’s Larry’s winning solution:

Anagram Programmer's Challenge

by Larry Landry

This implementation uses a large amount of memory to optimize the CPU utilization. To guarantee that we have enough memory for all matching words, we actually allocate an array of pointers for 30,000 words. Since the rules stated that there would be about 20,000 words in the dictionary, even if every word matched, we would still have enough storage. In reality this number could probably be less than 1-200 for all but the most rare of scenarios.

The basic algorithm is: 1) Convert the input string into a table of counts for each character from a-z. So "sammy" would have a count of 2 for "m" and 1 for each of "s", "a", and "y". This makes testing for the presence of a character as simple as checking and indexed value in an array. 2) Parse through the dictionary and find the words that can be composed of some portion of characters from the input characters. Build a list of pointers to each word. The number of words in this list will be in the tens instead of thousands. 3) Recursively process the words in this list and find strings of words that use up all of the characters. For each matching sequence, output the words to the file. The processing required by this algorithm is then

D * C1 + M * log2(M) * C2

where

D = size of input dictionary

M = number of matching words

C1 & C2 are constants

This algorithm works very well for cases where there are few words that match the input letters. The worst case scenario where all words can be made from the input letters will still take a very long time. I expect that matching words will typically be less than 100.


/* 4 */
#include   <stdio.h>

typedef unsigned char   uchar;
typedef unsigned short ushort;
typedef unsigned long  ulong;

#define MAX_WORDS   30000L
#define OUTPUT_BUFFER_SIZE  10000L
#define RETURN  '\n'

typedef struct {
   char*fWordStart;
   short   fWordLength;
} WordLoc;

/* Usage counts for each character (only indexes 'a' to 'z' are actually 
used) */
typedef uchar   CharData[256];

unsigned long Anagram(Str255 inputText, FILE *wordList,
   FILE *outputFile);

ulong findInputWords(register char *wordBuffer,
   WordLoc *validWords);
ulong findAnagrams(short numValidChars, ulong wordCount,
   WordLoc *validWords, short prevWordCount);

/* I use some global variables here to avoid passing them down into the 
recursive routine findAnagrams().  These values are constant once findAnagrams() 
is called. */

char     gOutputBuffer[OUTPUT_BUFFER_SIZE];
char    *gOutputBufferEnd = gOutputBuffer + 
 OUTPUT_BUFFER_SIZE - 512;
char    *gOutputPtr;
CharData  gValidChars;
WordLoc  *gWordsInUse[255];
FILE    *gOutputFile;

unsigned long Anagram(Str255 inputText, FILE *wordList,
   FILE *outputFile)
{
   fpos_t  wordBufferLength;
   char*  wordBuffer;
   short   index;
   short   numValidChars;
   WordLoc validWords[MAX_WORDS];
   char   ch;
   ulong   wordCount;

   gOutputFile = outputFile;
   gOutputPtr = gOutputBuffer;

/* To save on file I/O time, read the whole file all at once.  First, 
find the length of the file by seeking the end and finding the file pos. 
 Then allocate a buffer of that size, plus 2 bytes  (for a return and 
NULL char) and read the data into it.  Finally put the return and NULL 
char at the end. */

   fseek(wordList, 0L, SEEK_END);
   fgetpos(wordList, &wordBufferLength);
   wordBuffer = (char*) NewPtr((Size) wordBufferLength + 2);
   if (wordBuffer == NULL)
   return 0L;  /* real error handling here */
   rewind(wordList);
   fread(wordBuffer, (size_t) 1,
   (size_t) wordBufferLength, wordList);
   if (wordBuffer[wordBufferLength-1] != RETURN)
   wordBuffer[wordBufferLength++] = RETURN;
   wordBuffer[wordBufferLength] = '\0';

/* To save time ruling out words, we build a list of the valid characters 
in the words.  We start with no valid characters. */
   for (index='a'; index<'z'; index++)
   gValidChars[index] = 0;

/* Now build the list of valid characters.  Each array entry will be 
a count of how many times that character is present. */
   numValidChars = *inputText++;
   for (index=numValidChars; index>0; index--)
   if ((ch = *inputText++) != ' ')
   gValidChars[ch]++;
   else
   numValidChars--;
/* Find the list of words that can be made up from the letters in the 
input word */
   wordCount = findInputWords(wordBuffer,
 &validWords[MAX_WORDS-1]);
/* Now find the list of full anagrams that can be created from these 
words */
   wordCount = findAnagrams(numValidChars, wordCount,
   &validWords[MAX_WORDS-wordCount], 0);
/* Write the results to the output */
   *gOutputPtr = 0;/* Terminate the string */
   fprintf(outputFile, gOutputBuffer);
   DisposPtr(wordBuffer);
   return wordCount;
} /* Anagram */


ulong findInputWords(register char *wordBuffer,
   WordLoc *validWords)
{
   char*saveStart = wordBuffer;
   ulong   numberWords = 0;
   char ch;

   while (*wordBuffer)
   {
   ch = *wordBuffer++;
   if (ch == RETURN)
   {
/* Record this entry as a valid word */
   numberWords++;
   validWords->fWordStart = saveStart;
   validWords->fWordLength = (short)(wordBuffer -
   saveStart - 1);
   validWords--;

   wordBuffer--;
   while (saveStart < wordBuffer)
 gValidChars[*saveStart++]++;

/* Save the new start of word pointer */
   saveStart = ++wordBuffer;
   } else if (gValidChars[ch])
   gValidChars[ch]--;
   else
   {
/* This word didn't match so reset and go to the next word */
   wordBuffer--;
   while (saveStart < wordBuffer)
   gValidChars[*saveStart++]++;
   while (*wordBuffer++ != RETURN)
   ;
/* Save the new start of word pointer */
   saveStart = wordBuffer;
   } /* else */
   } /* while */
   return numberWords;
} /* findInputWords */


ulong findAnagrams(short numValidChars, ulong wordCount,
   WordLoc *validWords, short prevWordCount)
{
   ulong   wordIndex;
   ulong   usedIndex;
   short   chIndex;
   ulong   matchCount = 0;
   Boolean wordFits;
   char ch;
   char*tempPtr;
   WordLoc *theWord;



/* Try each word we have against the list of characters. */
   for (wordIndex=0; wordIndex<wordCount; wordIndex++)
   {
/* If there aren't enough characters left,  it can't be a match */
   if (validWords->fWordLength <= numValidChars)
   {
/* Go through the chars in this word testing to make sure that there 
is at least one of each char  available */
   wordFits = TRUE;
   for (chIndex=0; chIndex<validWords->fWordLength; chIndex++)
   {
   ch = validWords->fWordStart[chIndex];
   if (gValidChars[ch])
   gValidChars[ch]--;
   else
   {
/* Found an unavailable character, so this can't be part of the anagram. 
 Reset the character usage array and go to the next word. */
   wordFits = FALSE;
   while (--chIndex >= 0)
   gValidChars[validWords->fWordStart[chIndex]]++;
   break;  /* get out of the for loop */
   } /* else */
   } /* for */

   if (wordFits)
   {
/* This word fit, so see if it uses all the characters.   If so, then 
we have found an anagram.  Output the  anagram and increment the anagram 
count. */
   if (validWords->fWordLength == numValidChars)
   {
   matchCount++;
/* Copy the previous words for this anagram separated by spaces. */
   for (usedIndex=0; usedIndex<prevWordCount; usedIndex++)
   {
   theWord = gWordsInUse[usedIndex];
   memcpy(gOutputPtr, theWord->fWordStart,
   (size_t) theWord->fWordLength);
   gOutputPtr += theWord->fWordLength;
   *gOutputPtr++ = ' ';
   } /* for */
/* Now copy this new word and a return character */
   memcpy(gOutputPtr, validWords->fWordStart,
   (size_t) validWords->fWordLength);
   gOutputPtr += validWords->fWordLength;
   *gOutputPtr++ = RETURN;

/* To ensure that we don't overrun the output buffer check against the 
end of the buffer.  If the end pointer has been passed, write the data 
to the file  and reset the output pointer to the beginning of the buffer. 
*/
   if (gOutputPtr > gOutputBufferEnd)
   {
 *gOutputPtr = 0;/* Terminate the string */
   fprintf(gOutputFile, gOutputBuffer);
   gOutputPtr = gOutputBuffer;
   } /* if */
   }  /* if */
   else
   {
/* This word did fit, but didn't use all of the characters so add it 
to the list of previous words  in the anagram and then call this procedure 
recursively to find if there are more words that can be added to make 
an anagram with this base. */
   gWordsInUse[prevWordCount] = validWords;
   matchCount += findAnagrams(
   numValidChars - validWords->fWordLength,
   wordCount - wordIndex, validWords,
   prevWordCount + 1);
   } /* else */

/* Now undo the characters we took out of the validChar array */
   for (chIndex=0;chIndex<validWords->fWordLength;chIndex++)
   gValidChars[validWords->fWordStart[chIndex]]++;
   } /* if */
   } /* if */

  validWords++;
 } /* for */

   return matchCount;
} /* findAnagrams */







  
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.