Sha256: c3cf1858f0c7c3a22ad41148ac98800ffdb51b44edf2870ae90049d81e2be426

Contents?: true

Size: 1.62 KB

Versions: 91

Compression:

Stored size: 1.62 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
        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

91 entries across 91 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/python/exercises/word-search/example.py
trackler-2.2.1.158 tracks/python/exercises/word-search/example.py
trackler-2.2.1.157 tracks/python/exercises/word-search/example.py
trackler-2.2.1.156 tracks/python/exercises/word-search/example.py
trackler-2.2.1.155 tracks/python/exercises/word-search/example.py
trackler-2.2.1.154 tracks/python/exercises/word-search/example.py
trackler-2.2.1.153 tracks/python/exercises/word-search/example.py
trackler-2.2.1.152 tracks/python/exercises/word-search/example.py
trackler-2.2.1.151 tracks/python/exercises/word-search/example.py
trackler-2.2.1.150 tracks/python/exercises/word-search/example.py
trackler-2.2.1.149 tracks/python/exercises/word-search/example.py
trackler-2.2.1.148 tracks/python/exercises/word-search/example.py
trackler-2.2.1.147 tracks/python/exercises/word-search/example.py
trackler-2.2.1.146 tracks/python/exercises/word-search/example.py
trackler-2.2.1.145 tracks/python/exercises/word-search/example.py
trackler-2.2.1.144 tracks/python/exercises/word-search/example.py
trackler-2.2.1.143 tracks/python/exercises/word-search/example.py
trackler-2.2.1.142 tracks/python/exercises/word-search/example.py
trackler-2.2.1.141 tracks/python/exercises/word-search/example.py
trackler-2.2.1.140 tracks/python/exercises/word-search/example.py