vsem

brackerts

Dec 1st, 2021
572
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # You are given an expression with numbers, brackets and operators. For this task only the brackets matter.
  2. # Brackets come in three flavors: "{}" "()" or "[]".
  3. # Brackets are used to determine scope or to restrict some expression.
  4. # If a bracket is open, then it must be closed with a closing bracket of the same type.
  5. # The scope of a bracket must not intersected by another bracket.
  6. # In this task you should make a decision, whether to correct an expression or not based on the brackets. Do not worry about o
  7. # Do not worry about operators and operands.
  8. # **Input:**An expression with different of types brackets as a string (unicode).
  9. # **Output:**A verdict on the correctness of the expression in boolean (True or False).
  10. # *Example:*
  11. # checkio("((5+3)*2+1)") == True
  12. # checkio("{[(3+1)+2]+}") == True
  13. # checkio("(3+{1-1)}") == False
  14. # checkio("[1+1]+(2*2)-{3/3}") == True
  15. # checkio("(({[(((1)-2)+3)-3]/3}-3)") == False
  16. # checkio("2+3") == True
  17.  
  18.  
  19. def get_brackets(string):
  20.     string = "".join(item for item in string if item in "()[]{}")
  21.     brackets = ["()", "[]", "{}"]
  22.  
  23.     while any(item in string for item in brackets):
  24.  
  25.         for item in brackets:
  26.             if item in string:
  27.                 string = string.replace(item, "")
  28.    
  29.     return len(string) == 0
  30.  
  31.  
  32. print(get_brackets('("((5+3)*2+1)")'))
  33.  
RAW Paste Data