Sha256: 44c0a491a79e03095ea4e2cf9430a84da4dc101f41030b86b8b3c5cf666de217

Contents?: true

Size: 1.63 KB

Versions: 36

Compression:

Stored size: 1.63 KB

Contents

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

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
trackler-2.0.8.38 tracks/python/exercises/word-search/example.py
trackler-2.0.8.37 tracks/python/exercises/word-search/example.py
trackler-2.0.8.36 tracks/python/exercises/word-search/example.py
trackler-2.0.8.35 tracks/python/exercises/word-search/example.py
trackler-2.0.8.34 tracks/python/exercises/word-search/example.py
trackler-2.0.8.33 tracks/python/exercises/word-search/example.py
trackler-2.0.8.32 tracks/python/exercises/word-search/example.py
trackler-2.0.8.31 tracks/python/exercises/word-search/example.py
trackler-2.0.8.30 tracks/python/exercises/word-search/example.py
trackler-2.0.8.29 tracks/python/exercises/word-search/example.py
trackler-2.0.8.28 tracks/python/exercises/word-search/example.py
trackler-2.0.8.27 tracks/python/exercises/word-search/example.py
trackler-2.0.8.26 tracks/python/exercises/word-search/example.py
trackler-2.0.8.24 tracks/python/exercises/word-search/example.py
trackler-2.0.8.23 tracks/python/exercises/word-search/example.py
trackler-2.0.8.22 tracks/python/exercises/word-search/example.py
trackler-2.0.8.21 tracks/python/exercises/word-search/example.py
trackler-2.0.8.20 tracks/python/exercises/word-search/example.py
trackler-2.0.8.19 tracks/python/exercises/word-search/example.py
trackler-2.0.8.18 tracks/python/exercises/word-search/example.py