5. Balanced Brackets -> Python
Здравейте колеги,
имам затруднения с 5-та задача от допълнителните към темата Data Types and Variables. Ако може да ми дадете някоя насока в коя посока на мислене да тръгна?
Благодаря предварително, Диляна
1. Balanced Brackets
You will receive n lines. On those lines, you will receive one of the following:
- Opening bracket – “(“,
- Closing bracket – “)” or
- Random string
Your task is to find out if the brackets are balanced. That means after every closing bracket should follow an opening one. Nested parentheses are not valid, and if two consecutive opening brackets exist, the expression should be marked as unbalanced.
Input
- On the first line, you will receive n – the number of lines, which will follow
- On the next n lines, you will receive “(”, “)” or another string
Output
You have to print “BALANCED”, if the parentheses are balanced and “UNBALANCED” otherwise.
Constraints
- n will be in the interval [1…20]
- The length of the stings will be between [1…100] characters
Examples
Input |
Output |
|
Input |
Output |
8 ( 5 + 10 ) * 2 + ( 5 ) -12 |
BALANCED |
|
6 12 * ) 10 + 2 - ( 5 + 10 )
|
UNBALANCED |
Вашето решение е доста ясно, но не изпълнява едно условие и то е ,че не може първата скоба да е затваряща.
Добавих още 2 реда към вашия код.
Поздрави :)