Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Look for sure things thanks to comparisons solution in Clear category for Cipher Crossword by Phil15
BLANK = ' '
def checkio(crossword, words):
# 0) Replace zeros by blanks.
crossword = [[n if n else BLANK for n in row] for row in crossword]
lengths = set(map(len, words)) # with tests here it's just {5}.
# 1) Where are words in the crossword of ints? List int words.
nwords = []
for grid in (crossword, map(list, zip(*crossword))):
# line is row with the 1st grid, column with 2nd grid.
for line in grid:
nword = []
for nb in line + [BLANK]:
if nb != BLANK:
nword.append(nb)
else:
if len(nword) in lengths:
nwords.append(nword)
nword = []
# 2) Look for the things we are certain of.
while not all(isinstance(box, str) for row in crossword for box in row):
certain = set() # Nothing for sure yet.
for word in words:
# Compare word with int words of same length.
comparisons = (set(zip(nword, word)) for nword in nwords
if len(nword) == len(word))
# Check if the nword is compatible with the word.
pos = [comp for comp in comparisons
if len(comp) == len(set(word)) and \
all(n == w for n, w in comp if isinstance(n, str))]
if pos:
# What is common in all possibilities we found?
certain |= set.intersection(*pos) # This is then certain.
certain = {n: ch for n, ch in certain if n != ch} # almost dict(certain)
if not certain: return [list("FAIL!")] * 5
# 3) Update crossword and nwords with what we know for sure.
for grid in (crossword, nwords):
for i, row in enumerate(grid):
for j, n in enumerate(row):
grid[i][j] = certain.get(n, n)
print(*map(''.join, crossword), '', sep='\n') # just for fun
return crossword
Dec. 2, 2018