Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First: a little clumsy but works solution in Clear category for The Hidden Word by hbczmxy
def checkio(text, word):
text = text.lower()
result = [0, 0, 0, 0]
# if the word in one line
# Find words in each line in turn
# Once found, exit the loop and return the final result
lines = [line.replace(' ', '') for line in text.split('\n')]
for num, line in enumerate(lines, 1):
start_colunm = line.find(word)
if start_colunm != -1:
result[0], result[2] = num, num
result[1], result[3] = start_colunm + 1, start_colunm + len(word)
break
if start_colunm != -1:
return result
# if the word in one column
# Find the length of the longest line
longest_line_length = max(len(line) for line in lines)
# make a longest * longest list
# Assigns each character in the text to the corresponding position in the list by line
chars = []
for i in range(longest_line_length):
temp = []
for j in range(longest_line_length):
try:
temp.append(lines[i][j])
except IndexError:
temp.append(' ')
else:
chars.append(temp)
# Assigns each character in the text to the corresponding position in the list by line
columns = [ ['' for j in range(longest_line_length)] for i in range(longest_line_length) ]
for i in range(longest_line_length):
for j in range(longest_line_length):
columns[i][j] = chars[j][i]
else:
columns[i] = ''.join(columns[i]).strip()
# Find words in each column in turn
# Once found, exit the loop and return the final result
for num, column in enumerate(columns, 1):
start_line = column.find(word)
if start_line != -1:
result[1], result[3] = num, num
result[0], result[2] = start_line + 1, start_line + len(word)
break
if start_line != -1:
return result
#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]
assert checkio("Hi all!\nAnd all goodbye!\nOf course goodbye.\nor not","haoo") == [1, 1, 4, 1]
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 24, 2022