Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- # to replace, eq: and -> &
- connector = {'and':'&', 'or':'|'}
- print('')
- expr = input("Enter your expression: ")
- expr = expr.replace(' ', '')
- for k,v in connector.items():
- expr = expr.replace(k,v)
- # cari semua single vars
- var = []
- for c in expr:
- if c.isalpha():
- if c not in var:
- var.append(c)
- print("List of variable : ", end='')
- print(var)
- print('')
- # possible input 2^n
- n = 2**len(var)
- for i in range(n):
- # https://stackoverflow.com/questions/699866/python-int-to-binary
- binary = "{0:b}".format(i)
- # padding zero
- binary = binary.zfill(len(var))
- new_expr = expr[:]
- for i,v in enumerate(var):
- new_expr = new_expr.replace(var[i], binary[i])
- # convert back to original expression
- for k,v in connector.items():
- new_expr = new_expr.replace(v, ' {} '.format(k))
- output = eval(new_expr)
- output = int(output)
- print("Evaluate ({}) => {}".format(new_expr, output))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement