fork download
  1. # Stack solution of Codechef's COMPILER
  2. # Problem: https://w...content-available-to-author-only...f.com/problems/COMPILER
  3.  
  4. t = int(input())
  5. for _ in range(t):
  6. expression = input()
  7.  
  8. ans, stack = 0, []
  9. for idx, char in enumerate(expression):
  10. if char == '<':
  11. # push in stack
  12. stack.append(char)
  13.  
  14. elif char == '>':
  15. # if string is invalid break
  16. if not stack:
  17. break
  18.  
  19. stack.pop()
  20. # if string is valid upto index idx
  21. # calculate answer
  22. if not stack:
  23. ans = max(idx + 1, ans)
  24.  
  25. print(ans)
Runtime error #stdin #stdout #stderr 0.01s 7480KB
stdin
5
<<>>
><
<>>>
<>>
<><<<>>>>
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 6, in <module>
  File "<string>", line 1
    <<>>
     ^
SyntaxError: invalid syntax