Last active
May 20, 2022 00:13
-
-
Save MateusStanki/6d0c9a7cad3039aa3775c292f0442ba2 to your computer and use it in GitHub Desktop.
DojoPinion01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import unittest | |
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] | |
variants = [] | |
tokens_cursor = 0 | |
while(tokens_cursor < len(tokens)): | |
parenthesis_cursor = 3 + tokens_cursor | |
while(parenthesis_cursor <= len(tokens)): | |
variants.append(tokens[:tokens_cursor] + ["("] + tokens[tokens_cursor:parenthesis_cursor] + [")"] + tokens[parenthesis_cursor:]) | |
parenthesis_cursor += 2 | |
tokens_cursor += 2 | |
return variants | |
def count_parents(expression): | |
all_expressions = get_variants(expression) | |
if(len(all_expressions) == 1): | |
return evalutate_expr(all_expressions[0]) | |
else: | |
return sum(evalutate_expr(" ".join(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("true and false and true or 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) | |
def test_false_or_false_or_false(self): | |
self.assertEqual(count_parents("false or false or false"), 0) | |
def test_true_and_false_xor_true_or_true(self): | |
self.assertEqual(count_parents("true and false xor true or true"), 6) | |
def test_true_and_false_and_false_and_true(self): | |
self.assertEqual(count_parents("true xor false xor false xor true"), 5) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment