Skip to content

Commit fcee544

Browse files
Added criteria for checking if expression contains a specific free symbol, constant or number
1 parent a762afc commit fcee544

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

app/context/symbolic.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ def check_criterion(criterion, parameters_dict, generate_feedback=True):
3535
parsing_params.update({"simplify": False})
3636
if label in {"EQUALITY", "WRITTEN_AS"}:
3737
result = check_equality(criterion, parameters_dict)
38-
if label == "ORDER":
38+
elif label == "ORDER":
3939
result = check_order(criterion, parameters_dict)
40+
elif label == "CONTAINS":
41+
result = check_contains_symbol(criterion, parameters_dict)
4042
elif label == "WHERE":
4143
crit = criterion.children[0]
4244
subs = criterion.children[1]
@@ -143,6 +145,12 @@ def check_order(criterion, parameters_dict, local_substitutions=[]):
143145
return result
144146

145147

148+
def check_contains_symbol(criterion, parameters_dict, local_substitutions=[]):
149+
lhs_expr, rhs_expr = create_expressions_for_comparison(criterion, parameters_dict, local_substitutions)
150+
result = rhs_expr in lhs_expr.atoms()
151+
return result
152+
153+
146154
def find_coords_for_node_type(expression, node_type):
147155
stack = [(expression, tuple())]
148156
node_coords = []

app/tests/symbolic_evaluation_tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,7 @@ def test_criteria_where_numerical_comparison(self, response, answer, criteria, v
17951795
("1", "2", "response <= answer", True),
17961796
("2", "2", "response <= answer", True),
17971797
("3", "2", "response <= answer", False),
1798+
("5", "9", "response > 3", True),
17981799
]
17991800
)
18001801
def test_criteria_order_comparison(self, response, answer, criteria, value):
@@ -1806,6 +1807,29 @@ def test_criteria_order_comparison(self, response, answer, criteria, value):
18061807
result = evaluation_function(response, answer, params)
18071808
assert result["is_correct"] is value
18081809

1810+
@pytest.mark.parametrize(
1811+
"criteria, value",
1812+
[
1813+
("response contains c", False),
1814+
("response contains a", True),
1815+
("response contains x", True),
1816+
("response contains pi", True),
1817+
("response contains 23", True),
1818+
("response contains 3", False),
1819+
("response contains answer", True),
1820+
]
1821+
)
1822+
def test_contains_symbol(self, criteria, value):
1823+
response = "a+23+pi+sin(x)+y"
1824+
answer = "y"
1825+
params = {
1826+
"strict_syntax": False,
1827+
"elementary_functions": True,
1828+
"criteria": criteria,
1829+
}
1830+
result = evaluation_function(response, answer, params)
1831+
assert result["is_correct"] is value
1832+
18091833
@pytest.mark.parametrize(
18101834
"response, answer, value",
18111835
[

app/utility/criteria_parsing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
(" *= *", "EQUALITY"),
1717
(" *(>=?|<=?|ORDER) *", "ORDER"), # less than (or equal), < (<=), greater than (or equal), > (>=)
1818
(" *where *", "WHERE"),
19-
(" *written as *", "WRITTEN_AS"),
19+
(" *written +as *", "WRITTEN_AS"),
20+
(" *contains *", "CONTAINS"),
2021
(" *; *", "SEPARATOR"),
2122
(" *OTHER *", "OTHER", catch_undefined),
2223
]
@@ -29,6 +30,8 @@
2930
("BOOL", "EQUAL where EQUAL_LIST", infix),
3031
("BOOL", "RESERVED written as OTHER", infix),
3132
("BOOL", "RESERVED written as RESERVED", infix),
33+
("BOOL", "RESERVED contains OTHER", infix),
34+
("BOOL", "RESERVED contains RESERVED", infix),
3235
("EQUAL_LIST", "EQUAL;EQUAL", infix),
3336
("EQUAL_LIST", "EQUAL_LIST;EQUAL", append_last),
3437
("EQUAL", "OTHER = OTHER", infix),

0 commit comments

Comments
 (0)