Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
horizontal and vertical search solution in Clear category for The Hidden Word by l.szyman10
def checkio(text, word):
text = [''.join([i.lower() for i in line if i != ' ']) for line in text.splitlines()]
#horizontal checking
for y, line in enumerate(text, 1):
if word in line:
x = line.index(word) + 1
return [y, x, y, x + len(word) - 1]
#vertical checking
for y, line in enumerate(text[:-len(word) + 1]):
ind = 0
for j in range(line.count(word[0])):
pos = line.index(word[0], ind)
try:
to_check = ''.join([line[pos] for line in text[y:y+len(word)]])
if to_check == word:
return [y + 1, pos + 1, y + len(word), pos + 1]
except IndexError:
pass
ind = pos + 1
return []
#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]
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 21, 2020