0
votes

enter image description here

I have this problem to solve that is shown in the picture. i have tried the code given below

    # -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from constraint import *

problem = Problem()

problem.addVariable('oneACROSS',['HOSES','LASER','SAILS','SHEET','STEER'])

problem.addVariable('fourACROSS',['HEEL','HIKE','KEEL','KNOT','LINE'])

problem.addVariable('sevenACROSS',['AFT','ALE','EEL','LEE','TIE'])

problem.addVariable('eightACROSS',['HOSES','LASER','SAILS','SHEET','STEER'])

problem.addVariable('twoDOWN',  ['HOSES','LASER','SAILS','SHEET','STEER'])

problem.addVariable('threeDOWN',  ['HOSES','LASER','SAILS','SHEET','STEER'])

problem.addVariable('fiveDOWN',['HEEL','HIKE','KEEL','KNOT','LINE'])

problem.addVariable('sixDOWN',['AFT','ALE','EEL','LEE','TIE'])

problem.addConstraint(lambda oneACROSS,twoDOWN : oneACROSS[3]==twoDOWN[1])

problem.addConstraint(lambda oneACROSS,threeDOWN : oneACROSS[5]==threeDOWN[1]) 

problem.addConstraint(lambda fourACROSS,twoDOWN : fourACROSS[2]==twoDOWN[3])

problem.addConstraint(lambda fourACROSS,fiveDOWN : fourACROSS[3]==fiveDOWN[1])

problem.addConstraint(lambda threeDOWN,fourACROSS : fourACROSS[4]==threeDOWN[3])

problem.addConstraint(lambda twoDOWN,sevenACROSS : sevenACROSS[1]==twoDOWN[4])

problem.addConstraint(lambda sevenACROSS,fiveDOWN : sevenACROSS[2]==fiveDOWN[2]) 

problem.addConstraint(lambda threeDOWN,sevenACROSS : sevenACROSS[3]==threeDOWN[4])

problem.addConstraint(lambda sixDOWN,eightACROSS : eightACROSS[1]==sixDOWN[2])

problem.addConstraint(lambda twoDOWN,eightACROSS : eightACROSS[3]==twoDOWN[5])

problem.addConstraint(lambda fiveDOWN,eightACROSS : eightACROSS[4]==fiveDOWN[3])

problem.addConstraint(lambda threeDOWN,eightACROSS : eightACROSS[5]==threeDOWN[5])

solution=problem.getSolutions()

print(solution)

But it gives me this error

runfile('C:/Users/aliya/.spyder-py3/temp.py', wdir='C:/Users/aliya/.spyder-py3') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/aliya/.spyder-py3/temp.py', wdir='C:/Users/aliya/.spyder-py3')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/aliya/.spyder-py3/temp.py", line 50, in solution=problem.getSolutions()

File "C:\ProgramData\Anaconda3\lib\site-packages\constraint__init__.py", line 271, in getSolutions return self._solver.getSolutions(domains, constraints, vconstraints)

File "C:\ProgramData\Anaconda3\lib\site-packages\constraint__init__.py", line 567, in getSolutions return list(self.getSolutionIter(domains, constraints, vconstraints))

File "C:\ProgramData\Anaconda3\lib\site-packages\constraint__init__.py", line 544, in getSolutionIter if not constraint(variables, domains, assignments, pushdomains):

File "C:\ProgramData\Anaconda3\lib\site-packages\constraint__init__.py", line 991, in call self.forwardCheck(variables, domains, assignments)

File "C:\ProgramData\Anaconda3\lib\site-packages\constraint__init__.py", line 935, in forwardCheck if not self(variables, domains, assignments):

File "C:\ProgramData\Anaconda3\lib\site-packages\constraint__init__.py", line 993, in call return self._func(*parms)

TypeError: lambda() takes 2 positional arguments but 8 were given

Any help regarding this!

3
TypeError: <lambda>() takes 2 positional arguments but 8 were given ... but i am giving only two to the lambda function !Ali Yar Khan

3 Answers

1
votes

On all the lines starting with problem.addConstraint you are using variable names starting with a number, e.g. 1ACROSS and 2DOWN. This is not allowed in Python. Try calling them ONEACROSS and TWODOWN or VAR1ACROSS and VAR2DOWN, for instance, and it should work better.

0
votes

Python parser forbids naming variables that way, for the sake of parsing numbers and variables separately, as naming a variable 1e1 would create a chaos - is it the number 10.0 or the variable 1e1?

"Python, please output for me 1e1!" - "Why is it 10.0? I stored 100 over there!"

But the variables are actually stored in a way that allows binding a string that starts with a number to a value, because that feature is no harm in hashing maps of any kind, and so using this "trick" you can achieve your wanted numeral-prefixed-name variable without hurting the parser severability.

I would say that technically, naming variables in that manner is not a violation to python guidelines, but it is highly discouraged, and as a rule unnecessary. Using globals for injecting variables is known as a very bad practice and this case should not be an outstanding.

So change

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])

To:

problem.addConstraint(lambda ACROSS,DOWN: ACROSS in range((1,1),(1,5)) and DOWN in range ((1,3),(5,3)) and ACROSS[2]==DOWN[0])
problem.addConstraint(lambda DOWN,ACROSS: DOWN in range((1,5),(5,5)) and ACROSS[4]==DOWN[0])
problem.addConstraint(lambda ACROSS,DOWN: ACROSS in range((3,2),(3,5)) and DOWN in range ((3,4),(6,4)) and ACROSS[1]==DOWN[2] and ACROSS[2]==DOWN[0] and ACROSS[3]==DOWN[2])
problem.addConstraint(lambda DOWN,ACROSS: DOWN in range((4,1),(6,1)) and ACROSS in range((5,1),(5,5)) and DOWN[1]==ACROSS[0])
0
votes

hi dear crossword puzzle is a very complex problem i have a solution of this assignment but implemented with ortools library by google. Following are the requirements to use this library: Requirements:

**Library:** ortools by google

**Requirements for this library:**


**1-you have to install/update your pip forcefully by 2 commands:**
    1- "conda config --add channels conda-forge"
    2- "conda insatll pip=18.0"


**2-you have to update or install protobuf library version=3.6.1 using following command:**
    1- "pip insatll protobuf=3.6.1"


**3-if protobuf cannot istall following libraries with it you have to also install them:**
   1- six 1.11.1 (Query: pip install six=1.11.0).
   2- setuptools 40.6.2 (Query: pip install setuptools=40.6.2)
   3- ortools 6.10.6025 (Query: pip install ortools=6.10.6025)
**"pywrapcp"** this function will help for the creation of the solver.
**.IntVar() **function is used to add the varriable and domains
**.Add()** function is used to add constraints.

follow is the code for this problem:

    ## Author: Ahsan Azeem
## Solution of Crossword using ortools library
from ortools.constraint_solver import pywrapcp
def main():
  solver = pywrapcp.Solver("Problem")
  alpha = "_abcdefghijklmnopqrstuvwxyz"
  a = 1
  b = 2
  c = 3
  d = 4
  e = 5
  f = 6
  g = 7
  h = 8
  i = 9
  j = 10
  k = 11
  l = 12
  m = 13
  n = 14
  o = 15
  p = 16
  q = 17
  r = 18
  s = 19
  t = 20
  u = 21
  v = 22
  w = 23
  x = 24
  y = 25
  z = 26
  num_words = 15
  word_len = 5
  AA = [
      [h, o, s, e, s],  # HOSES
      [l, a, s, e, r],  # LASER
      [s, a, i, l, s],  # SAILS
      [s, h, e, e, t],  # SHEET
      [s, t, e, e, r],  # STEER
      [h, e, e, l, 0],  # HEEL
      [h, i, k, e, 0],  # HIKE
      [k, e, e, l, 0],  # KEEL
      [k, n, o, t, 0],  # KNOT
      [l, i, n, e, 0],  # LINE
      [a, f, t, 0, 0],  # AFT
      [a, l, e, 0, 0],  # ALE
      [e, e, l, 0, 0],  # EEL
      [l, e, e, 0, 0],  # LEE
      [t, i, e, 0, 0]  # TIE
  ]
  num_overlapping = 12
  overlapping = [
      [0, 2, 1, 0],  # s
      [0, 4, 2, 0],  # s
      [3, 1, 1, 2],  # i
      [3, 2, 4, 0],  # k
      [3, 3, 2, 2],  # e
      [6, 0, 1, 3],  # l
      [6, 1, 4, 1],  # e
      [6, 2, 2, 3],  # e
      [7, 0, 5, 1],  # l
      [7, 2, 1, 4],  # s
      [7, 3, 4, 2],  # e
      [7, 4, 2, 4]  # r
  ]
  n = 8
  A = {}
  for I in range(num_words):
    for J in range(word_len):
      A[(I, J)] = solver.IntVar(0, 26, "A(%i,%i)" % (I, J))
  A_flat = [A[(I, J)] for I in range(num_words) for J in range(word_len)]
  E = [solver.IntVar(0, num_words, "E%i" % I) for I in range(n)]
  solver.Add(solver.AllDifferent(E))
  for I in range(num_words):
    for J in range(word_len):
      solver.Add(A[(I, J)] == AA[I][J])
  for I in range(num_overlapping):
    # This is what I would do:
    # solver.Add(A[(E[overlapping[I][0]], overlapping[I][1])] ==  A[(E[overlapping[I][2]], overlapping[I][3])])
    # But we must use Element explicitly
    solver.Add(
        solver.Element(
            A_flat, E[overlapping[I][0]] * word_len + overlapping[I][1]) ==
        solver.Element(
            A_flat, E[overlapping[I][2]] * word_len + overlapping[I][3]))
  solution = solver.Assignment()
  solution.Add(E)
  db = solver.Phase(E + A_flat,
                    solver.INT_VAR_SIMPLE,
                    solver.ASSIGN_MIN_VALUE)
  solver.NewSearch(db)
  num_solutions = 0
  while solver.NextSolution():
    print(E)
    print_solution(A, E, alpha, n, word_len)
    num_solutions += 1
  solver.EndSearch()
  print()
  print("num_solutions:", num_solutions)
  print("failures:", solver.Failures())
def print_solution(A, E, alpha, n, word_len):
  for ee in range(n):
    print("%i: (%2i)" % (ee, E[ee].Value()), end=' ')
    print("".join(["%s" % (alpha[A[ee, ii].Value()]) for ii in range(word_len)]))
if __name__ == "__main__":
  main()

hope you got you answer