Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Refactored solution in Creative category for The Hidden Word by nickie
from itertools import zip_longest
def checkio(text, word):
search = lambda t: max((l.find(word), i) for i, l in enumerate(t))
t = ["".join(c for c in l if c != ' ') for l in text.lower().splitlines()]
hor = search(t)
if hor[0] >= 0: return [hor[1]+1, hor[0]+1, hor[1]+1, hor[0]+len(word)]
ver = search("".join(l) for l in zip_longest(*t, fillvalue=' '))
if ver[0] >= 0: return [ver[0]+1, ver[1]+1, ver[0]+len(word), ver[1]+1]
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio("""DREAMING of apples on a wall,
And dreaming often, dear,
I dreamed that, if I counted all,
-How many would appear?""", "ten") == [2, 14, 2, 16]
assert checkio("""He took his vorpal sword in hand:
Long time the manxome foe he sought--
So rested he by the Tumtum tree,
And stood awhile in thought.
And as in uffish thought he stood,
The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
And burbled as it came!""", "noir") == [4, 16, 7, 16]
Feb. 15, 2014
Comments: