In this kata, you will be given a string of text and valid parentheses, such as `"h(el)lo"`. You must return the string, with only the text inside parentheses reversed, so `"h(el)lo"` becomes `"h(le)lo"`. However, if said parenthesized text contains parenthesized text itself, then that too must reversed back, so it faces the original direction. When parentheses are reversed, they should switch directions, so they remain syntactically correct (i.e. `"h((el)l)o"` becomes `"h(l(el))o"`). This pattern should repeat for however many layers of parentheses. There may be multiple groups of parentheses at any level (i.e. `"(1) (2 (3) (4))"`), so be sure to account for these.

For example:
```python
reverse_in_parentheses("h(el)lo") == "h(le)lo"
reverse_in_parentheses("a ((d e) c b)") == "a (b c (d e))"
reverse_in_parentheses("one (two (three) four)") == "one (ruof (three) owt)"
reverse_in_parentheses("one (ruof ((rht)ee) owt)") == "one (two ((thr)ee) four)"
```

Input parentheses will always be valid (i.e. you will never get "(()").

Difficulty: introductory

Starter code:

def reverse_in_parentheses(string):
	

Solutions:
def reverse_in_parentheses(s):
    stack = []
    for i in s:
        stack.append(i)
        
        if i == ')':
            opening = len(stack) - stack[::-1].index('(') - 1
            stack.append(''.join([i[::-1].translate(str.maketrans('()',')(')) for i in stack[opening:][::-1]]))  
            del stack[opening:-1]
  
    return ''.join(stack)  

def reverse_in_parentheses(string):
    a = 0
    for n in range(string.count("(")):
        a, b = string.find("(", a), 0
        for i in range(a, len(string)):
            if string[i] == "(": b += 1
            elif string[i] == ")": b -= 1
            
            if b == 0: break
        a += 1
        string = string[:a] + string[a:i][::-1].translate(str.maketrans(")(", "()")) + string[i:]
    return string

def reverse_in_parentheses(s):

    def find_pairs(s):
        stack, pairs = [], {}
        for i, c in enumerate(s):
            if c == '(': stack.append(i)
            if c == ')':
                opening = stack.pop()
                pairs[opening] = i
                pairs[i] = opening
        return pairs

    def walk(start, end, direction):
        while start != end:
            if s[start] not in '()':
                yield s[start]
            else:
                yield '('
                yield from walk(pairs[start]-direction, start, -direction)
                yield ')'
                start = pairs[start]
            start += direction
    
    pairs = find_pairs(s)
    return ''.join(walk(0, len(s), 1))

import itertools

def reverse_in_parentheses(string):
    stream = iter(string)
    return _recurse(stream)

def safe_rev(s):
    return s[::-1].translate(str.maketrans('()', ')('))

def _recurse(stream):
    ret = ''
    for c in stream:
        if c == '(':
            ret += c
            ret += safe_rev(_recurse(stream)) + ')'
        elif c == ')':
            break
        else:
            ret += c
    return ret


def reverse_in_parentheses(string):
    counter = 0
    res = [""]
    
    for i in string:
        
        if i == "(":
            res.append("")
            counter += 1
            
        elif i == ")":
            counter -= 1
            
            if counter % 2 == 0:
                res[counter] += "(" + res.pop() + ")"
            else:
                res[counter] = "(" + res.pop() + ")" + res[counter]            
        
        elif counter % 2 == 0:
            res[counter] += i
            
        else:
            res[counter] = i + res[counter]
            
    return res[0]

def reverse_in_parentheses(string):
    for i in range(len(string)):
        if string[i]=="(":
            finder,depth=i,1
            while depth>int():depth,finder=depth+")(".index(string[finder+1])-1+")(".index(string[finder+1]) if string[finder+1] in "()" else depth,finder+1
            string=string[:i+1]+"".join(["()"[")(".index(y)] if y in "()" else y for y in list(reversed(string[i+1:finder]))])+string[finder:]
    return string

def reverse_in_parentheses(string):
    for i in range(len(string)):
        if string[i]=="(":
            walker,depth=i,1
            while depth>int():depth,walker=depth+")(".index(string[walker+1])-1+")(".index(string[walker+1]) if string[walker+1] in "()" else depth,walker+1
            string=string[:i+1]+"".join(["()"[")(".index(y)] if y in "()" else y for y in list(reversed(string[i+1:walker]))])+string[walker:]
    return string

def reverse_in_parentheses(string):
    for i in range(len(string)):
        if string[i]=="(":
            counter,k=i,1
            while k>int():k,counter=k+")(".index(string[counter+1])-1+")(".index(string[counter+1]) if string[counter+1] in "()" else k,counter+1
            string=string[:i+1]+"".join(["()"[")(".index(y)] if y in "()" else y for y in list(reversed(string[i+1:counter]))])+string[counter:]
    return string

swap_parentheses = str.maketrans('()', ')(')

def reverse_in_parentheses(s):
    stack, res = [], list(s)
    for i, c in enumerate(s):
        if c == '(': stack.append(i)
        elif c == ')':
            j = stack.pop()
            res[j + 1:i] = ''.join(res[i - 1:j:-1]).translate(swap_parentheses)
    return ''.join(res)

def pairs(s):
    stack, pairs = [], {}
    for i, c in enumerate(s):
        if c == '(': stack.append(i)
        elif c == ')': pairs[stack.pop()] = i
    return pairs

swap_parentheses = str.maketrans('()', ')(')

def reverse_in_parentheses(s):
    res = list(s)
    for i, j in pairs(s).items():
        res[i + 1:j] = ''.join(res[j - 1:i:-1]).translate(swap_parentheses)
    return ''.join(res)