|
|
|
@ -110,149 +110,184 @@ def lstToStr(lst): |
|
|
|
|
def copyList(lst): |
|
|
|
|
return [elem for elem in lst] |
|
|
|
|
|
|
|
|
|
# def addPars(line:str): |
|
|
|
|
# chars = strToList(line) |
|
|
|
|
# plusCount = chars.count("+") |
|
|
|
|
|
|
|
|
|
# seekingEnd = None |
|
|
|
|
# parsPos = dict() |
|
|
|
|
|
|
|
|
|
# print("#", line) |
|
|
|
|
# for i in range(len(chars)): |
|
|
|
|
# char = chars[i] |
|
|
|
|
# def addParsToSub(line:str): |
|
|
|
|
# # this function split the strings addition thing |
|
|
|
|
# # no parenthesis here |
|
|
|
|
|
|
|
|
|
# if(char == "+"): |
|
|
|
|
# parsPos[i-1] = None |
|
|
|
|
# seekingEnd = i-1 |
|
|
|
|
# addlines = line.split("*") # each element is an addition |
|
|
|
|
# #newline = "(" |
|
|
|
|
# newline = "" |
|
|
|
|
|
|
|
|
|
# if( (char == "*" and seekingEnd != None) ): |
|
|
|
|
# parsPos[seekingEnd] = i |
|
|
|
|
# seekingEnd = None |
|
|
|
|
# for i in range(len(addlines)): |
|
|
|
|
# l = addlines[i] |
|
|
|
|
# if( i < 1 ): |
|
|
|
|
# newline += f"({l})*" |
|
|
|
|
# elif( i >= 1 and i < len(addlines)-1 ): |
|
|
|
|
# newline += f"({l})*" |
|
|
|
|
# elif( i == len(addlines)-1 ): |
|
|
|
|
# newline += f"({l})" |
|
|
|
|
|
|
|
|
|
# if( seekingEnd != None and i >= len(chars) - 1 ): |
|
|
|
|
# parsPos[seekingEnd] = i+1 |
|
|
|
|
# seekingEnd = None |
|
|
|
|
# #newline += ")" |
|
|
|
|
|
|
|
|
|
# if( seekingEnd != None and char == ")" ): |
|
|
|
|
# parsPos[seekingEnd] = i+1 |
|
|
|
|
# seekingEnd = None |
|
|
|
|
# return newline |
|
|
|
|
|
|
|
|
|
# print(f"{i=}:{char=} {parsPos=}") |
|
|
|
|
|
|
|
|
|
# offset = 0 |
|
|
|
|
# needEnd = 0 |
|
|
|
|
# for pstart, pend in parsPos.items(): |
|
|
|
|
# print("#", pstart, pend) |
|
|
|
|
# def getLowestPar(par:dict): |
|
|
|
|
# bestdiff = None |
|
|
|
|
# bestkey = None |
|
|
|
|
|
|
|
|
|
# if(pend != None): |
|
|
|
|
# chars.insert(pstart+offset, "(") |
|
|
|
|
# offset += 1 |
|
|
|
|
# for key, val in par.items(): |
|
|
|
|
# diff = val - key |
|
|
|
|
|
|
|
|
|
# chars.insert(pend+offset, ")") |
|
|
|
|
# offset += 1 |
|
|
|
|
# else: |
|
|
|
|
# chars.insert(pstart+offset, 0) |
|
|
|
|
# offset += 1 |
|
|
|
|
# printMath(chars) |
|
|
|
|
# if( bestdiff == None ): |
|
|
|
|
# bestdiff = diff |
|
|
|
|
# bestkey = key |
|
|
|
|
# continue |
|
|
|
|
|
|
|
|
|
# chars.insert(pstart+offset, "+") |
|
|
|
|
# offset += 1 |
|
|
|
|
# printMath(chars) |
|
|
|
|
# if( diff > bestdiff ): |
|
|
|
|
# bestdiff = diff |
|
|
|
|
# bestkey = key |
|
|
|
|
# continue |
|
|
|
|
|
|
|
|
|
# chars.insert(pstart+offset-2, "(") |
|
|
|
|
# offset += 1 |
|
|
|
|
# printMath(chars) |
|
|
|
|
# return bestkey |
|
|
|
|
|
|
|
|
|
# needEnd += 1 |
|
|
|
|
|
|
|
|
|
# for i in range(needEnd): |
|
|
|
|
# chars.append(")") |
|
|
|
|
|
|
|
|
|
# print("RES", end=" ") |
|
|
|
|
# printMath(chars) |
|
|
|
|
# def addPars(line:str, usedPar=[]): |
|
|
|
|
# line = line.replace(" ", "") |
|
|
|
|
# newline = line |
|
|
|
|
# count = 0 |
|
|
|
|
|
|
|
|
|
# return chars |
|
|
|
|
# # Get pars etc |
|
|
|
|
# pars = findParens(line) |
|
|
|
|
# chars = strToList(line) |
|
|
|
|
|
|
|
|
|
# import ast |
|
|
|
|
# print(pars) |
|
|
|
|
|
|
|
|
|
# def recurse(node): |
|
|
|
|
# out = "" |
|
|
|
|
# if( isinstance(node, ast.Add) ): |
|
|
|
|
# # Get pars with lowest depth |
|
|
|
|
# start = getLowestPar(pars) |
|
|
|
|
# end = pars[start]+1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def addPars(line:str): |
|
|
|
|
# out = strToList(line) |
|
|
|
|
# # Enclose addition in pars |
|
|
|
|
# subline = lstToStr(chars[start:end]) |
|
|
|
|
# newsubline = addParsToSub(subline) |
|
|
|
|
|
|
|
|
|
# offset = 0 |
|
|
|
|
# parDepth = 0 |
|
|
|
|
# seekingClose = False |
|
|
|
|
# newline = newline.replace(subline, newsubline) |
|
|
|
|
|
|
|
|
|
# # for start, end in pars.items(): |
|
|
|
|
# # if( not start in usedPar ): |
|
|
|
|
# # x, y = start, end+1 |
|
|
|
|
# # subline = lstToStr(chars[x:y]) |
|
|
|
|
# # newsubline = addParsToSub(subline) |
|
|
|
|
|
|
|
|
|
# # newline.replace(subline, newsubline) |
|
|
|
|
# # print(f"{newsubline=}") |
|
|
|
|
|
|
|
|
|
# print("FINAL", newline) |
|
|
|
|
# print(subline, newsubline) |
|
|
|
|
# print(addParsToSub(newline)) |
|
|
|
|
|
|
|
|
|
# def addParsRec(line:str, usedPar=[]): |
|
|
|
|
# line = line.replace(" ", "") |
|
|
|
|
# newline = line |
|
|
|
|
|
|
|
|
|
# pars = findParens(newline) |
|
|
|
|
|
|
|
|
|
# startFound = None |
|
|
|
|
# endFound = None |
|
|
|
|
|
|
|
|
|
# for i in range(len(line)): |
|
|
|
|
# char = line[i] |
|
|
|
|
# prefix = "-----" |
|
|
|
|
# def addParsRec(line:str, i=0, it=None): |
|
|
|
|
# it = it or line.count("(") |
|
|
|
|
|
|
|
|
|
# if( char == "+" and not seekingClose ): |
|
|
|
|
# out.insert(i - 1 + offset, "(" ) |
|
|
|
|
# seekingClose = True |
|
|
|
|
# startFound = i-1+offset |
|
|
|
|
# offset += 1 |
|
|
|
|
# if( i < it ): |
|
|
|
|
# return addParsRec( addParsToSub(line), i+1, it ) |
|
|
|
|
# else: |
|
|
|
|
# return line |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# prefix = "start" |
|
|
|
|
# def addPars(chars:list): |
|
|
|
|
# curDepth = 0 |
|
|
|
|
# addDepth = 0 |
|
|
|
|
|
|
|
|
|
# elif( char == "(" ): |
|
|
|
|
# parDepth += 1 |
|
|
|
|
# isSearching = False |
|
|
|
|
|
|
|
|
|
# offset = 0 |
|
|
|
|
# for i, char in enumerate(chars): |
|
|
|
|
# if( char == "(" ): |
|
|
|
|
# curDepth += 1 |
|
|
|
|
# elif( char == ")" ): |
|
|
|
|
# parDepth -= 1 |
|
|
|
|
# curDepth -= 1 |
|
|
|
|
|
|
|
|
|
# elif( (char == "*") and seekingClose and parDepth == 0 ): |
|
|
|
|
# out.insert(i + offset, ")") |
|
|
|
|
# offset += 1 |
|
|
|
|
# seekingClose = False |
|
|
|
|
# endFound = i+offset |
|
|
|
|
# if( char == "+" ): |
|
|
|
|
# before = chars[i-1+offset] |
|
|
|
|
# if(before == ")" or before == "("): |
|
|
|
|
# chars.insert(i+offset, 0) |
|
|
|
|
# offset += 1 |
|
|
|
|
|
|
|
|
|
# prefix = "close" |
|
|
|
|
# curDepth2 = curDepth |
|
|
|
|
# for di, char2 in enumerate(chars[i+1:]): |
|
|
|
|
|
|
|
|
|
# if( char2 == "(" ): |
|
|
|
|
# curDepth2 += 1 |
|
|
|
|
# elif( char2 == ")" ): |
|
|
|
|
# curDepth2 -= 1 |
|
|
|
|
|
|
|
|
|
# print(f"{i=} : {di+1=} {char=} {char2=} {curDepth=}/{curDepth2=}") |
|
|
|
|
|
|
|
|
|
# math = lstToStr(out) |
|
|
|
|
# print(f"{prefix} {i=}:{char=} {math=} {offset=}") |
|
|
|
|
# if( char2 == "*" and curDepth == curDepth2 ): |
|
|
|
|
# chars.insert(i+offset+di, ")") |
|
|
|
|
# offset += 1 |
|
|
|
|
|
|
|
|
|
# return out |
|
|
|
|
|
|
|
|
|
def addParsToSub(line:str): |
|
|
|
|
# this function split the strings addition thing |
|
|
|
|
# no parenthesis here |
|
|
|
|
|
|
|
|
|
addlines = line.split("*") # each element is an addition |
|
|
|
|
newline = "" |
|
|
|
|
# # if( char == "+" and not isSearching ): |
|
|
|
|
# # chars.insert(i-1+offset, "(") |
|
|
|
|
# # addDepth = curDepth |
|
|
|
|
# # isSearching = True |
|
|
|
|
# # offset += 1 |
|
|
|
|
|
|
|
|
|
for i in range(len(addlines)): |
|
|
|
|
l = addlines[i] |
|
|
|
|
if( i < 1 ): |
|
|
|
|
newline += f"({l})*" |
|
|
|
|
elif( i >= 1 and i < len(addlines)-1 ): |
|
|
|
|
newline += f"({l})*" |
|
|
|
|
elif( i == len(addlines)-1 ): |
|
|
|
|
newline += f"({l})" |
|
|
|
|
# # elif( char == "*" and curDepth == addDepth ): |
|
|
|
|
# # chars.insert(i+offset, ")") |
|
|
|
|
|
|
|
|
|
return newline |
|
|
|
|
# return chars |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parseMath2(line): |
|
|
|
|
line = line.replace(" ", "") |
|
|
|
|
# addPars( strToList("2*3+(4*5)") ) |
|
|
|
|
|
|
|
|
|
def parseMath2(line:str): |
|
|
|
|
line = line.replace(" ", "") |
|
|
|
|
count = 0 |
|
|
|
|
|
|
|
|
|
# calculate all pars and replace them |
|
|
|
|
pars = findParens(line) |
|
|
|
|
chars = strToList(line) |
|
|
|
|
print(chars) |
|
|
|
|
|
|
|
|
|
for start, end in pars.items(): |
|
|
|
|
calc = calcMath(chars[start:end+1]) |
|
|
|
|
|
|
|
|
|
for j in range(start, end+1): |
|
|
|
|
chars[j] = "" |
|
|
|
|
|
|
|
|
|
chars[start] = str(calc) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("##########3") |
|
|
|
|
# remove bad chars |
|
|
|
|
chars = list( filter(lambda c: c != "", chars) ) |
|
|
|
|
print(chars) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# calculate the rest |
|
|
|
|
count += calcMath(chars) |
|
|
|
|
|
|
|
|
|
return count |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
problem = mathproblems[0] |
|
|
|
|
res = parseMath2(problem) |
|
|
|
|
print("\n--##########################--") |
|
|
|
|
print( problem, "=", res ) |
|
|
|
|
# problem = mathproblems[0] |
|
|
|
|
# res = parseMath2(problem) |
|
|
|
|
# print("\n--##########################--") |
|
|
|
|
# print( problem, "=", res ) |
|
|
|
|
|
|
|
|
|
# mathsum = 0 |
|
|
|
|
|
|
|
|
|