# author: Marcin Cherek # python version: 3.4 # Date: 5 of Mai 2016 # language: English # Title: solve Algorithm python implementation import decimal import logging logging.basicConfig() logger= logging.getLogger(__name__) logger.setLevel(logging.INFO) #Prepares the the calculation #this method splits the Operators #from the Numbers def _prepare(calculation): for i in ["+","-","*","/"]: calculation=calculation.replace(i,";"+i+";") return calculation.split(";") #Replaces the sub calculation #with the Result of it def _replaceRes(deb,result,zähler): deb[zähler-1]="" deb[zähler]="" deb[zähler+1]=result return deb #Normal calculation of two #Numebers and one Operator def _calc(z1,z2,op): if(op=="+"):return z1+z2 elif(op=="-"):return z1-z2 elif(op=="*"):return z1*z2 elif(op=="/"):return z1/z2 #Shrinks the Array #It removes the empty elements def _popArr(deb): p=0 while p<len(deb): if deb[p]=="": deb.pop(p) continue p+=1 return deb #Most important method #It iterates the calculation #and corrdinates the subcalcs with the #results of it def _solve_simple(deb,op): result=0;zähler=0 for i in deb: if i==op: logger.info(deb) result=_calc(decimal.Decimal(deb[zähler-1]),decimal.Decimal(deb[zähler+1]),op) _replaceRes(deb,result,zähler) result=0 zähler+=1 deb = _popArr(deb) return deb #Calls the simple Alg #First of course */ and after that +- def solve(calculation): calculation = _prepare(calculation) for i in ["*","/","+","-"]: calculation=_solve_simple(calculation,i) return decimal.Decimal(calculation[len(calculation)-1])
Thursday, May 5, 2016
Python solve Algorithm
Location:
Zürich, Schweiz
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment