So, the golf is, write this as Perl. The concept is basically, you have a string representing the characters in a character class, and you need to invert that class (ignoring the NULL character).char *complement (char *set) { int limit = 255; char *table = (char *) malloc((1+limit) * sizeof(char)); /* FIXED * +/ int i; /* populate table with all non-null characters */ for (i = 0; i < limit; i++) table[i] = i+1; table[limit] = 0; /* for characters in 'set', make their value in the table NULL */ while (*set) table[*set++ - 1] = 0; /* now, swap NULLs in the table with the last element in the table, to condense the table */ for (i = 0; i < limit; i++) if (! table[i]) table[i] = table[--limit], table[limit] = 0; return table; }
UPDATE: thanks to blokhead, I'm correcting my answer. I optimized and failed to test my optimization. My function body is 39 characters long.
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Golf: string complement
by kvale (Monsignor) on May 15, 2004 at 15:52 UTC | |
by japhy (Canon) on May 15, 2004 at 16:08 UTC | |
|
Re: Golf: string complement
by tachyon (Chancellor) on May 15, 2004 at 04:43 UTC | |
by japhy (Canon) on May 15, 2004 at 04:47 UTC | |
by tachyon (Chancellor) on May 15, 2004 at 05:05 UTC | |
by japhy (Canon) on May 15, 2004 at 05:07 UTC | |
by davido (Cardinal) on May 15, 2004 at 16:16 UTC | |
|
Re: Golf: string complement
by davido (Cardinal) on May 15, 2004 at 06:38 UTC | |
by cLive ;-) (Prior) on May 15, 2004 at 23:27 UTC | |
by BrowserUk (Patriarch) on May 15, 2004 at 23:38 UTC | |
by cLive ;-) (Prior) on May 16, 2004 at 00:56 UTC | |
by BrowserUk (Patriarch) on May 16, 2004 at 01:23 UTC | |
| |
by japhy (Canon) on May 16, 2004 at 01:23 UTC | |
|
Re: Golf: string complement
by bsb (Priest) on May 29, 2004 at 05:38 UTC |