Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File: WordSearch.py
- # Description:
- # Student Name: Justin Leung
- # Student UT EID: JSL2333
- # Partner Name:
- # Partner UT EID:
- # Course Name: CS 303E
- # Unique Number: 51635
- # Date Created: 04/15/15
- # Date Last Modified: 04/15/15
- #CHANGE NUM_COLUMNS TO NUM ROWS
- import string
- def Square(rows, columns):
- in_file = open ("hidden.txt", "r")
- line = in_file.readline()
- line = in_file.readline()
- square = []
- for i in range(int(rows)):
- line = in_file.readline()
- line = line.replace(" ","")
- line = line.strip()
- square.append(line)
- return square
- #print(square)
- def Words(rows):
- square= []
- in_file = open ("hidden.txt", "r")
- line = in_file.readline()
- line = in_file.readline()
- for i in range(int(rows)):
- line = in_file.readline()
- line = in_file.readline()
- line = in_file.readline()
- line = line.strip()
- num_words = line
- square.append(num_words)
- #print(num_words)
- for i in range(int(num_words)):
- line = in_file.readline()
- line = line.strip()
- square.append(line)
- return square
- #print(square)
- def FindHorizontalWord(num_rows, square, word_list):
- for i in range(1, int(word_list[0]) + 1):
- check_word = word_list[i]
- reverse_word = check_word[::-1]
- #print(word_list[i])
- for j in range(int(num_rows)):
- #print (square[j])
- check = check_word in square[j]
- check_reverse = reverse_word in square[j]
- if (check):
- print(word_list[i])
- print(j + 1, square[j].find(check_word) + 1)
- elif (check_reverse):
- print(word_list[i])
- print(j + 1, square[j].find(reverse_word)+ len(reverse_word))
- else:
- #print("not in this row")
- continue
- def FindVerticalWord(num_rows, square, word_list):
- for i in range(1, int(word_list[0]) + 1):
- check_word = word_list[i]
- reverse_word = check_word[::-1]
- #print(word_list[i])
- for j in range(int(num_rows)):
- #print (square[j])
- check = check_word in square[j]
- check_reverse = reverse_word in square[j]
- if (check):
- print(word_list[i])
- print(square[j].find(check_word) + 1, j + 1)
- elif (check_reverse):
- print(word_list[i])
- print(square[j].find(reverse_word)+ len(reverse_word), j + 1)
- else:
- print(word_list[i])
- print("0 0")
- def newSquare(columns, square):
- transpose_square = []
- for j in range (len(square[0])):
- new_square = []
- for i in range (len(square)):
- new_square.append(square[i][j])
- transpose_square.append(new_square)
- vertical_square = []
- for i in range (int(columns)):
- temp_string = ''
- row = temp_string.join(transpose_square[i])
- vertical_square.append(row)
- return vertical_square
- #return transpose_square
- def main():
- #open file for reading
- in_file = open ("hidden.txt", "r")
- #read number of columns and characters in each row
- line = in_file.readline()
- line = line.strip()
- dim = line.split()
- num_rows, num_columns = dim
- #print(num_columns, num_rows)
- #print(Square(num_columns, num_rows))
- #print(newSquare(num_columns, Square(num_columns, num_rows)))
- #print(Words(num_columns))
- FindHorizontalWord(num_rows, Square(num_rows, num_columns), Words(num_rows))
- print("\n")
- FindVerticalWord(num_columns, newSquare(num_columns, Square(num_rows, num_columns)), Words(num_columns))
- #should above be num columns or num rows
- #newSquare(Square(num_rows, num_columns))
- #print (result)
- #Words(num_columns)
- # close file
- in_file.close()
- #print (num_columns + ' ' + num_rows)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement