forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rewrite tests so that users have more freedom of implementation * update example-implementation to fit test-interface * reduce skeleton to minimal as discussed in exercism#272
- Loading branch information
Showing
3 changed files
with
56 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,22 @@ | ||
import copy | ||
|
||
|
||
class Point(object): | ||
def __init__(self, x, y): | ||
self.x = x | ||
self.y = y | ||
|
||
def __repr__(self): | ||
return 'Point({}:{})'.format(self.x, self.y) | ||
|
||
def __add__(self, other): | ||
return Point(self.x + other.x, self.y + other.y) | ||
|
||
def __sub__(self, other): | ||
return Point(self.x - other.x, self.y - other.y) | ||
|
||
def __eq__(self, other): | ||
return self.x == other.x and self.y == other.y | ||
|
||
def __ne__(self, other): | ||
return not(self == other) | ||
|
||
|
||
DIRECTIONS = (Point(1, 0), Point(1, -1), Point(1, 1), Point(-1, -1), | ||
Point(0, -1), Point(0, 1), Point(-1, 1), Point(-1, 0)) | ||
|
||
|
||
class WordSearch(object): | ||
def __init__(self, puzzle): | ||
self.rows = puzzle.split() | ||
self.width = len(self.rows[0]) | ||
self.height = len(self.rows) | ||
|
||
def find_char(self, coordinate): | ||
if coordinate.x < 0 or coordinate.x >= self.width: | ||
return | ||
if coordinate.y < 0 or coordinate.y >= self.height: | ||
return | ||
return self.rows[coordinate.y][coordinate.x] | ||
|
||
def find(self, word, position, direction): | ||
current = copy.copy(position) | ||
for letter in word: | ||
if self.find_char(current) != letter: | ||
return | ||
current += direction | ||
return position, current - direction | ||
|
||
def search(self, word): | ||
positions = (Point(x, y) for x in range(self.width) for y in range(self.height)) | ||
for pos in positions: | ||
for d in DIRECTIONS: | ||
result = self.find(word, pos, d) | ||
if result: | ||
return result | ||
return None | ||
def find_stop(row, column, word, puzzle): | ||
directions = [(0, 1), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 0), (-1, 1), (-1, -1)] | ||
for d in directions: | ||
row_nr, column_nr = row, column | ||
for char_nr, char in enumerate(word): | ||
try: | ||
if puzzle[row_nr][column_nr] != char: | ||
break | ||
except IndexError: | ||
break | ||
if char_nr == len(word) - 1: | ||
return row_nr, column_nr | ||
row_nr += d[0] | ||
column_nr += d[1] | ||
|
||
|
||
def search(puzzle, word): | ||
for row_nr, row in enumerate(puzzle): | ||
for column_nr, char in enumerate(row): | ||
stop = find_stop(row_nr, column_nr, word, puzzle) | ||
if stop: | ||
return (row_nr, column_nr), stop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,2 @@ | ||
class Point(object): | ||
def __init__(self, x, y): | ||
self.x = x | ||
self.y = y | ||
|
||
def __repr__(self): | ||
return 'Point({}:{})'.format(self.x, self.y) | ||
|
||
def __add__(self, other): | ||
return Point(self.x + other.x, self.y + other.y) | ||
|
||
def __sub__(self, other): | ||
return Point(self.x - other.x, self.y - other.y) | ||
|
||
def __eq__(self, other): | ||
return self.x == other.x and self.y == other.y | ||
|
||
def __ne__(self, other): | ||
return not(self == other) | ||
|
||
|
||
class WordSearch(object): | ||
def __init__(self, puzzle): | ||
pass | ||
|
||
def search(self, word): | ||
return None | ||
def search(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters