Cross Words Solver in python (unfinished)

D

Deleted member 36445

Guest
Hey guys, im a cuber from brazil. I have a sub15 avg of 12.

But I'm posting this to ask help in a little python script I wrote to solve a cross words puzzle.

The puzzle consist on finding english words in a 3x8 grid, you can go diagonal or straight but can't come back on a string of words.

67wa.png


My script abstract the grid and it have a findNeighbor function, which gives the neighbors of a given letter.

Here is a snippet:

Code:
letters = 'wololfmacroonemnrdoahcar'

a = generateGrid(3, 8)

[[[1, []], [2, []], [3, []]],
 [[4, []], [5, []], [6, []]],
 [[7, []], [8, []], [9, []]],
 [[10, []], [11, []], [12, []]],
 [[13, []], [14, []], [15, []]],
 [[16, []], [17, []], [18, []]],
 [[19, []], [20, []], [21, []]],
 [[22, []], [23, []], [24, []]]]

for n, letter in enumerate(letters):
  x, y = getN(n+1, a)
  a[x][y][1] = letter

pprint.pprint(a)

[[[1, 'w'], [2, 'o'], [3, 'l']],
 [[4, 'o'], [5, 'l'], [6, 'f']],
 [[7, 'm'], [8, 'a'], [9, 'c']],
 [[10, 'r'], [11, 'o'], [12, 'o']],
 [[13, 'n'], [14, 'e'], [15, 'm']],
 [[16, 'n'], [17, 'r'], [18, 'd']],
 [[19, 'o'], [20, 'a'], [21, 'h']],
 [[22, 'c'], [23, 'a'], [24, 'r']]]

findNeighbors(1, a)

[[4, 'o'], [2, 'o'], [5, 'l']]

#==============================================
# The problem:

def loop(i, w, a):
  return

def test(w, a):
  starters = [n for n, i in enumerate(letters) if i == w[0]]
  l = []
  if len(starters) > 0:
    for i in starters:
      l.append(loop(i, w, a))
  return l

But I coudn't wrote the loop function which should iterate trough the neighbors and find words on a dictionary. Could someone help me write this function?

Here is the code:

Code:
import pprint

letters = 'wololfmacroonemnrdoahcar'

english = 'here goes the dictionaty of english words to iterate'

def generateGrid(x, y):
  l = [[m,[]] for m in range(1, x*y+1)]
  return [l[i:i+x] for i in range(0, len(l), x)]

def getN(n, a):
  m = [[o[0] for o in i] for i in a]
  return [[m.index(i), i.index(n)] for i in m if n in i][0]

def outbound(x, y, a):
  try:
    if -1 in [x, y]: return True
    a[x][y]
    return False
  except: return True

def findNeighbors(x, a):
  l = []
  points = getN(x, a)
  ll = [[points[0]-1, points[1]-1],
   [points[0], points[1]-1],
   [points[0]+1, points[1]-1],
   [points[0]-1, points[1]],
   [points[0]+1, points[1]],
   [points[0]-1, points[1]+1],
   [points[0], points[1]+1],
   [points[0]+1, points[1]+1]]
  for i in ll:
    if not outbound(i[0], i[1], a): l.append(a[i[0]][i[1]])
  return l

a = generateGrid(3, 8)

for n, letter in enumerate(letters):
  x, y = getN(n+1, a)
  a[x][y][1] = letter

def hasNeighbor(n, l, a):
  return [x for x,y in findNeighbors(n, a) if y==l]

def loop(i, w, a):
  """Houston, we have a problem"""
  pass

def test(w, a):
  starters = [n for n, i in enumerate(letters) if i == w[0]]
  l = []
  if len(starters) > 0:
    for i in starters:
      l.append(loop(i, w, a))
  return l

test('spam', a)
 

Attachments

  • 67wa.png
    67wa.png
    17.9 KB · Views: 2
Last edited by a moderator:
Back
Top