Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
6 lines, 2 for loops. Fully explained. solution in Clear category for The Hidden Word by bavili8766
import itertools # we need this for itertools.zip_longest to arrange the lines into columns
def checkio(text, word):
for a, b in enumerate(text.lower().replace(' ', '').split('\n')): # horizontal check : makes the text lowercase, removes whitespace, then splits the text into a list of the lines
if word in b: # if the word is found in a row :
return [a+1, b.index(word[0])+1, a+1, b.index(word[0])+len(word)] # returns the coordinates
for a, b in enumerate(list(itertools.zip_longest(*[i for i in text.lower().replace(' ', '').split('\n')], fillvalue=''))): # vertical check : uses zip_longest to take the lines we used earlier and organise them into columns
if word in ''.join(b): # if the word is found in a column :
return [b.index(word[0])+1, a+1, b.index(word[0])+len(word), a+1] # returns the coordinates
#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!")
Aug. 6, 2021