| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 02928df commit b001abc
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,57 +1,22 @@ | |||
| 1 | - import copy | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - class Point(object): | ||
| 5 | - def __init__(self, x, y): | ||
| 6 | - self.x = x | ||
| 7 | - self.y = y | ||
| 8 | - | ||
| 9 | - def __repr__(self): | ||
| 10 | - return 'Point({}:{})'.format(self.x, self.y) | ||
| 11 | - | ||
| 12 | - def __add__(self, other): | ||
| 13 | - return Point(self.x + other.x, self.y + other.y) | ||
| 14 | - | ||
| 15 | - def __sub__(self, other): | ||
| 16 | - return Point(self.x - other.x, self.y - other.y) | ||
| 17 | - | ||
| 18 | - def __eq__(self, other): | ||
| 19 | - return self.x == other.x and self.y == other.y | ||
| 20 | - | ||
| 21 | - def __ne__(self, other): | ||
| 22 | - return not(self == other) | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - DIRECTIONS = (Point(1, 0), Point(1, -1), Point(1, 1), Point(-1, -1), | ||
| 26 | - Point(0, -1), Point(0, 1), Point(-1, 1), Point(-1, 0)) | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - class WordSearch(object): | ||
| 30 | - def __init__(self, puzzle): | ||
| 31 | - self.rows = puzzle.split() | ||
| 32 | - self.width = len(self.rows[0]) | ||
| 33 | - self.height = len(self.rows) | ||
| 34 | - | ||
| 35 | - def find_char(self, coordinate): | ||
| 36 | - if coordinate.x < 0 or coordinate.x >= self.width: | ||
| 37 | - return | ||
| 38 | - if coordinate.y < 0 or coordinate.y >= self.height: | ||
| 39 | - return | ||
| 40 | - return self.rows[coordinate.y][coordinate.x] | ||
| 41 | - | ||
| 42 | - def find(self, word, position, direction): | ||
| 43 | - current = copy.copy(position) | ||
| 44 | - for letter in word: | ||
| 45 | - if self.find_char(current) != letter: | ||
| 46 | - return | ||
| 47 | - current += direction | ||
| 48 | - return position, current - direction | ||
| 49 | - | ||
| 50 | - def search(self, word): | ||
| 51 | - positions = (Point(x, y) for x in range(self.width) for y in range(self.height)) | ||
| 52 | - for pos in positions: | ||
| 53 | - for d in DIRECTIONS: | ||
| 54 | - result = self.find(word, pos, d) | ||
| 55 | - if result: | ||
| 56 | - return result | ||
| 57 | - return None | ||
| 1 | + def find_stop(row, column, word, puzzle): | ||
| 2 | + directions = [(0, 1), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 0), (-1, 1), (-1, -1)] | ||
| 3 | + for d in directions: | ||
| 4 | + row_number, column_number = row, column | ||
| 5 | + for char_number, character in enumerate(word): | ||
| 6 | + try: | ||
| 7 | + if puzzle[row_number][column_number] != character: | ||
| 8 | + break | ||
| 9 | + except IndexError: | ||
| 10 | + break | ||
| 11 | + if char_number == len(word) - 1: | ||
| 12 | + return row_number, column_number | ||
| 13 | + row_number += d[0] | ||
| 14 | + column_number += d[1] | ||
| 15 | + | ||
| 16 | + | ||
| 17 | + def search(puzzle, word): | ||
| 18 | + for row_number, row in enumerate(puzzle): | ||
| 19 | + for column_number, character in enumerate(row): | ||
| 20 | + stop = find_stop(row_number, column_number, word, puzzle) | ||
| 21 | + if stop: | ||
| 22 | + return (row_number, column_number), stop | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,27 +1,2 @@ | |||
| 1 | - class Point(object): | ||
| 2 | - def __init__(self, x, y): | ||
| 3 | - self.x = x | ||
| 4 | - self.y = y | ||
| 5 | - | ||
| 6 | - def __repr__(self): | ||
| 7 | - return 'Point({}:{})'.format(self.x, self.y) | ||
| 8 | - | ||
| 9 | - def __add__(self, other): | ||
| 10 | - return Point(self.x + other.x, self.y + other.y) | ||
| 11 | - | ||
| 12 | - def __sub__(self, other): | ||
| 13 | - return Point(self.x - other.x, self.y - other.y) | ||
| 14 | - | ||
| 15 | - def __eq__(self, other): | ||
| 16 | - return self.x == other.x and self.y == other.y | ||
| 17 | - | ||
| 18 | - def __ne__(self, other): | ||
| 19 | - return not(self == other) | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - class WordSearch(object): | ||
| 23 | - def __init__(self, puzzle): | ||
| 24 | - pass | ||
| 25 | - | ||
| 26 | - def search(self, word): | ||
| 27 | - return None | ||
| 1 | + def search(): | ||
| 2 | + pass | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,83 +1,53 @@ | |||
| 1 | 1 | import unittest | |
| 2 | 2 | ||
| 3 | - from word_search import WordSearch, Point | ||
| 3 | + from word_search import search | ||
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | 6 | class WordSearchTests(unittest.TestCase): | |
| 7 | 7 | ||
| 8 | - @classmethod | ||
| 9 | - def setUpClass(self): | ||
| 10 | - puzzle = ('jefblpepre\n' | ||
| 11 | - 'camdcimgtc\n' | ||
| 12 | - 'oivokprjsm\n' | ||
| 13 | - 'pbwasqroua\n' | ||
| 14 | - 'rixilelhrs\n' | ||
| 15 | - 'wolcqlirpc\n' | ||
| 16 | - 'screeaumgr\n' | ||
| 17 | - 'alxhpburyi\n' | ||
| 18 | - 'jalaycalmp\n' | ||
| 19 | - 'clojurermt') | ||
| 20 | - self.example = WordSearch(puzzle) | ||
| 8 | + puzzle = ['jefblpepre', | ||
| 9 | + 'camdcimgtc', | ||
| 10 | + 'oivokprjsm', | ||
| 11 | + 'pbwasqroua', | ||
| 12 | + 'rixilelhrs', | ||
| 13 | + 'wolcqlirpc', | ||
| 14 | + 'screeaumgr', | ||
| 15 | + 'alxhpburyi', | ||
| 16 | + 'jalaycalmp', | ||
| 17 | + 'clojurermt'] | ||
| 21 | 18 | ||
| 22 | 19 | def test_horizontal_words_left_to_right(self): | |
| 23 | - self.assertEqual( | ||
| 24 | - self.example.search('clojure'), | ||
| 25 | - (Point(0, 9), Point(6, 9)) | ||
| 26 | - ) | ||
| 20 | + self.assertEqual(search(WordSearchTests.puzzle, 'clojure'), ((9, 0), (9, 6))) | ||
| 27 | 21 | ||
| 28 | 22 | def test_horizontal_words_right_to_left(self): | |
| 29 | - self.assertEqual( | ||
| 30 | - self.example.search('elixir'), | ||
| 31 | - (Point(5, 4), Point(0, 4)) | ||
| 32 | - ) | ||
| 23 | + self.assertEqual(search(WordSearchTests.puzzle, 'elixir'), ((4, 5), (4, 0))) | ||
| 33 | 24 | ||
| 34 | 25 | def test_vertical_words_top_to_bottom(self): | |
| 35 | - self.assertEqual( | ||
| 36 | - self.example.search('ecmascript'), | ||
| 37 | - (Point(9, 0), Point(9, 9)) | ||
| 38 | - ) | ||
| 26 | + self.assertEqual(search(WordSearchTests.puzzle, 'ecmascript'), ((0, 9), (9, 9))) | ||
| 39 | 27 | ||
| 40 | 28 | def test_vertical_words_bottom_to_top(self): | |
| 41 | - self.assertEqual( | ||
| 42 | - self.example.search('rust'), | ||
| 43 | - (Point(8, 4), Point(8, 1)) | ||
| 44 | - ) | ||
| 45 | - | ||
| 46 | - def test_diagonal_words_top_left_to_bottom_right(self): | ||
| 47 | - self.assertEqual( | ||
| 48 | - self.example.search('java'), | ||
| 49 | - (Point(0, 0), Point(3, 3)) | ||
| 50 | - ) | ||
| 51 | - | ||
| 52 | - def test_diagonal_upper_bottom_right_to_top_left(self): | ||
| 53 | - self.assertEqual( | ||
| 54 | - self.example.search('lua'), | ||
| 55 | - (Point(7, 8), Point(5, 6)) | ||
| 56 | - ) | ||
| 57 | - | ||
| 58 | - def test_diagonal_upper_bottom_left_to_top_right(self): | ||
| 59 | - self.assertEqual( | ||
| 60 | - self.example.search('lisp'), | ||
| 61 | - (Point(2, 5), Point(5, 2)) | ||
| 62 | - ) | ||
| 63 | - | ||
| 64 | - def test_diagonal_upper_top_right_to_bottom_left(self): | ||
| 65 | - self.assertEqual( | ||
| 66 | - self.example.search('ruby'), | ||
| 67 | - (Point(7, 5), Point(4, 8)) | ||
| 68 | - ) | ||
| 29 | + self.assertEqual(search(WordSearchTests.puzzle, 'rust'), ((4, 8), (1, 8))) | ||
| 30 | + | ||
| 31 | + def test_diagonal_top_left_to_bottom_right(self): | ||
| 32 | + self.assertEqual(search(WordSearchTests.puzzle, 'java'), ((0, 0), (3, 3))) | ||
| 33 | + | ||
| 34 | + def test_diagonal_bottom_right_to_top_left(self): | ||
| 35 | + self.assertEqual(search(WordSearchTests.puzzle, 'lua'), ((8, 7), (6, 5))) | ||
| 36 | + | ||
| 37 | + def test_diagonal_bottom_left_to_top_right(self): | ||
| 38 | + self.assertEqual(search(WordSearchTests.puzzle, 'lisp'), ((5, 2), (2, 5))) | ||
| 39 | + | ||
| 40 | + def test_diagonal_top_right_to_bottom_left(self): | ||
| 41 | + self.assertEqual(search(WordSearchTests.puzzle, 'ruby'), ((5, 7), (8, 4))) | ||
| 69 | 42 | ||
| 70 | 43 | def test_words_that_are_not_in_the_puzzle(self): | |
| 71 | - self.assertIsNone(self.example.search('haskell')) | ||
| 44 | + self.assertIsNone(search(WordSearchTests.puzzle, 'haskell')) | ||
| 72 | 45 | ||
| 73 | 46 | def test_search_differently_sized_puzzles(self): | |
| 74 | - puzzle = ('qwertyuiopz\n' | ||
| 75 | - 'luamsicrexe\n' | ||
| 76 | - 'abcdefghijk') | ||
| 77 | - self.assertEqual( | ||
| 78 | - WordSearch(puzzle).search('exercism'), | ||
| 79 | - (Point(10, 1), Point(3, 1)) | ||
| 80 | - ) | ||
| 47 | + puzzle = ['qwertyuiopz', | ||
| 48 | + 'luamsicrexe', | ||
| 49 | + 'abcdefghijk'] | ||
| 50 | + self.assertEqual(search(puzzle, 'exercism'), ((1, 10), (1, 3))) | ||
| 81 | 51 | ||
| 82 | 52 | ||
| 83 | 53 | if __name__ == '__main__': | |
| Back | FazBrowse Home | New Git URL |
0 commit comments