tree from string

-1

1

A string like this one:

"a, 4.25, ((true, 5), 3, (false, false, false)), abc"

Describes a tree with 13 nodes, 9 of which are leaf nodes. In C# the leaf nodes in this example would be of types string, float and bool, this differs between languages and depending on your implementation. The important structure forming symbols here are , ( and ) with spaces being ignored.

See the diagram in David Carrahers answer for a tree of the desired shape.

var a = new cake(4, "abc")

The above contains 8 symbols, I'm counting bracket and " pairs as a half and things like . as 1.

The goal is to write code with the fewest symbols which given a string like the one shown above, creates the tree it represents.

For example in the form of arrays inside each other, or interconnecting node objects.

60 extra points if you don't sort of cheat by adjusting the input string slightly then executing it.

I won't count the symbols that define the input string.

daniero
100 - (10 symbols)
+40 points for understanding numbers, strings, bools
    and any other basic types the language understands.
+20 points for using a reasonably general language.
150 total points

fejesjoco
100 - (est 37 symbols)
+20 points for it understanding numbers as well as strings.
+40 points for using a reasonably general language.
97 total points

tomas
100 - (10 symbols)
90 total points

David Carraher
100 - (est 19 symbols)
81 total points

alan2here

Posted 2014-02-07T10:02:21.337

Reputation: 265

2Looks like homework, basic tokenizing as part of writing a script interpreter. – None – 2014-02-07T11:49:06.877

Please define the exact syntax of the string. – fejesjoco – 2014-02-07T13:00:21.840

I only see 12 nodes, can you explain it a bit better? – Teun Pronk – 2014-02-07T13:56:58.463

@TeunPronk I guess the 13th is the root node. – Joel Bosveld – 2014-02-07T14:27:05.563

The 13th node is the root, although this dosn't matter greatly. It's not homework or a part of a course. – alan2here – 2014-02-07T17:11:55.617

Why don't you give examples of the output you are expecting? – DavidC – 2014-02-08T02:31:24.253

*"[...] creates the tree it represents."* There are a lot of ways to form a tree out of a list. What defines a token (i.e. are your stings comma and space delimited, and why two delimitters)? What imposes structure on the tree (i.e are the parenthesis intended to indicate sub-trees)? Is the tree expected to be binary? This is very weakly specified, and consequently is not going to generate a very interesting set of responses. You can get help with these kinds of problems in chat or by posting to one of the meta sand-boxes.

– dmckee --- ex-moderator kitten – 2014-02-08T14:54:02.830

The question now has more detail. Now I'd like to see your solution dmckee :) – alan2here – 2014-02-08T16:41:43.983

How is "adjusting input string" cheating? How is using symbols a bad thing? I don't understand the purpose of this challenge any more. It started as a good question but then you spoiled it with nonsense - and changed rules on the run! Retracting my upvote and -1! – Tomas – 2014-02-09T14:32:48.303

Symbols is equivalent to characters, see the description for the atomic-code-golf tag, most challenges here are for short code. And a solution like "s.toTree();" where "s" is the input string is not really a solution. – alan2here – 2014-02-09T21:47:40.443

Answers

3

JavaScript

s="a, 4.25, ((true, 5), 3, (false, false, false)), abc"
eval(("["+s+"]").replace(/\(/g,"[").replace(/\)/g,"]").replace(/([a-z]+)/g,"'$1'"))

The exact syntax, input and output format weren't exactly defined... I replace every a-z character to become a string, even true/false, which could be booleans, but there's no indication if there should be a distinction. I don't know if other characters are allowed or not (ő, $, \uFA20, ...), so my code doesn't care about that either.

fejesjoco

Posted 2014-02-07T10:02:21.337

Reputation: 5 174

I tried it in the Firebug console in Firefox. Also works in the developer console of IE. – fejesjoco – 2014-02-07T17:31:06.160

The result is a recursive array: ["a", 4.25, [["true", 5], 3, ["false", "false", "false"]], "abc"] – fejesjoco – 2014-02-07T17:39:15.200

It may be bit of cheating, but it's the most straightforward way I can imagine. Writing a parser is a big task, but many languages have eval-like functions, so there's a reusable parser right there. – fejesjoco – 2014-02-07T17:43:18.393

Oh, you replace some of the brackets in the string with another type then execute the string :P Impressive all the same :) You may win, particularly if no less cheaty solutions are posted. – alan2here – 2014-02-07T17:49:27.900

2

Mathematica

StringReplace replaces all instances of ( with List[; instances of ) with ].

The FullForm of List must be used (instead of {…}); otherwise ToExpression will not parse the string correctly.

f@t_:=ToExpression["List["<>StringReplace[t,{"("->"List[",")"->"]"}]<>"]"]

Example

f["a, 4.25, ((true, 5), 3, (false, false, false)), abc"]

{a, 4.25, {{true, 5}, 3, {false, false, false}}, abc}


Verification

TreeForm[<expression>] displays the Mathematica expression as a tree.

TreeForm[f["a, 4.25, ((true, 5), 3, (false, false, false)), abc"]]

tree form

DavidC

Posted 2014-02-07T10:02:21.337

Reputation: 24 524

2

R, 36 chars

plot(read.tree(,paste0("(",a,");")))

package ape must be loaded.

Output:

enter image description here

Tomas

Posted 2014-02-07T10:02:21.337

Reputation: 2 333

2

Common Lisp

I think it would count as something around 11 symbols?

Wrap parentheses around the string, remove the commas and evaluate it, and you have a LISP list.

(read-from-string (remove #\, (concatenate 'string "(" s ")" )))

s is the string.

For the given example it returns a list (A 4.25 ((TRUE 5) 3 (FALSE FALSE FALSE)) ABC)

daniero

Posted 2014-02-07T10:02:21.337

Reputation: 17 193

1For those not familiar with lisp, it would be helpful to know that the internal representation of s-expressions is a tree. By striping the commas daniero has rendered the input into a s-expression. Then he just internalizes it and ::Bingo!:: – dmckee --- ex-moderator kitten – 2014-02-08T18:53:38.217