Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Check-Rotate-Check solution in Clear category for The Hidden Word by bryukh
from itertools import zip_longest
def check_horizontals(text, word):
"""
Search the word in the text by rows
Args:
text -- A list of strings
word -- our "hidden" word
Return:
The row and column of the first letter of the word
"""
for i, line in enumerate(text):
if word in line:
return i, line.index(word)
return None, None
def checkio(text, word):
"""
Search the "hidden" word in the text.
Args:
text -- Multiline string for search
word -- our "hidden" word
Return:
The list with coordinates of the word
"""
#normalize text in the matrix
text = text.replace(" ", "").lower()
data = text.split("\n")
row, col = check_horizontals(data, word)
if row is not None:
return [row + 1, col + 1, row + 1, col + len(word)]
#transpose text. zip_longest does not truncate rows.
data = ("".join(row) for row in zip_longest(*data, fillvalue=""))
row, col = check_horizontals(data, word)
if row is not None:
return [col + 1, row + 1, col + len(word), row + 1]
raise ValueError("The rhyme does not contain the word.")
Feb. 6, 2014
Comments: