Toy Language Types
OpenAIOnsiteMedium
A type in a toy language is written in JSON form. A type is one of:
- a primitive: one of the strings
int,float,str; - a generic: a string starting with an uppercase letter, optionally followed by digits, such as
T,T1orS; - a tuple: a JSON list of types, possibly empty, possibly containing further tuples.
For example "int" is the primitive int, "T2" is the generic T2, and ["int", ["str", "T2"]] is a tuple of the primitive int and the tuple of str and the generic T2.
Implement one function describe(kind, data). The string kind selects what data holds and what is returned; every result is a string.
Part 1: Rendering types and signatures
Support two kinds.
"type":datais a type. Return its text. A name is written as itself. A tuple is written as[, then its element types' texts separated by commas, then], with no spaces anywhere."signature":datais[params, result], whereparamsis a list of types andresultis a type. Return(, the parameters' texts separated by commas,) ->, then the result's text. The only spaces are the ones around->.
- Example 1
- data
[["int","T1",["str",["T2"]]],["T1","float"]]- kind
"signature"- Output
"(int,T1,[str,[T2]]) -> [T1,float]"
Why: The three parameters print as
int,T1and[str,[T2]], joined by commas inside parentheses, then) ->and the result[T1,float].
Constraints
- The total number of names and tuples in
datais at most10^3 - Nesting depth is at most
100
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.