Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Toy Problem of the Week

Name: Anonymous 2009-12-12 18:22

I was raking around on the net and found this problem and coded up a solution. Once we get at least 10 solutions I'll post mine and a link to where I found the problem.
Any language, any libraries (but declare them) allowed. Aim for clarity and conciseness. Assume the input is well formed. Mine is 5 lines of scheme code (6 if we include requiring a library). No calling an "answer" function that "you wrote already and put in an external lib" ;)

Consider the problem of turning a list consisting
of a mix between symbols and non-symbols into a
list of lists of the symbol and its following
non-symbols. That is:

Input:    ({<symbol> <non-symbol>*} ... )
Output:   ((<symbol> (<non-symbol>*)) ...)
Example:     (a 1 2 3 b 4 5 c d 8 9 e)
           -> ((a (1 2 3)) (b (4 5)) (c ()) (d (8 9)) (e ()))

Name: >>13 2009-12-12 22:30

>>33
Why do you think strings are stored as linked lists? That would be terribly inefficient.

Given OP's syntax, I'm mostly assuming Lisp/Scheme meaning of symbol/non-symbol, which would mean the following, in C-like languages:
symbol - an object/structure, usually referred by a pointer, the structure has a name as one of it's fields. If you want to simplify this, you're free to use null terminated strings
non-symbols - other types of objects, since it wasn't specified what it means. If you want to simplify this requirement, you could say they're numbers, even though that would probably be different from what OP asked.
a list - linked list: structure of 2 elements, first is a pointer to the object, the second is a pointer to the rest of the list. A symbol called NIL, or empty list object '() can be used to represent the empty list. If you want to simplify this in C, you could use a NULL or 0.

At least the meanings I assigned to the terms used in OP's problem, but maybe I'm reading too much into it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List