[CentralOH] DoJo Problem for Tonight
Matthew DuCharme
ducharmemp at gmail.com
Thu Mar 21 23:35:32 EDT 2019
Since everyone has gone for the readable solutions so far, here's mine. I
wish I could figure out some way to avoid allocating the extra strings from
the slices overall.
from itertools import chain, product
def is_word_in_puzzle(word, puzzle):
rows = len(puzzle)
columns = len(puzzle[0])
puzzle = "".join(chain.from_iterable(puzzle))
return any(
word in (
puzzle[i * columns: i * columns + columns], puzzle[j::columns]
) for i, j in product(range(rows), range(columns))
)
puzzle = [
[ 'F', 'A', 'C', 'I' ],
[ 'O', 'B', 'Q', 'P' ],
[ 'A', 'N', 'O', 'B' ],
[ 'M', 'A', 'S', 'S' ],
]
assert is_word_in_puzzle('FOAM', puzzle) == True
assert is_word_in_puzzle('ABNA', puzzle) == True
assert is_word_in_puzzle('CQOS', puzzle) == True
assert is_word_in_puzzle('ANOBFFF', puzzle) == False
assert is_word_in_puzzle('MASS', puzzle) == True
On Thu, Mar 21, 2019 at 9:27 PM neil ludban <neil.ludban at gmail.com> wrote:
> #!/usr/bin/env python3.7
>
> def row_generator(p):
> for row in p:
> yield ''.join(row)
>
> def col_generator(p):
> for col in zip(*p):
> yield ''.join(col)
>
> def word_in_puzzle(w, p):
> for x in row_generator(p):
> if w in x:
> return True
> for x in col_generator(p):
> if w in x:
> return True
> return False
>
> puzzle = [
> [ 'F', 'A', 'C', 'I' ],
> [ 'O', 'B', 'Q', 'P' ],
> [ 'A', 'N', 'O', 'B' ],
> [ 'M', 'A', 'S', 'S' ],
> ]
>
> print(word_in_puzzle('FOAM', puzzle))
> print(word_in_puzzle('MASS', puzzle))
>
>
> On Thu, Mar 21, 2019 at 3:39 PM Travis Risner <deeppunster at gmail.com>
> wrote:
>
>> Hi folks,
>>
>> If you are interested, here is a problem to consider for tonight’s
>> meeting.
>>
>> ————————
>>
>> Given a 2D matrix of characters and a target word, write a function that
>> returns whether the word can be found in the matrix by going
>> left-to-right, or up-to-down.
>>
>> For example, given the following matrix:
>>
>> [['F', 'A', 'C', 'I'],
>> ['O', 'B', 'Q', 'P'],
>> ['A', 'N', 'O', 'B'],
>> ['M', 'A', 'S', 'S']]
>> and the target word 'FOAM', you should return true, since it's the
>> leftmost column. Similarly, given the target word 'MASS', you should
>> return true, since it's the last row.
>>
>> ————————
>>
>> See you tonight!
>>
>> Travis
>> _______________________________________________
>> CentralOH mailing list
>> CentralOH at python.org
>> https://mail.python.org/mailman/listinfo/centraloh
>>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20190321/baaf11bb/attachment-0001.html>
More information about the CentralOH
mailing list