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

solves crossword puzzle by outputting starting location of word and and its direction

Python, 262 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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
	v = True
	while v == True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[row][col]:
			v = True
			break
		i += 1
		f += 1
		if col >= len(board[0]):
			v = False
			break
	return v
def word_going_left(board,word,row,col):
	i = 0
	f = 0
	v = True
	while v == True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[row][col]:
			v = False
			break
		i += 1
		f += 1
		col -= 1
		if col <= 0:
			v = False
			break
	return v
def word_going_up(board,word,row,col):
	i = 0
	f = 0
	v = True
	while v == True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[row][col]:
			v = False
			break
		i += 1
		f += 1
		row -= 1
		if row <= 0:
			v = False
			break
	return v
def word_going_down(board,word,row,col):
	i = 0
	f = 0
	v = True
	while v == True:
		if word[i] == board[row][col]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[row][col]:
			v = False
			break
		i += 1
		row += 1
		if row >= len(board):
			v = False
			break
	return v

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


def find_word_in_puzzle(board,word):
	value = True
	new_row = 0
	new_col = 0
	direction = ''
	for r in range(0,len(board)):
		for c in range(0,len(board)):
			if word_going_right(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'right'
				value = True
				break
			if word_going_left(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'left'
				value = True
				break
			if word_going_up(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'up'
				value = True
				break
			if word_going_down(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'down'
				value = True
				break
			if word_going_downright(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'down and right'
				value = True
				break
			if word_going_downleft(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'down and left'
				value = True
				break
			if word_going_upright(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'up and right'
				value = True
				print "%s starts at (%d, %d) going %s" % (word,new_row,new_col,direction)
				break
			if word_going_upleft(board,word,r,c) == True:
				new_row = r
				new_col = c
				direction = 'up and left'
				value = True
				break
	if value == True:
		print "%s starts at (%d, %d) going %s" % (word,new_row,new_col,direction)
	if value == False:
		print "%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)

doesn't print out solutions

Created by Tommy Fang on Thu, 17 Oct 2013 (MIT)
Python recipes (4591)
Tommy Fang's recipes (2)

Required Modules

Other Information and Tasks