Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function brackets (expr) that takes a string consisting only of the symbols (, ), (. . [ and ] an analyzes the string
Write a function brackets (expr) that takes a string consisting only of the symbols (, ), (. . [ and ] an analyzes the string to make sure that the brackets are all "balanced" (matched) properly. For example the string 001' and(OO)) contain balanced brackets, whereas OO) ' and'(' are unbalanced We refer to (, { and [ as..left brackets", and ).) and ] as right brackets". The basic algorithm to implement works like this: input: a string S output: a list L for each character in S if the character is a left bracket else if the character is a right bracket, but L is empty else if the character is a right bracket and append the character to L return the string 'error the rightmost element of L is a matching left bracket delete the rightmost element of L (because we have a match) else we must have a mismatch, so return L Note that if the original string is properly balanced, the return value should be an empty list. Examples Function Call brackets ((()) brackets'((L (O)])) brackets'[ brackets (' [') brackets[) brackets())[[)]' brackets('(O[ [] (C]] ) brackets(') Return Value error' brackets TOOTLOR brackets('[(O) brackets('([ ([])) brackets('[] ) [(([]))) brackets (' { [ ( [ ] , ) brackets(0 []) [i ') brackets((O (CO)' brackets((E(CC)]))) )') brackets([ ]O') brackets((CE)' brackets C error' bracketstt1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started