Skip to content
This repository has been archived by the owner on Feb 12, 2018. It is now read-only.

Commit

Permalink
READY for code review now.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtorgan committed Dec 10, 2012
1 parent 6131a64 commit c6b97b2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
10 changes: 10 additions & 0 deletions torgan/AbstractGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __extract_words_coordinates(self, list):
return words_coordinates

def parse_matrix(self, matrix):
self.__check_format(matrix)
for i in xrange(len(matrix)):
words_coordinates = self.__extract_words_coordinates(matrix[i])
for (x1, x2) in words_coordinates:
Expand All @@ -42,3 +43,12 @@ def __build_tree(self, parent, is_vertical):
parent.children.append(self.__build_tree(cross, not is_vertical))
return parent

def __check_format(self, list):
if len(list) == 0:
raise TypeError
for line in list:
isRight = True
for i in line:
isRight &= i == '*' or i == '_'
if not isRight:
raise TypeError
68 changes: 39 additions & 29 deletions torgan/Main.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
from Crossword import Crossword
import sys

file = open("grid.txt")
input_grid = []
for line in file.readlines():
input_grid.append(line.split())
file.close()

file = open("dict.txt")
input_dict = []
for line in file.readlines():
input_dict.extend(line.split())
file.close()

# TODO: add exception handling for: working with files, validate input - grid's format

crossword = Crossword()
crossword.parse_matrix(input_grid)
is_right = crossword.fill_with_words(input_dict)

if is_right:
for word in crossword.horizontal_words:
if word.value is not None:
input_grid[word.item][word.start : word.end + 1] = word.value
for word in crossword.vertical_words:
if word.value is not None:
for line in input_grid:
if word.start <= input_grid.index(line) <= word.end:
line[word.item] = word.value[input_grid.index(line) - word.start]
for line in input_grid:
print line
else:
print "Error! Can not fill crossword with that dictionary!"

try:

file = open("grid.txt")
for line in file.readlines():
input_grid.append(line.split())
file.close()

file = open("dict.txt")
for line in file.readlines():
input_dict.extend(line.split())
file.close()

crossword = Crossword()
crossword.parse_matrix(input_grid)
is_right = crossword.fill_with_words(input_dict)

if is_right:
for word in crossword.horizontal_words:
if word.value is not None:
input_grid[word.item][word.start : word.end + 1] = word.value
for word in crossword.vertical_words:
if word.value is not None:
for line in input_grid:
if word.start <= input_grid.index(line) <= word.end:
line[word.item] = word.value[input_grid.index(line) - word.start]
for line in input_grid:
print line
else:
print "Can not fill crossword with that dictionary!"

except IOError as e:
print "Error: ", e.strerror, e.filename
except TypeError:
print "Error: wrong format of the input crossword grid - empty or contains wrong chars!"
except:
print "Unexpected error:", sys.exc_info()[0]
raise



Expand Down

0 comments on commit c6b97b2

Please sign in to comment.