Is it Well Formed?? (Problem 44) - InfyTQ Solution in Python
Is it Well Formed?? (Problem 44)
InfyTQ Solution in Python
Image by Cheezburger |
PROBLEM STATEMENT
UC Berkley CS61A Fall 2011 Final
The D33P language includes three types of tokens: open parentheses, close parentheses, and integers.
An expression is well-formed if it contains balanced parentheses, and each integer correctly indicates its depth: the number of nested sets of parentheses that surround that integer.
Implement correct depth, which takes a list of tokens as input and returns True if and only if a prefix of the input is a well-formed D33P expression.
Assume that the input contains a balanced set of nested parentheses with single-digit positive integers surrounded by parentheses.
You only need to check that the integers indicate the correct depths.
The D33P language includes three types of tokens: open parentheses, close parentheses, and integers.
An expression is well-formed if it contains balanced parentheses, and each integer correctly indicates its depth: the number of nested sets of parentheses that surround that integer.
Implement correct depth, which takes a list of tokens as input and returns True if and only if a prefix of the input is a well-formed D33P expression.
Assume that the input contains a balanced set of nested parentheses with single-digit positive integers surrounded by parentheses.
You only need to check that the integers indicate the correct depths.
Sample Input | Expected Output |
---|---|
list('(1)') | True |
list('(2)') | False |
list('((2)((3)))') | True |
list('((3)(2))') | False |
SOLUTION
SHARE
If you find this useful, please share with your friends and Community.
CODE TOGETHER..GROW TOGETHER.
CODE TOGETHER..GROW TOGETHER.
Newer Posts
Newer Posts
Older Posts
Older Posts
Comments