Toy Language Types

Asked at OpenAIOnsite

Reported by the problem's creator, not verified

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, T1 or S;
  • 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.

Examples

  1. Example 1

    Input: {"data":[["int","T1",["str",["T2"]]],["T1","float"]],"kind":"signature"}

    Output: (int,T1,[str,[T2]]) -> [T1,float]

    Explanation: The three parameters print as int, T1 and [str,[T2]], joined by commas inside parentheses, then ) -> and the result [T1,float].

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.