Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Iamhankai Forest of Thought Check Expression

From Leeroopedia
Knowledge Sources
Domains Verification, Game_Solving
Last Updated 2026-02-14 03:00 GMT

Overview

Concrete tool for validating Game24 arithmetic expressions provided by the Forest-of-Thought repository.

Description

The check_expression function validates a candidate Game24 solution by: (1) extracting all digits from the expression, (2) verifying they match the input numbers using Counter-based multiset comparison, and (3) evaluating the expression to check it equals the target (24). Uses Python's eval() for expression evaluation with error handling for invalid expressions.

Usage

Called within forest_solve() and naive_solve() to validate candidate expressions before accepting them as solutions.

Code Reference

Source Location

Signature

def check_expression(input_string, expression, target_result=24):
    """
    Validate arithmetic expression for Game24.

    Args:
        input_string (str): Available numbers (e.g., "1 2 3 4").
        expression (str): Candidate expression (e.g., "(1+2+3)*4").
        target_result (int): Target evaluation result (default: 24).

    Returns:
        bool: True if expression uses correct numbers and evaluates to target.
    """

Import

from methods.bfs import check_expression

I/O Contract

Inputs

Name Type Required Description
input_string str Yes Space-separated input numbers
expression str Yes Candidate arithmetic expression
target_result int No Target value (default: 24)

Outputs

Name Type Description
is_valid bool True if expression is valid

Usage Examples

from methods.bfs import check_expression

# Valid expression
assert check_expression("1 2 3 4", "(1+2+3)*4", 24) == True

# Wrong numbers
assert check_expression("1 2 3 4", "(1+2+5)*4", 24) == False

# Wrong result
assert check_expression("1 2 3 4", "1+2+3+4", 24) == False

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment