def check_parentheses(text):
openBrackets = []
closeBrackets = []
pairs = []
for char in range(len(text)):
if text[char] == '(':
openBrackets.append(char)
elif text[char] == ')':
closeBrackets.append(char)
for o in reversed(range(len(openBrackets))):
for c in range(len(closeBrackets)):
if openBrackets[o] < closeBrackets[c]:
pairs.append(openBrackets[o])
pairs.append(closeBrackets[c])
closeBrackets.remove(closeBrackets[c])
break
listText = list(text)
for c in range(len(listText)):
if (listText[c] == '(' or listText[c] == ')') and c not in pairs:
listText.insert(c, '**')
listText.insert(c+2, '**')
break
return ''.join(listText)
print(check_parentheses(')(asdf)))'))
print(check_parentheses('((((asdf)))'))
print(check_parentheses('((((asdf))'))
print(check_parentheses('(ab)((cd)(asdf)))'))
print(check_parentheses('(ab)((cd)(asdf)())'))
print(check_parentheses('(ab)(((cd)(asdf)'))
print(check_parentheses('(ab)(((cd)(asdf'))
print(check_parentheses('(ab)(((cd)(asdf)))))'))