Advertisement
Guest User

88888888

a guest
Jan 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. def count_truths(operands, operators):
  2.  
  3. operations = {
  4. '&': lambda left, right: left and right,
  5. '|': lambda left, right: left or right,
  6. '^': lambda left, right: (left and not right) or (right and not left)
  7. }
  8. def evaluate (left, right, op):
  9. return operations[op](left, right)
  10.  
  11. def reduce (op_idx):
  12. new_operands = operands[:]
  13. left, right = operands[op_idx:op_idx+2]
  14. new_operators = operators[:]
  15. op = operators[op_idx]
  16. new_operands[op_idx:op_idx+2] = [evaluate(left, right, op)]
  17. new_operators[op_idx:op_idx+1] = []
  18. return new_operands, new_operators
  19.  
  20. if not operators: return 1 if operands[0] else 0
  21. counts = [count_truths(*reduce(idx)) for idx in range(len(operators))]
  22. return sum(counts)
  23.  
  24. def solve(literals, symbols):
  25. operands = [literal == 't' for literal in literals]
  26. operators = list(symbols)
  27. return count_truths(operands, operators)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement