Skip to content

Commit 8160f60

Browse files
committed
switched truth table raise exception to returning result
1 parent 3ae4526 commit 8160f60

1 file changed

Lines changed: 41 additions & 10 deletions

File tree

evaluation_function/truth_table/evaluate.py

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ def evaluate_truth_table(input: list[list[str]], num_atoms) -> Result:
2727
"""
2828

2929
if len(input) == 0:
30-
raise Exception("no input was given")
30+
return Result(
31+
is_correct=False,
32+
feedback_items=[(Exception, "no input was given")]
33+
)
34+
3135
elif len(input) == 1:
32-
raise Exception("Must provide names and its truth values")
36+
return Result(
37+
is_correct=False,
38+
feedback_items=[(Exception, "Must provide names and its truth values")]
39+
)
3340

3441
# find the atoms of the formula
3542
formulas = input[0]
@@ -44,9 +51,15 @@ def evaluate_truth_table(input: list[list[str]], num_atoms) -> Result:
4451
formula = formula_parser(formula_string)
4552

4653
except BuildError as e:
47-
raise Exception(error_message + str(e))
54+
return Result(
55+
is_correct=False,
56+
feedback_items=[(BuildError, error_message + str(e))]
57+
)
4858
except ValueError as e:
49-
raise Exception(error_message + str(e))
59+
return Result(
60+
is_correct=False,
61+
feedback_items=[(ValueError, error_message + str(e))]
62+
)
5063

5164
# formula is valid
5265

@@ -62,7 +75,10 @@ def evaluate_truth_table(input: list[list[str]], num_atoms) -> Result:
6275

6376
# if an atom is undefined, erro
6477
if atom not in existing_atoms:
65-
raise Exception(f"in column {i+1}, atom {atom} in formula {formula_string} is undefined")
78+
return Result(
79+
is_correct=False,
80+
feedback_items=[(Exception, f"in column {i+1}, atom {atom} in formula {formula_string} is undefined")]
81+
)
6682

6783
# replace strings with
6884
formulas[i] = formula
@@ -78,22 +94,37 @@ def evaluate_truth_table(input: list[list[str]], num_atoms) -> Result:
7894
elif input[i][j] == "ff":
7995
input[i][j] = False
8096
else:
81-
raise Exception(f"cell in column {j+1} row {i+1} invalid")
97+
return Result(
98+
is_correct=False,
99+
feedback_items=[(Exception, f"cell in column {j+1} row {i+1} invalid")]
100+
)
82101

83102

84103
# check that every combination of the atoms is stated in the truth table.
85104

86105
if len(existing_atoms) != num_atoms:
87-
raise Exception(f"missing combinations in truth table")
106+
return Result(
107+
is_correct=False,
108+
feedback_items=[(Exception, f"missing combinations in truth table")]
109+
)
88110
if len(input) - 1 < 2 ** num_atoms:
89-
raise Exception(f"missing combinations in truth table")
111+
return Result(
112+
is_correct=False,
113+
feedback_items=[(Exception, f"missing combinations in truth table")]
114+
)
90115
if len(input) - 1 > 2 ** num_atoms:
91-
raise Exception(f"excessive combinations in truth table")
116+
return Result(
117+
is_correct=False,
118+
feedback_items=[(Exception, f"excessive combinations in truth table")]
119+
)
92120

93121

94122
unique_rows = set(tuple(row[cell] for cell in existing_atoms.values()) for row in input[1:])
95123
if unique_rows < 2 ** num_atoms:
96-
raise Exception("dupliated assignment to atoms")
124+
return Result(
125+
is_correct=False,
126+
feedback_items=[(Exception, "dupliated assignment to atoms")]
127+
)
97128

98129

99130
# evaluate truth table row by row

0 commit comments

Comments
 (0)