sentientcabbagebulb
sentientcabbagebulb
Untitled
1 post
Don't wanna be here? Send us removal request.
sentientcabbagebulb · 2 years ago
Text
Calculator go brrrrrrr
def cog(str):
res= []
sub=""
for a in str:
try:
if type(int(a))== int:
sub+=a
except ValueError:
res.append(int(sub))
sub= ""
res.append(a)
res.append(int(sub))
return(res)
def find(lst, target):
for a in range(len(lst)):
if lst[a]== target:
return(a)
def calc(lst):
if len(lst)==1:
return(lst[0])
print(lst)
for a in range(len(lst)):
try:
if lst[a]== "*":
lst[a]= lst[a+1] * lst[a-1]
lst.pop(a+1)
lst.pop(a-1)
if lst[a]== "/":
lst[a]= lst[a-1] / lst[a+1]
lst.pop(a+1)
lst.pop(a-1)
if lst[a]== "+":
lst[a]= lst[a+1] + lst[a-1]
lst.pop(a+1)
lst.pop(a-1)
except IndexError:
return find(lst)
def calc2(lst):
ops= ["*","/","+","-"]
if len(lst) <= 1:
return(lst[0])
try:
for a in ops:
cops = find(lst, a)
if find(lst, a) != None:
if lst[cops]=="*":
lst[cops] = lst[cops +1] * lst[cops-1]
lst.pop(cops+1)
lst.pop(cops-1)
continue
if lst[cops]=="/":
lst[cops] = lst[cops -1] / lst[cops+1]
lst.pop(cops+1)
lst.pop(cops-1)
continue
if lst[cops]=="+":
lst[cops] = lst[cops +1] + lst[cops-1]
lst.pop(cops+1)
lst.pop(cops-1)
continue
if lst[cops]=="-":
lst[cops] = lst[cops +1] - lst[cops-1]
lst.pop(cops+1)
lst.pop(cops-1)
continue
except IndexError:
return lst[0]
return(calc2(lst))
yourturn= input("Equation")
print("=======================================================================")
print(calc2(cog(yourturn)))
1 note · View note