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 lisovsky
def checkio(text, word):
rows = text.lower().replace(" ", "").split("\n")
for row_index, row in enumerate(rows):
column_index = row.find(word)
if column_index != -1:
return [
row_index + 1,
column_index + 1,
row_index + 1,
column_index + len(word),
]
max_row_len = max([len(row) for row in rows])
rows = [row.ljust(max_row_len, "#") for row in rows]
for column_index in range(max_row_len):
row_index = "".join([row[column_index] for row in rows]).find(word)
if row_index != -1:
return [
row_index + 1,
column_index + 1,
row_index + len(word),
column_index + 1,
]
return [0, 0, 0, 0]
# 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!")
Sept. 26, 2022