-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluate Operations
More file actions
21 lines (19 loc) · 1.06 KB
/
Evaluate Operations
File metadata and controls
21 lines (19 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def evaluate(numbers, operators):
ln=len(numbers); lo=len(operators) #length of list numbers operators
answer,i,j=0,1,0 #initalize answer, index i & j to zero
#number of operations will be ln-1, so we append the list operators to match the operations
while (ln-lo)>1:
operators.append(operators[j]); #append the list operators on left to right basis
j+=1; #increment index j
lo=len(operators) #update the length of operators
#perform operations left to right in the list operators
for ops in operators:
if ops=='+': answer=numbers[i-1]+numbers[i];
elif ops=='-': answer=numbers[i-1]-numbers[i];
elif ops=='*': answer=numbers[i-1]*numbers[i];
elif ops=='/': answer=numbers[i-1]/numbers[i];
elif ops=='%': answer=numbers[i-1]%numbers[i];
numbers[i-1]=0;
numbers[i]=answer; #update the answer in the list numbers
i+=1; #increment index i
return int(numbers[-1]) #return the last element of the list numbers with final answer