Python Forum

Full Version: Cross word puzzle solve using python constraint library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[Image: open?id=1GAH82jPFC-OSY-HPY9VZixf7Tjn_YPe3]
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from constraint import *
problem = Problem()
problem.addVariable("1ACROSS",["HOSES","LASER","SAILS","SHEET","STEER"])
problem.addVariable("4ACROSS",["HEEL","HIKE","KEEL","KNOT","LINE"])
problem.addVariable("7ACROSS",["AFT","ALE","EEL","LEE","TIE"])
problem.addVariable("8ACROSS",["HOSES","LASER","SAILS","SHEET","STEER"])
problem.addVariable("2DOWN",  ["HOSES","LASER","SAILS","SHEET","STEER"])
problem.addVariable("3DOWN",  ["HOSES","LASER","SAILS","SHEET","STEER"])
problem.addVariable("5DOWN",["HEEL","HIKE","KEEL","KNOT","LINE"])
problem.addVariable("6DOWN",["AFT","ALE","EEL","LEE","TIE"])
problem.addConstraint(lambda 1ACROSS,2DOWN: 1ACROSS in range((1,1),(1,5)) and 2DOWN in range ((1,3),(5,3)) and 1ACROSS[2]==2DOWN[0])
problem.addConstraint(lambda 3DOWN,1ACROSS: 3DOWN in range((1,5),(5,5)) and 1ACROSS[4]==3DOWN[0])
problem.addConstraint(lambda 4ACROSS,5DOWN: 4ACROSS in range((3,2),(3,5)) and 5DOWN in range ((3,4),(6,4)) and 4ACROSS[1]==2DOWN[2] and 4ACROSS[2]==5DOWN[0] and 4ACROSS[3]==3DOWN[2])
problem.addConstraint(lambda 6DOWN,8ACROSS: 6DOWN in range((4,1),(6,1)) and 8ACROSS in range((5,1),(5,5)) and 6DOWN[1]==8ACROSS[0])
problem.addConstraint(7ACROSS in range((4,3),(4,5)) and 7ACROSS[0]==2DOWN[3] and 7ACROSS[1]==5DOWN[1] and 7ACROSS[2]==3DOWN[3])
problem.addConstraint(8ACROSS in range((5,1),(5,5)) and 8ACROSS[0]==6DOWN[1] and 8ACROSS[2]==2DOWN[4] and 8ACROSS[3]==5DOWN[2] and 8ACROSS[4]==3DOWN[4])
problem.getSolution()
But this code didn't work... any help regarding this . When i run this it gives this output
Error:
line 16 problem.addConstraint(lambda 1ACROSS,2DOWN: 1ACROSS in range((1,1),(1,5)) and 2DOWN in range ((1,3),(5,3)) and 1ACROSS[2]==2DOWN[0]) ^ SyntaxError: invalid syntax
The documentation for problem.addVariable always shows items in list as numerics, never strings, although I don't see any constraints on doing so:
Output:
addVariable(self, variable, domain) Add a variable to the problem Example: >>> problem = Problem() >>> problem.addVariable("a", [1, 2]) >>> problem.getSolution() in ({'a': 1}, {'a': 2}) True Parameters: variable - Object representing a problem variable (type=hashable object) domain - Set of items defining the possible values that the given variable may assume (type=list, tuple, or instance of Domain) addVariables(self, variables, domain) Add one or more variables to the problem Example: >>> problem = Problem() >>> problem.addVariables(["a", "b"], [1, 2, 3]) >>> solutions = problem.getSolutions() >>> len(solutions) 9 >>> {'a': 3, 'b': 1} in solutions True Parameters: variables - Any object containing a sequence of objects represeting problem variables (type=sequence of hashable objects) domain - Set of items defining the possible values that the given variables may assume (type=list, tuple, or instance of Domain)
If that were an issue, I would expect it to be documented, but don't see it.

I found the example of building a crossword which uses constraints: http://www.cromulentrambling.com/2017/01...ython.html
Please note that the author builds problem thusly:
problem.addVariables(list(range(s*s)), [0,1])
again with a list of numerics for domain.