Welcome, guest | Sign In | My Account | Store | Cart
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)):
			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,r,c):
	i = 0
	f = 0
	v = True
	while v == True:
		if word[i] == board[r][c]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[r][c]:
			v = False
			break
		i += 1
		c += 1
		if c >= len(board[0]):
			v = False
			break
	return v
def word_going_left(board,word,r,c):
	i = 0
	f = 0
	while True:
		if word[i] == board[r][c]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[r][c]:
			v = False
			break
		i += 1
		c -= 1
		if c <= 0:
			v = False
			break
	return v
def word_going_up(board,word,r,c):
	i = 0
	f = 0
	v = True
	while True:
		if word[i] == board[r][c]:
			f += 1
		if f == len(word):
			v = True
			break
		if word[i] != board[r][c]:
			v = False
			break
		i += 1
		r -= 1
		if r <= 0:
			v = False
			break
	return v
def word_going_down(board,word,r,c):
	i = 0
	f = 0
	v = True
	while  True:
		if word[i] == board[r][c]:
			f += 1
		else:
			v = False
		if f == len(word):
			v = True
			break
		if word[i] != board[r][c]:
			v = False
			break
		i += 1
		r += 1
		if r >= len(board):
			v = False
			break
	return v

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


def find_word_in_puzzle(board,word):
	row = 0
	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:
				row = r
				col = c
				direction = 'right'
				find = True
				break
			if word_going_left(board,word,r,c) == True:
				row = r
				col = c
				direction = 'left'
			if word_going_up(board,word,r,c) == True:
				row = r
				col = c
				direction = 'up'
				find = True
				break
			if word_going_down(board,word,r,c) == True:
				row = r
				col = c
				direction = 'down'
			if word_going_downright(board,word,r,c) == True:
				row = r
				col = c
				direction = 'down and right'
			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 %s" % (word,row,col,direction)
		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)

History