forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_330.py
39 lines (30 loc) · 988 Bytes
/
problem_330.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
NOT_SYMBOL = "¬"
OR_SYMBOL = "OR"
AND_SYMBOL = "AND"
def get_solution(string, symbols):
power = len(symbols)
count_sol = 2 ** power
sol = None
for solution in range(count_sol):
bin_rep = format(solution, '0' + str(power) + 'b')
new_str = string[:]
for i in range(power):
val = str(bool(int(bin_rep[i])))
new_str = new_str.replace(symbols[i], val)
new_str = new_str.replace(NOT_SYMBOL, "not ")
new_str = new_str.replace(OR_SYMBOL, "or")
new_str = new_str.replace(AND_SYMBOL, "and ")
if eval(new_str) == True:
sol = bin_rep
break
if not sol:
return None
solution_map = dict()
for i in range(power):
solution_map[symbols[i]] = bool(int(sol[i]))
return solution_map
# Tests
assert get_solution(
"(¬c OR b) AND (b OR c) AND (¬b OR c) AND (¬c OR ¬a)",
['a', 'b', 'c']
) == {'a': False, 'b': True, 'c': True}