Welcome, guest | Sign In | My Account | Store | Cart

crossword puzzle solver

Python, 208 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import sys
def read_board(board_filename):
	bd = []
	for line in open(board_filename):
		bd.append(line.strip())
	return bd

def print_board(board):
	for i in range(0,len(board)):
		for j in range(0,len(board[i])):
			sys.stdout.write(board[i][j])
			sys.stdout.write(' ')
		print
		
	# This function should take in a list of strings and print the board
def word_going_right(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		f += 1
		if col >= len(board[0]):
			return False
def word_going_left(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		
		if word[i] != board[row][col]:
			return False
		i += 1
		f += 1
		col -= 1
		if col <= 0:
			return False
def word_going_up(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		f += 1
		row -= 1
		if row <= 0:
			return False
def word_going_down(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		row += 1
		if row >= len(board):
			return False

def word_going_downright(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		row += 1
		col += 1
		if row >= len(board) or col >= len(board[0]):
			return False
def word_going_downleft(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		row += 1
		col -= 1
		if row >= len(board) or col <= 0:
			return False
def word_going_upright(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		f += 1
		row -= 1
		col += 1
		if row < 0 or col >= len(board[0]):
			return False
def word_going_upleft(board,word,row,col):
	i = 0
	f = 0
	while True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			return True
		if word[i] != board[row][col]:
			return False
		i += 1
		row -= 1
		col -= 1
		if row <= 0 or col <= 0:
			return False


def find_word_in_puzzle(board,word):
	
	for r in range(0,len(board)):
		for c in range(0,len(board)):
			if word_going_right(board,word,r,c) == True:
				row = r
				col = c
				direction = 'right'
				return True
			if word_going_left(board,word,r,c) == True:
				row = r
				col = c
				direction = 'left'
				return True
			if word_going_up(board,word,r,c) == True:
				row = r
				col = c
				direction = 'up'
				return True
			if word_going_down(board,word,r,c) == True:
				row = r
				col = c
				direction = 'down'
				return True
			if word_going_downright(board,word,r,c) == True:
				row = r
				col = c
				direction = 'down and right'
				return True
			if word_going_downleft(board,word,r,c) == True:
				row = r
				col = c
				direction = 'down and left'
			if word_going_upright(board,word,r,c) == True:
				row = r
				col = c
				direction = 'up and right'
			if word_going_upleft(board,word,r,c) == True:
				row = r
				col = c
				direction = 'up and left'
		if True:
			"%s starts at (%d, %d) going" % (word,r,c)
		if False:
			"%s can't be found" % (word)
		
	# This function shoud call eight functions (one for each direction)
	# An example call:  is_word_going_right(board, word, row, col)
        # See the HW 5 pdf.
	

if __name__ == '__main__':
	board_filename = raw_input("Enter the file containing the board ==> ")
	print board_filename
	print # print an empty line

	# board is a list of strings (a bit simpler than the Sudoku structure)
	board = read_board(board_filename)

	# print the board, you must write this function
	print_board(board)

	print # print an empty line
	words_filename = raw_input("Enter the file containing the words ==> ")
	print words_filename

	print # print an empty line
	for line in open(words_filename):
		word = line.strip()
		find_word_in_puzzle(board, word)
Created by Tommy Fang on Thu, 17 Oct 2013 (MIT)
Python recipes (4591)
Tommy Fang's recipes (2)

Required Modules

Other Information and Tasks