+ 1

What are constraints in python?

Can anyone give me an easy example and explanation? I’m not that far in the python lesson here but it’s part of my homework. Thanks in advance!

20th Mar 2018, 3:56 AM
Robbie Jones
Robbie Jones - avatar
1 Resposta
+ 2
PYTHON-CONSTRAINT Introduction The Python constraint module offers solvers for Constraint Solving Problems (CSPs) over finite domains in simple and pure Python. CSP is class of problems which may be represented in terms of variables (a, b, ...), domains (a in [1, 2, 3], ...), and constraints (a < b, ...). Examples Basics This interactive Python session demonstrates the module basic operation: >>> from constraint import * >>> problem = Problem() >>> problem.addVariable("a", [1,2,3]) >>> problem.addVariable("b", [4,5,6]) >>> problem.getSolutions() [{'a': 3, 'b': 6}, {'a': 3, 'b': 5}, {'a': 3, 'b': 4}, {'a': 2, 'b': 6}, {'a': 2, 'b': 5}, {'a': 2, 'b': 4}, {'a': 1, 'b': 6}, {'a': 1, 'b': 5}, {'a': 1, 'b': 4}] >>> problem.addConstraint(lambda a, b: a*2 == b, ("a", "b")) >>> problem.getSolutions() [{'a': 3, 'b': 6}, {'a': 2, 'b': 4}] >>> problem = Problem() >>> problem.addVariables(["a", "b"], [1, 2, 3]) >>> problem.addConstraint(AllDifferentConstraint()) >>> problem.getSolutions() [{'a': 3, 'b': 2}, {'a': 3, 'b': 1}, {'a': 2, 'b': 3}, {'a': 2, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}] Details: https://labix.org/python-constraint
20th Mar 2018, 4:51 AM
📈SmileGoodHope📈
📈SmileGoodHope📈 - avatar