-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·83 lines (54 loc) · 2.37 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import unittest
OPERANDS = ["true", "false"]
OPERATORS = ["xor", "and", "or"]
def evalutate_expr(expr: str) -> bool:
expr = expr.replace("true", "True").replace("false", "False").replace("xor", "!=")
return eval(expr)
def get_variants(expression):
tokens = expression.split(" ")
if len(tokens) <= 3:
return [expression]
return [
expression,
f"({' '.join(tokens[0:3])}) {' '.join(tokens[3:])}",
f"{' '.join(tokens[:2])} ({' '.join(tokens[2:5])})",
]
def count_parents(expression):
all_expressions = get_variants(expression)
return sum(evalutate_expr(e) for e in all_expressions)
class CountingParentsTestCase(unittest.TestCase):
def test_true(self):
self.assertEqual(count_parents("true"), 1)
def test_false(self):
self.assertEqual(count_parents("false"), 0)
def test_true_and_true(self):
self.assertEqual(count_parents("true and true"), 1)
def test_true_and_false(self):
self.assertEqual(count_parents("true and false"), 0)
def test_true_or_false(self):
self.assertEqual(count_parents("true or false"), 1)
def test_false_or_false(self):
self.assertEqual(count_parents("false or false"), 0)
def test_true_xor_true(self):
self.assertEqual(count_parents("true xor true"), 0)
def test_false_xor_true(self):
self.assertEqual(count_parents("false xor true"), 1)
def test_false_and_true_and_true(self):
self.assertEqual(count_parents("false and true and true"), 0)
def test_true_and_true_and_true(self):
self.assertEqual(count_parents("true and true and true"), 3)
def test_false_and_false_and_false(self):
self.assertEqual(count_parents("false and false and false"), 0)
def test_false_and_false_or_true(self):
self.assertEqual(count_parents("false and false or true"), 2)
def test_true_and_false_or_true(self):
self.assertEqual(count_parents("true and false or true"), 3)
def test_true_or_false_or_true(self):
self.assertEqual(count_parents("true or false or true"), 3)
def test_true_xor_false_and_true(self):
self.assertEqual(count_parents("true xor false and true"), 3)
def test_true_and_false_xor_true(self):
self.assertEqual(count_parents("true and false xor true"), 3)
if __name__ == "__main__":
unittest.main()