In this Kata, you will be given a mathematical string and your task will be to remove all braces as follows:

```Haskell
solve("x-(y+z)") = "x-y-z"
solve("x-(y-z)") = "x-y+z"
solve("u-(v-w-(x+y))-z") = "u-v+w+x+y-z"
solve("x-(-y-z)") = "x+y+z"
```

There are no spaces in the expression. Only two operators are given: `"+" or "-"`. 

More examples in test cases. 

Good luck!

Difficulty: EASY
Tags: String algorithms

Starter code:
def solve(s):
	

Solutions:
from functools import reduce

def solve(st):
	(res, s, k) = ([], '', 1)
	for ch in st:
		if ch == '(':
			res.append(k)
			k = 1
		elif ch == ')':
			res.pop()
			k = 1
		elif ch == '-':
			k = -1
		elif ch == '+':
			k = 1
		else:
			s += '-' + ch if reduce(lambda a, b: a * b, res, 1) * (1 if k == 1 else -1) < 0 else '+' + ch
	return s if s[0] == '-' else s[1:]


SWAPPER = str.maketrans('+-', '-+')

def solve(s):
	return simplify(iter(s)).strip('+')

def simplify(it, prevOp='+'):
	lst = []
	for c in it:
		if c == '(':
			lst.append(simplify(it, '+' if not lst else lst.pop()))
		elif c == ')':
			break
		else:
			if not lst and c.isalpha():
				lst.append('+')
			lst.append(c)
	s = ''.join(lst)
	if prevOp == '-':
		s = s.translate(SWAPPER)
	return s


def negafy(s):
	(res, i) = ('', 0)
	while True:
		if i == len(s):
			break
		if 96 < ord(s[i]) and ord(s[i]) < 123:
			res += s[:i] + '-' + s[i]
			(s, i) = (s[i + 1:], -1)
		i += 1
	return res + s

def solve(s):
	while s[0] == '(' and s[-1] == ')':
		s = s[1:-1]
	while '--' in s:
		s = s.replace('--', '+')
	while '+-' in s:
		s = s.replace('+-', '-')
	while s.startswith('+'):
		s = s[1:]
	if '(' not in s:
		return s
	a = s.rindex('(')
	(b, c, d) = (s[a:].index(')') + a, s[a - 1], '')
	while a > 0 and b < len(s) - 1 and (s[a - 1] == '(') and (s[b + 1] == ')'):
		(a, b) = (a - 1, b + 1)
	if c == '-':
		d = negafy(s[a:b + 1])
	else:
		d = s[a - 1] + s[a + 1:b]
	return solve(s[:a - 1] + d + s[b + 1:])


def solve(s, p=1, q=1, Q=[], z=''):
	for c in s:
		if c == '+':
			p = 1
		elif c == '-':
			p = -1
		elif c == '(':
			q *= p
			Q += [p]
			p = 1
		elif c == ')':
			q *= Q.pop()
			p = 1
		else:
			z += '-+'[p * q > 0] + c
	return z.strip('+')


def solve(s):
	pos = [0]
	s = '(' + s + ')'

	def parse():
		sgn = -1 if s[pos[0]] == '-' else 1
		pos[0] += 2 if s[pos[0]] in ('+', '-') else 1
		lst = []
		while s[pos[0]] != ')':
			if '(' not in (s[pos[0]], s[pos[0] + 1]):
				lst.append((-1 if s[pos[0]] == '-' else 1, s[pos[0] + 1] if s[pos[0]] in ('+', '-') else s[pos[0]]))
				pos[0] += 2 if s[pos[0]] in ('+', '-') else 1
			else:
				lst.extend(parse())
		pos[0] += 1
		return [(t[0] * sgn, t[1]) for t in lst]
	return ''.join([('+' if t[0] == 1 else '-') + t[1] for t in parse()]).lstrip('+')


def solve(p):
	return '{}{}'.format('$', ''.join([['+', '-'][(len([j for j in range(len(p[:i])) if p[j:j + 2] == '-(' and (not len([k for k in range(len(p[:i])) if k > j and p[k] == ')' and (p[j:k].count(')') < p[j:k].count('('))]))]) + int(p[i - 1] == '-')) % 2] + c for (i, c) in enumerate(p) if c not in ['(', ')', '+', '-']])).replace('$+', '').replace('$', '')


def clean(string, to_clean=[('+-', '-'), ('-+', '-'), ('++', '+'), ('--', '+')]):
	cleaned_string = string
	for (i, j) in to_clean:
		while i in cleaned_string:
			cleaned_string = cleaned_string.replace(i, j)
	print((cleaned_string, string))
	return clean(cleaned_string) if cleaned_string != string else string

def solve(string):
	while '(' in string:
		counter = int()
		while counter < len(string):
			if string[counter] == '(':
				for x in range(counter + 1, len(string)):
					if string[x] == ')':
						(string, counter) = (string[:counter] + ''.join(['+-'['-+'.index(y)] if y in '-+' and string[counter - 1] == '-' and (string[counter + ind] not in '(-+') else y for (ind, y) in enumerate(string[counter + 1:x])]) + string[x + 1:], counter - 1)
					elif string[x] == '(':
						pass
					else:
						continue
					break
			counter += 1
			print(string)
	to_return = clean(string)
	return to_return[1:] if to_return[int()] == '+' else to_return


import re
from functools import reduce

def solve(s):
	(sign, stack) = ({('+', '-'): '-', ('-', '+'): '-', ('+', '+'): '+', ('-', '-'): '+'}, [])
	for i in s:
		if i != ')':
			stack.append(i)
		else:
			t = len(stack) - stack[::-1].index('(') - 1
			stack.pop(t)
			ini = stack[t - 1]
			if ini in '+-':
				stack[t:] = list(re.sub('(?<=\\w)([-+])(?=\\w)', lambda x: sign[ini, x.group(1)], ''.join(stack[t:])))
		stack = list(re.sub('([-+]+)', lambda g: reduce(lambda x, y: sign[x, y], list(g.group())), ''.join(stack)))
	return ''.join(stack).lstrip('+')


def solve(s):
	(sign, res, x) = ([], [], False)
	for c in s:
		if c in '()-+':
			if c == '(':
				sign.append(x)
			elif c == ')':
				del sign[-1]
			x = c == '-'
		else:
			res.append('+-'[sum(sign) % 2 ^ x])
			res.append(c)
	return ''.join(res).lstrip('+')


import re

class Expr:

	def __init__(self, terms):
		self.terms = terms

	def __add__(self, other):
		return Expr(self.terms + other.terms)

	def __sub__(self, other):
		return self + -other

	def __neg__(self):
		return Expr([(sign ^ 1, var) for (sign, var) in self.terms])

	def __str__(self):
		return ''.join(('+-'[sign] + var for (sign, var) in self.terms)).strip('+')

def solve(s):
	return str(eval(re.sub('(\\w+)', 'Expr([(0, "\\1")])', s)))