Advertisement
Guest User

joy3

a guest
Apr 18th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. # File: WordSearch.py
  2.  
  3. # Description:
  4.  
  5. # Student Name: Justin Leung
  6.  
  7. # Student UT EID: JSL2333
  8.  
  9. # Partner Name:
  10.  
  11. # Partner UT EID:
  12.  
  13. # Course Name: CS 303E
  14.  
  15. # Unique Number: 51635
  16.  
  17. # Date Created: 04/15/15
  18.  
  19. # Date Last Modified: 04/15/15
  20.  
  21. #CHANGE NUM_COLUMNS TO NUM ROWS
  22.  
  23. import string
  24.  
  25. def Square(rows, columns):
  26. in_file = open ("hidden.txt", "r")
  27. line = in_file.readline()
  28. line = in_file.readline()
  29.  
  30. square = []
  31. for i in range(int(rows)):
  32. line = in_file.readline()
  33. line = line.replace(" ","")
  34. line = line.strip()
  35. square.append(line)
  36. return square
  37. #print(square)
  38.  
  39. def Words(rows):
  40. square= []
  41. in_file = open ("hidden.txt", "r")
  42. line = in_file.readline()
  43. line = in_file.readline()
  44.  
  45. for i in range(int(rows)):
  46. line = in_file.readline()
  47.  
  48. line = in_file.readline()
  49. line = in_file.readline()
  50. line = line.strip()
  51. num_words = line
  52. square.append(num_words)
  53.  
  54. #print(num_words)
  55.  
  56.  
  57. for i in range(int(num_words)):
  58. line = in_file.readline()
  59. line = line.strip()
  60. square.append(line)
  61. return square
  62. #print(square)
  63.  
  64.  
  65. def FindHorizontalWord(num_rows, square, word_list):
  66.  
  67. for i in range(1, int(word_list[0]) + 1):
  68. check_word = word_list[i]
  69. reverse_word = check_word[::-1]
  70. #print(word_list[i])
  71. for j in range(int(num_rows)):
  72. #print (square[j])
  73. check = check_word in square[j]
  74. check_reverse = reverse_word in square[j]
  75. if (check):
  76. print(word_list[i])
  77. print(j + 1, square[j].find(check_word) + 1)
  78. elif (check_reverse):
  79. print(word_list[i])
  80. print(j + 1, square[j].find(reverse_word)+ len(reverse_word))
  81. else:
  82. #print("not in this row")
  83. continue
  84.  
  85. def FindVerticalWord(num_rows, square, word_list):
  86.  
  87. for i in range(1, int(word_list[0]) + 1):
  88. check_word = word_list[i]
  89. reverse_word = check_word[::-1]
  90. #print(word_list[i])
  91. for j in range(int(num_rows)):
  92. #print (square[j])
  93. check = check_word in square[j]
  94. check_reverse = reverse_word in square[j]
  95. if (check):
  96. print(word_list[i])
  97. print(square[j].find(check_word) + 1, j + 1)
  98. elif (check_reverse):
  99. print(word_list[i])
  100. print(square[j].find(reverse_word)+ len(reverse_word), j + 1)
  101. else:
  102. print(word_list[i])
  103. print("0 0")
  104.  
  105.  
  106. def newSquare(columns, square):
  107. transpose_square = []
  108. for j in range (len(square[0])):
  109. new_square = []
  110. for i in range (len(square)):
  111. new_square.append(square[i][j])
  112. transpose_square.append(new_square)
  113.  
  114. vertical_square = []
  115. for i in range (int(columns)):
  116. temp_string = ''
  117. row = temp_string.join(transpose_square[i])
  118. vertical_square.append(row)
  119.  
  120.  
  121. return vertical_square
  122. #return transpose_square
  123.  
  124.  
  125.  
  126. def main():
  127.  
  128. #open file for reading
  129. in_file = open ("hidden.txt", "r")
  130.  
  131. #read number of columns and characters in each row
  132. line = in_file.readline()
  133. line = line.strip()
  134. dim = line.split()
  135. num_rows, num_columns = dim
  136. #print(num_columns, num_rows)
  137.  
  138. #print(Square(num_columns, num_rows))
  139. #print(newSquare(num_columns, Square(num_columns, num_rows)))
  140. #print(Words(num_columns))
  141.  
  142. FindHorizontalWord(num_rows, Square(num_rows, num_columns), Words(num_rows))
  143. print("\n")
  144. FindVerticalWord(num_columns, newSquare(num_columns, Square(num_rows, num_columns)), Words(num_columns))
  145. #should above be num columns or num rows
  146.  
  147. #newSquare(Square(num_rows, num_columns))
  148.  
  149.  
  150.  
  151. #print (result)
  152.  
  153. #Words(num_columns)
  154.  
  155. # close file
  156. in_file.close()
  157.  
  158. #print (num_columns + ' ' + num_rows)
  159.  
  160.  
  161. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement