-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_expression.py
47 lines (37 loc) · 1.27 KB
/
binary_expression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""This example convert the binary string expression0 to int and calculate the result """
stri = '101+100-110*101010/101'
total_count = 0
for i ,j in enumerate(stri):
if j in "+-/*":
total_count=total_count+1
res=[]
k=0
c=0
# for i,j in enumerate(stri):
# if j in "+-/*": # count the total mathematical expression
# total_count = total_count + 1
for i, j in enumerate(stri):
if j in "+-/*":
res.append(str(int(stri[k:i],2)))
res.append(j)
k=i+1
c=c+1
if c ==total_count:
res.append(str(int(stri[i+1:],2)))
c=c+1
print(res)
print(eval(('').join(res)))
# result = []
# k=0
# count = 0
# for i,j in enumerate(stri):
# if j in "+-/*":
# result.append(str(int(stri[k:i],2))) # Get the binary expression and convert into INT and append in the Result list
# result.append(j) # Append the mathematical expression in Result list
# k = i+1
# count = count+1
# if count == total_count:
# result.append(str(int(stri[i+1:],2))) # Append the last binary expression in the Result List
# count = count+1
# print(result) #The complete list of Int value and mathematical expression in the order of given string.
# print(eval(('').join(result))) # Join the list and eval() the Result.