Rich Text Tokens

Asked at FigmaOnsite

Reported by the problem's creator, not verified

A rich-text editor stores a document as a flat stream of tokens and rebuilds its nesting when it loads the document. Implement the class TokenReader with the methods below; each method is called on fresh inputs and every call's return value is checked.

class TokenReader:
    def __init__(self) -> None: ...
    def is_well_formed(self, tokens: list[str]) -> bool: ...

In is_well_formed, each token is a string.

Examples

  1. Example 1

    Input: init = [], operations = [["is_well_formed",[[]]],["is_well_formed",[["hello"]]],["is_well_formed",[["INDENT","a","DEDENT"]]],["is_well_formed",[["INDENT","INDENT","a","DEDENT","b","DEDENT"]]],["is_well_formed",[["INDENT","DEDENT","INDENT","DEDENT"]]],["is_well_formed",[["DEDENT","INDENT"]]],["is_well_formed",[["INDENT"]]]]

    Output: [true,true,true,true,true,false,false]

    Explanation: An empty stream and a stream of only text open no levels, so both are well formed. ["INDENT", "a", "DEDENT"] opens and closes one level. In ["DEDENT", "INDENT"] the dedent arrives while no level is open, so the stream is not well formed even though the counts of INDENT and DEDENT are equal. ["INDENT"] leaves a level open at the end.

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.