Originally I have a wordsearch puzzle and my goal is to find the words in it given a previous list of words. I was able to successfully find the words in the horizontal and vertical lines, but I'm having trouble find the diagonals.
My main ideia is to take my input of letters and join then in a one big string and then search for the word in this string, returning the line the word belongs to, and after that I just check where it starts, and so on.
For my diagonals I was planning the same, but let's say this is my input:
c l i o a
e a g b e
a r t l s
j i a u a
e e q u b
e u g r r
c i d e p
b i o s b
2
cat
big
Once I convert my diagonals in a joined list this is what I get:
# first diagonal
first_diag = ['b', 'ci', 'eio', 'euds', 'jegeb', 'aiqrp', 'eraur', 'catub', 'lgla', 'ibs', 'oe', 'a']
# second diagonal
sec_diag = ['c', 'le', 'iaa', 'ogrj', 'abtie', 'elaee', 'suquc', 'augib', 'brdi', 'reo', 'ps', 'b']
From that I would take where in my original puzzle the word "cat"
is in row (0) and column (0). The same for the word "big"
[8][0].
Expected output:
c . . . .
. a . . .
. . t . .
. . . . .
. . . . .
. . g . .
. i . . .
b . . . .
How can I do that, or is there another way to do it? tnx in advance.
edit: If needed, I can post the code I have.
At first I'd store the row-/column- values of the diagonal strings in four arrays. Let's say, your matrix has nr rows and nc columns, then
first_diag_r = [max(nr - 1 - i, 0) for i, _ in enumerate(first_diag)]
and
first_diag_c = [min(i, nc - 1) for i, _ in enumerate(first_diag)]
and accordingly for the second_diag.
Now, if you find a word in there, which is not at the beginning of a diagonal string like your two examples above, this offset has to be calculated: For the first_diag this means adding offset to column value and substracting offset from row value.