|
|
@ -206,53 +206,66 @@ def copyList(lst): |
|
|
|
# return line |
|
|
|
# return line |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def addPars(chars:list): |
|
|
|
def addPars(chars:list): |
|
|
|
# curDepth = 0 |
|
|
|
curDepth = 0 |
|
|
|
# addDepth = 0 |
|
|
|
addDepth = 0 |
|
|
|
|
|
|
|
|
|
|
|
# isSearching = False |
|
|
|
isSearching = False |
|
|
|
|
|
|
|
|
|
|
|
# offset = 0 |
|
|
|
offset = 0 |
|
|
|
# for i, char in enumerate(chars): |
|
|
|
for i, char in enumerate(chars): |
|
|
|
# if( char == "(" ): |
|
|
|
if( char == "(" ): |
|
|
|
# curDepth += 1 |
|
|
|
curDepth += 1 |
|
|
|
# elif( char == ")" ): |
|
|
|
elif( char == ")" ): |
|
|
|
# curDepth -= 1 |
|
|
|
curDepth -= 1 |
|
|
|
|
|
|
|
|
|
|
|
# if( char == "+" ): |
|
|
|
if( char == "+" ): |
|
|
|
# before = chars[i-1+offset] |
|
|
|
before = chars[i-1+offset] |
|
|
|
# if(before == ")" or before == "("): |
|
|
|
# if(before == ")" or before == "("): |
|
|
|
# chars.insert(i+offset, 0) |
|
|
|
# chars.insert(i+offset, "0") |
|
|
|
# offset += 1 |
|
|
|
# offset += 1 |
|
|
|
|
|
|
|
|
|
|
|
# curDepth2 = curDepth |
|
|
|
curDepthSec = curDepth |
|
|
|
# for di, char2 in enumerate(chars[i+1:]): |
|
|
|
for di, char2 in enumerate(chars[i+1:]): |
|
|
|
|
|
|
|
|
|
|
|
# if( char2 == "(" ): |
|
|
|
if( char2 == "(" ): |
|
|
|
# curDepth2 += 1 |
|
|
|
curDepthSec += 1 |
|
|
|
# elif( char2 == ")" ): |
|
|
|
continue |
|
|
|
# curDepth2 -= 1 |
|
|
|
elif( char2 == ")" ): |
|
|
|
|
|
|
|
curDepthSec -= 1 |
|
|
|
|
|
|
|
|
|
|
|
# print(f"{i=} : {di+1=} {char=} {char2=} {curDepth=}/{curDepth2=}") |
|
|
|
if( curDepthSec != curDepth ): |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
# if( char2 == "*" and curDepth == curDepth2 ): |
|
|
|
print(f"{i=} : {di+1+offset=} {char=} {char2=} {curDepth=} : {curDepthSec=}") |
|
|
|
# chars.insert(i+offset+di, ")") |
|
|
|
|
|
|
|
# offset += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( char2 == "*" and curDepth == curDepthSec ): |
|
|
|
|
|
|
|
chars.insert(i+offset+di, ")") |
|
|
|
|
|
|
|
offset += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # if( char == "+" and not isSearching ): |
|
|
|
|
|
|
|
# # chars.insert(i-1+offset, "(") |
|
|
|
|
|
|
|
# # addDepth = curDepth |
|
|
|
|
|
|
|
# # isSearching = True |
|
|
|
|
|
|
|
# # offset += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # elif( char == "*" and curDepth == addDepth ): |
|
|
|
# if( char == "+" and not isSearching ): |
|
|
|
# # chars.insert(i+offset, ")") |
|
|
|
# chars.insert(i-1+offset, "(") |
|
|
|
|
|
|
|
# addDepth = curDepth |
|
|
|
|
|
|
|
# isSearching = True |
|
|
|
|
|
|
|
# offset += 1 |
|
|
|
|
|
|
|
|
|
|
|
# return chars |
|
|
|
# elif( char == "*" and curDepth == addDepth ): |
|
|
|
|
|
|
|
# chars.insert(i+offset, ")") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return chars |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Fusk(int): |
|
|
|
|
|
|
|
def __init__(self, value): |
|
|
|
|
|
|
|
self.value = value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __mul__(self, other): |
|
|
|
|
|
|
|
return Fusk(self.value + other.value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __sub__(self, other): |
|
|
|
|
|
|
|
return Fusk(self.value * other.value) |
|
|
|
|
|
|
|
|
|
|
|
# addPars( strToList("2*3+(4*5)") ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parseMath2(line:str): |
|
|
|
def parseMath2(line:str): |
|
|
|
line = line.replace(" ", "") |
|
|
|
line = line.replace(" ", "") |
|
|
@ -285,13 +298,28 @@ def parseMath2(line:str): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# problem = mathproblems[0] |
|
|
|
# problem = mathproblems[0] |
|
|
|
|
|
|
|
# print(problem.replace(" ", "")) |
|
|
|
|
|
|
|
# newprob = problem.replace(" ", "") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# probList = addPars( strToList(newprob) ) |
|
|
|
|
|
|
|
# newprob = lstToStr(probList) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#print(newprob) |
|
|
|
# res = parseMath2(problem) |
|
|
|
# res = parseMath2(problem) |
|
|
|
# print("\n--##########################--") |
|
|
|
# print("\n--##########################--") |
|
|
|
# print( problem, "=", res ) |
|
|
|
# print( problem, "=", res ) |
|
|
|
|
|
|
|
|
|
|
|
# mathsum = 0 |
|
|
|
mathsum, mathsum2 = 0, 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for maththing in mathproblems: |
|
|
|
|
|
|
|
mathsum += parseMath(maththing) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = str.maketrans({"*": "-", "+": "*"}) |
|
|
|
|
|
|
|
trans = maththing.translate(x) |
|
|
|
|
|
|
|
print("##############", x, trans) |
|
|
|
|
|
|
|
|
|
|
|
# for maththing in mathproblems: |
|
|
|
maththing = re.sub( r"([0-9]+)", r"Fusk(\1)", trans ) |
|
|
|
# mathsum += parseMath(maththing) |
|
|
|
print(maththing) |
|
|
|
|
|
|
|
mathsum2 += eval(maththing) |
|
|
|
|
|
|
|
|
|
|
|
# print(mathsum) |
|
|
|
print(mathsum, mathsum2) |
|
|
|