fork download
  1. def check_parentheses(text):
  2.  
  3. openBrackets = []
  4. closeBrackets = []
  5. pairs = []
  6.  
  7. for char in range(len(text)):
  8. if text[char] == '(':
  9. openBrackets.append(char)
  10. elif text[char] == ')':
  11. closeBrackets.append(char)
  12.  
  13. for o in reversed(range(len(openBrackets))):
  14. for c in range(len(closeBrackets)):
  15. if openBrackets[o] < closeBrackets[c]:
  16. pairs.append(openBrackets[o])
  17. pairs.append(closeBrackets[c])
  18. closeBrackets.remove(closeBrackets[c])
  19. break
  20.  
  21. listText = list(text)
  22.  
  23. for c in range(len(listText)):
  24. if (listText[c] == '(' or listText[c] == ')') and c not in pairs:
  25. listText.insert(c, '**')
  26. listText.insert(c+2, '**')
  27. break
  28.  
  29. return ''.join(listText)
  30.  
  31.  
  32. print(check_parentheses(')(asdf)))'))
  33. print(check_parentheses('((((asdf)))'))
  34. print(check_parentheses('((((asdf))'))
  35. print(check_parentheses('(ab)((cd)(asdf)))'))
  36. print(check_parentheses('(ab)((cd)(asdf)())'))
  37. print(check_parentheses('(ab)(((cd)(asdf)'))
  38. print(check_parentheses('(ab)(((cd)(asdf'))
  39. print(check_parentheses('(ab)(((cd)(asdf)))))'))
Success #stdin #stdout 0.01s 9992KB
stdin
Standard input is empty
stdout
**)**(asdf)))
**(**(((asdf)))
**(**(((asdf))
(ab)((cd)(asdf))**)**
(ab)((cd)(asdf)())
(ab)**(**((cd)(asdf)
(ab)**(**((cd)(asdf
(ab)(((cd)(asdf)))**)**)