Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Hidden Word by Pertti64
def checkio(text, word):
rows = [row.lower().replace(' ', '') for row in text.split('\n')]
dims = len(rows), max(map(len, rows))
filled_rows = list(map(lambda el: el + '*'*(dims[1] - len(el)), rows))
# matrix = [list(row) for row in filled_rows] # not needed as you can access strings by index anyway
transposed_rows = [''.join([filled_rows[j][i] for j in range(dims[0])]) for i in range(dims[1])]
def search(word, rows, transposed_rows):
for i, row in enumerate(rows):
place = row.find(word)
if place != -1:
return [i+1, place+1, i+1, place + len(word)]
for j, row in enumerate(transposed_rows):
place = row.find(word)
if place != -1:
return [place+1, j+1, place + len(word), j+1]
return search(word, rows, transposed_rows)
#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!")
July 25, 2021