Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
loops and zip_longest solution in Clear category for The Hidden Word by krzysztofrrr
from itertools import zip_longest
def checkio(text, word):
# remove spaces
text = text.replace(' ','')
# build 2d array
horizontal_ar = [list(line) for line in text.split()]
# iterate horizontally
for linenum, line in enumerate(horizontal_ar):
f = ''.join(line).lower().find(word)
if f>=0:
return [linenum+1, f+1,linenum+1,f+len(word)]
# transpose horizontal_ar array
transposed_ar = [list(column) for column in zip_longest(*horizontal_ar, fillvalue='')]
# iterate vertically
for colnum, line in enumerate(transposed_ar):
f = ''.join(line).lower().find(word)
if f>=0:
return [f+1, colnum+1, f+len(word),colnum+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]
May 17, 2020