Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def count_truths(operands, operators):
- operations = {
- '&': lambda left, right: left and right,
- '|': lambda left, right: left or right,
- '^': lambda left, right: (left and not right) or (right and not left)
- }
- def evaluate (left, right, op):
- return operations[op](left, right)
- def reduce (op_idx):
- new_operands = operands[:]
- left, right = operands[op_idx:op_idx+2]
- new_operators = operators[:]
- op = operators[op_idx]
- new_operands[op_idx:op_idx+2] = [evaluate(left, right, op)]
- new_operators[op_idx:op_idx+1] = []
- return new_operands, new_operators
- if not operators: return 1 if operands[0] else 0
- counts = [count_truths(*reduce(idx)) for idx in range(len(operators))]
- return sum(counts)
- def solve(literals, symbols):
- operands = [literal == 't' for literal in literals]
- operators = list(symbols)
- return count_truths(operands, operators)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement