Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Generator solution in Clear category for The Hidden Word by guido
def checkio(text, word):
text, word = text.lower(), word.lower() # Normalize case
lines = text.splitlines() # Split into lines
lines = [''.join(line.split()) for line in lines] # Remove whitespace
width = max(len(line) for line in lines)
lines = [line + ' '*(width-len(line)) for line in lines] # Make all lines the same size
for answer in findit(lines, word):
return answer # Return first answer
def findit(lines, word):
height = len(lines)
nword = len(word)
for row, line in enumerate(lines):
for col, ch in enumerate(line):
if ch == word[0]:
if line[col:col+nword] == word:
yield [row+1, col+1, row+1, col+nword]
if row + nword <= height:
for i in range(1, nword):
if lines[row+i][col] != word[i]:
break
else:
yield [row+1, col+1, row+nword, col+1]
Feb. 19, 2014
Comments: