# Read a crossword with a twist!

Similar to this question, but this is a crossword variation!

Instead of only one letter per grid square, you can have one or two.

# Input:

• A 2d array, or whatever works in your language.
• You can assume valid inputs
• Any array size must work

# Output:

• An array of all the words
• Across and down
• All words must be joined together, i.e. linked in an unbroken chain of words (if not return false)
• Words must be at least two grid squares, not letters

### Example:

[["",  "wo", "r",  "k"],
[ "",   "r",  "",   ""],
[ "he", "l",  "lo", ""],


### Returns:

["work", "world", "hello", "load", "dad"]


### Example:

[["he", "ll", "o"],
[ "",   "",   ""],
[ "wo", "r",  "ld"]]


### Returns:

false


This is , so I'll be running this on windows 7 with 2.5ghz and 16gb of ram. If your code is really esoteric, provide a link to the compiler so I can actually run it.

• Welcome to PPCG! Dec 18, 2016 at 18:10
• You should replace the two spaces part with two grid squares. Dec 19, 2016 at 14:04
• What size input size will the speed be measured with? Dec 19, 2016 at 14:06
• @MartinEnder the examples Dec 19, 2016 at 19:14
• @epicbob57 That seems way to small to measure reliable timings. You'd mostly be measuring I/O and other overhead. Dec 19, 2016 at 19:15

# Python 3

import numpy
from scipy.ndimage import measurements

def crosswords(arr):
M=numpy.asarray(arr)
# check connectivity
if measurements.label(numpy.where(M!='',1,0))[-1] != 1:
return 'false'

words = []
def get_words(mat):
for r in mat:
word,counter='',0
for c in r:
if c=='':
if counter>1:
words.append(word)
word, counter = '', 0
else:
word, counter = word+c, counter+1
if counter > 1:
words.append(word)
get_words(M)
# transpose M
get_words(M.T)
return words


# Usage:

Function takes an array of array of strings as input:

crosswords( [["", "wo", "r", "k"], [ "", "r", "", ""], [ "he", "l", "lo", ""], [ "", "d", "ad", ""]])

Returns the string false when connectivity returns multiple labels. Returns array of valid words otherwise.

I timed it with timeit, time.time() and using the console command time and but I don't know which one to use or which one to post here.

• I realized that I don't have Python 3... anyway, I'll be testing it using time.time() Dec 19, 2016 at 19:18
• I can't seem to install scipy using pip... Dec 20, 2016 at 0:16
• did you use pip3 ? Dec 20, 2016 at 0:16
• pip 9.0.1 (python 3.5) Dec 20, 2016 at 0:19
• oh you are on windows, try it with admin priviliges Dec 20, 2016 at 0:28