Context
How would you find words in this list diagonally? Note: To show that a word has been found, it can be replaced by any character such as '-'
grid = ["HXXWXXXXXD",
"XOXXOXXXOX",
"XXRXXCXGXX",
"XXXSXXXXXT",
"XXXXEXXXEX"]
I figured that all possibilities of diagonals in the grid are:
- Going downwards and left
- Going downwards and right
- Going upwards and left
- Going upwards and right
The words to find, in this instance would be:
words = ["HORSE","COW","DOG","ET"] # don't ask
How I found words vertically and horizontally
Finding words diagonally seems much more difficult than horizontally or vertically. When finding words horizontally, I could just loop through each row in grid and each word in words. I could then replace word in row with a symbol * len(word)to illustrate that it had been found. Vertically, I rotated the grid 90° clockwise and then did the same process of looping through the lists horizontally. I then rotated the list back to its original state.
What different solutions are there to finding words diagonally?