ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

JBaack's solution

to Word Search in the Lua Track

Published at Dec 13 2019 · 0 comments
Instructions
Test suite
Solution

In word search puzzles you get a square of letters and have to find specific words in them.

For example:

``````jefblpepre
camdcimgtc
oivokprjsm
pbwasqroua
rixilelhrs
wolcqlirpc
screeaumgr
alxhpburyi
jalaycalmp
clojurermt
``````

There are several programming languages hidden in the above square.

Words can be hidden in all kinds of directions: left-to-right, right-to-left, vertical and diagonal.

Given a puzzle and a list of words return the location of the first and last letter of each word.

Running the tests

To run the tests, run the command `busted` from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

word-search_spec.lua

``````local WordSearch = require('word-search')

describe('word-search', function()
local puzzle = {
'jefblpepre',
'camdcimgtc',
'oivokprjsm',
'pbwasqroua',
'rixilelhrs',
'wolcqlirpc',
'screeaumgr',
'alxhpburyi',
'jalaycalmp',
'clojurermt',
}

it('should find horizontal words written left-to-right', function()
local first, last = WordSearch(puzzle).find('clojure')
assert.same({ 1, 10 }, first)
assert.same({ 7, 10 }, last)
end)

it('should find horizontal words written right-to-left', function()
local first, last = WordSearch(puzzle).find('elixir')
assert.same({ 6, 5 }, first)
assert.same({ 1, 5 }, last)
end)

it('should find vertical words written top-to-bottom', function()
local first, last = WordSearch(puzzle).find('ecmascript')
assert.same({ 10, 1 }, first)
assert.same({ 10, 10 }, last)
end)

it('should find vertical words written bottom-to-top', function()
local first, last = WordSearch(puzzle).find('rust')
assert.same({ 9, 5 }, first)
assert.same({ 9, 2 }, last)
end)

it('should find diagonal words written top-left-to-bottom-right', function()
local first, last = WordSearch(puzzle).find('java')
assert.same({ 1, 1 }, first)
assert.same({ 4, 4 }, last)
end)

it('should find diagonal upper written bottom-right-to-top-left', function()
local first, last = WordSearch(puzzle).find('lua')
assert.same({ 8, 9 }, first)
assert.same({ 6, 7 }, last)
end)

it('should find diagonal upper written bottom-left-to-top-right', function()
local first, last = WordSearch(puzzle).find('lisp')
assert.same({ 3, 6 }, first)
assert.same({ 6, 3 }, last)
end)

it('should find diagonal upper written top-right-to-bottom-left', function()
local first, last = WordSearch(puzzle).find('ruby')
assert.same({ 8, 6 }, first)
assert.same({ 5, 9 }, last)
end)

it('should not find words that are not in the puzzle', function()
assert.same(nil, first)
assert.same(nil, last)
end)

it('should be able to search differently-sized puzzles', function()
local puzzle = {
'qwertyuiopz',
'luamsicrexe',
'abcdefghijk'
}
local first, last = WordSearch(puzzle).find('exercism')
assert.same({ 11, 2 }, first)
assert.same({ 4, 2 }, last)
end)
end)``````
``````return function(puzzle)

local first = {nil, nil}
local last = {nil, nil}

function col_check(word)
local drow = string.reverse(word)
local column = ""
for col = 1, #puzzle[1] do
column = ""
for row = 1, #puzzle do
column = column .. string.sub(puzzle[row], col, col)
end
first[2], last[2] = string.find(column, word)
if(first[2] == nil) then
last[2], first[2] = string.find(column, drow)
end
if(first[2] ~= nil) then
first[1] = col
last[1] = col
return first[1]
end
end
return first[1]
end

function row_check(word)
local drow = string.reverse(word)
for row = 1, #puzzle do
first[1], last[1] = string.find(puzzle[row], word)
if(first[1] == nil) then
last[1], first[1] = string.find(puzzle[row], drow)
end
if(first[1] ~= nil) then
first[2] = row
last[2] = row
break
end
end
return first[1]
end

function diag_forward(word)
local drow = string.reverse(word)
local diag_word = ""
local offset = 0
for col_pos = 1, #puzzle[1] - 1 do
for diag = col_pos, #puzzle[1] do
diag_word = diag_word .. string.sub(puzzle[diag - offset], diag, diag)
end
first[2], last[2] = diag_word:find(word)
if(first[2] == nil) then
last[2], first[2] = diag_word:find(drow)
end
if(first[2] ~= nil) then
first[1] = first[2]
last[1] = last[2]
return first[1]
end
offset = col_pos
diag_word = ""
end
offset = 0
diag_word = ""
for row_pos = 1, #puzzle[1] - 1 do
for diag = row_pos, #puzzle do
diag_word = diag_word .. string.sub(puzzle[diag], diag - offset, diag - offset)
end
first[1], last[1] = diag_word:find(word)
if(first[1] == nil) then
last[1], first[1] = diag_word:find(drow)
end
if(first[1] ~= nil) then
first[2] = first[1] + offset
last[2] = last[1] + offset
return first[1]
end
offset = row_pos
diag_word = ""
end
return first[1]
end

function diag_reverse(word)
local drow = string.reverse(word)
local diag_word = ""
local row = 1
for col_pos = #puzzle[1], 2, -1 do
for diag = col_pos, 1, -1 do
diag_word = diag_word .. string.sub(puzzle[row], diag, diag)
row = row + 1
end
first[2], last[2] = diag_word:find(word)
if(first[2] == nil) then
last[2], first[2] = diag_word:find(drow)
end
if(first[2] ~= nil) then
first[1] = col_pos + 1 - first[2]
last[1] = col_pos + 1 - last[2]
return first[1]
end
row = 1
diag_word = ""
end
column = #puzzle[1]
diag_word = ""
for row_pos = 2, #puzzle - 1 do
for diag = row_pos, #puzzle do
diag_word = diag_word .. string.sub(puzzle[diag], column, column)
column = column - 1
end
first[1], last[1] = diag_word:find(word)
if(first[1] == nil) then
last[1], first[1] = diag_word:find(drow)
end
if(first[1] ~= nil) then
first[1] = #puzzle + 1 - first[1]
first[2] = row_pos + #puzzle[row_pos] - first[1]
last[1] = #puzzle + 1 - last[1]
last[2] = row_pos + #puzzle[row_pos] - last[1]
return first[1]
end
column = #puzzle[1]
diag_word = ""
end
return first[1]
end

return{
find = function(word)
if(nil ~= row_check(word)) then
return first, last
elseif(nil ~= col_check(word)) then
return first, last
elseif(nil ~= diag_forward(word)) then
return first, last
elseif(nil ~= diag_reverse(word)) then
return first, last
end

return nil
end
}
end``````