Hone logo
Hone
Problems

Simple Constraint Solver in Python

This challenge asks you to implement a basic constraint solver in Python. Constraint solving is a powerful technique used in various fields like scheduling, puzzle solving (Sudoku, KenKen), and resource allocation. Your solver will focus on solving simple integer constraints with equality and inequality relationships.

Problem Description

You are tasked with creating a Python class called ConstraintSolver. This class should be able to accept a list of constraints and then attempt to find a solution that satisfies all constraints. Each constraint will be represented as a tuple: (variable1, operator, value, variable2, operator2, value2).

  • variable1 and variable2 are strings representing the names of the variables involved in the constraint. If a constraint only involves one variable, variable2 and the subsequent operators/values will be None.
  • operator is a string representing the relationship between variable1 and value. It can be one of the following:
    • "==": Equal to
    • "!=": Not equal to
    • "<": Less than
    • ">": Greater than
    • "<=": Less than or equal to
    • ">=": Greater than or equal to
  • value is an integer representing the value that variable1 should be compared to.
  • operator2 and value2 are used for constraints involving two variables. They follow the same rules as operator and value. If variable2 is None, these are ignored.

The solver should return a dictionary where keys are variable names and values are the assigned integer values that satisfy all constraints. If no solution exists, it should return None. The solver should attempt to find one valid solution; it does not need to find all possible solutions.

Examples

Example 1:

Input: constraints = [("x", "==", 5), ("y", ">=", 10), ("x", "<", "y")]
Output: {"x": 5, "y": 10}
Explanation:  x = 5, y = 10 satisfies all constraints. x == 5, y >= 10, and x < y.

Example 2:

Input: constraints = [("x", "==", 5), ("y", "==", 7), ("x", "!=", "y")]
Output: {"x": 5, "y": 7}
Explanation: x = 5, y = 7 satisfies all constraints. x == 5, y == 7, and x != y.

Example 3:

Input: constraints = [("x", "==", 5), ("y", "==", 7), ("x", "==", "y")]
Output: None
Explanation: No solution exists because x cannot be equal to both 5 and y, and y is fixed at 7.

Example 4: (Edge Case - Single Variable)

Input: constraints = [("x", "<=", 10)]
Output: {"x": 10}
Explanation: A valid solution is x = 10.  Any value less than or equal to 10 would also be valid, but we only need to return one.

Constraints

  • Variable names will be strings consisting of lowercase letters.
  • Values will be integers.
  • The number of constraints can range from 0 to 10.
  • The solver should be reasonably efficient. Brute-force approaches are acceptable for this problem size, but avoid extremely inefficient algorithms.
  • The solver should handle cases where no solution exists gracefully, returning None.

Notes

  • You can use a backtracking approach to explore possible solutions.
  • Consider the order in which you assign values to variables. A good ordering can significantly improve performance.
  • Start by implementing the solver for constraints involving only one variable. Then, extend it to handle two-variable constraints.
  • Think about how to efficiently check if a partial assignment violates any constraints.
  • The problem focuses on the core constraint solving logic; input parsing and error handling are not required. Assume the input is always well-formed.
Loading editor...
python