<p>Given a string <code>s</code> representing a valid expression, implement a basic calculator to evaluate it, and return <em>the result of the evaluation</em>.</p>

<p><strong>Note:</strong> You are <strong>not</strong> allowed to use any built-in function which evaluates strings as mathematical expressions, such as <code>eval()</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;1 + 1&quot;
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot; 2-1 + 2 &quot;
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;(1+(4+5+2)-3)+(6+8)&quot;
<strong>Output:</strong> 23
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
	<li><code>s</code> consists of digits, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, and <code>&#39; &#39;</code>.</li>
	<li><code>s</code> represents a valid expression.</li>
	<li><code>&#39;+&#39;</code> is <strong>not</strong> used as a unary operation (i.e., <code>&quot;+1&quot;</code> and <code>&quot;+(2 + 3)&quot;</code> is invalid).</li>
	<li><code>&#39;-&#39;</code> could be used as a unary operation (i.e., <code>&quot;-1&quot;</code> and <code>&quot;-(2 + 3)&quot;</code> is valid).</li>
	<li>There will be no two consecutive operators in the input.</li>
	<li>Every number and running calculation will fit in a signed 32-bit integer.</li>
</ul>

def is_digit(s, i):
    try:
        int(s[i])
        return True
    except ValueError:
        return False


def need_prepend_plus(s, i):
    if i == 0 and s[i] == "(":
        return True
    if is_digit(s, i):
        if i == 0 or s[i - 1] == "(":
            return True
    return False


def prepend_plus(s):
    list_ = []
    for i in range(len(s)):
        if need_prepend_plus(s, i):
            list_.append(i)

    if len(list_) == 0:
        return s

    #print('here', list_)
    s_ = s[:list_[0]]
    i = list_[0]
    for j in list_[1:]:
        #print(s_, i, j)
        s_ += "+"
        s_ += s[i:j]
        i = j
    s_ += "+" + s[list_[-1]:]
    return s_


def split_into_list(s):
    ret = []
    for i in range(len(s)):
        ret.append(s[i])

    digit_start_end = []
    i = 0
    while i < len(s):
        start = None
        if is_digit(s, i):
            start = i
        while i < len(s) and is_digit(s, i):
            i += 1

        if start is not None:
            digit_start_end.append((start, i))
        i += 1

    #print(ret)
    #print(digit_start_end)
    for (start, end) in reversed(digit_start_end):
        num = int(s[start:end])
        for _ in range(end-start-1):
            del ret[start]
        #print(ret, start, num)
        ret[start] = num
    return ret


def get_sign(c):
    if c == "+":
        return 1
    elif c == "-":
        return -1
    else:
        raise ValueError(f"Got {c}")


def get_num_from_list(sub_list):
    if len(sub_list) == 1:
        return sub_list[0]

    val = 0
    #print('n', sub_list)
    assert len(sub_list) % 2 == 0
    for i in range(len(sub_list) // 2):
        sign_char = sub_list[2 * i]
        raw_val = sub_list[2 * i + 1]
        val += get_sign(sign_char) * raw_val
    return val


def solve_(list_):
    stack = []
    i = 0
    num_brackets = 0

    while i < len(list_):
        c = list_[i]
        if c != ")":
            stack.append(c)
        # eg (+3-42)
        else:
            sub_list = []
            while True:
                val = stack.pop()
                if val == "(":
                    break
                sub_list.append(val)
            # print(sub_list)
            sub_list.reverse()  # 24-3+ -> +3-42
            val = get_num_from_list(sub_list)
            stack.append(val)
        #print(i, stack)
        i += 1
    #print(stack, num_brackets)
    return get_num_from_list(stack)


def solution(s):
    s = s.replace(" ", "")
    s = prepend_plus(s)
    #print(s)
    s = split_into_list(s)
    #print(s)
    return solve_(s)



class Solution:
    def calculate(self, s: str) -> int:
        return solution(s)
        
30918