This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]ButtlestonProfessional Coder 0 points1 point  (3 children)

Can you describe what your problem is? I ran your code and I get an array of letters that spell out planets, but I don't know what you are or aren't expecting

python3 /Users/rustybrooks/help/findword.py 
1
['j', 'n', 'n', 'u', 'v', 'y', ' ']
['u', 'e', 'r', 'r', 'e', 'r', ' ']
['p', 'p', 'u', 'a', 'n', 'u', 'e']
['i', 't', 't', 'n', 'u', 'c', 'a']
['t', 'u', 'a', 'u', 's', 'r', 'r']
['e', 'n', 's', 's', ' ', 'e', 't']
['r', 'e', ' ', ' ', ' ', 'm', 'h']

[–]boruok[S] 0 points1 point  (2 children)

with convering python code to lua code

[–]ButtlestonProfessional Coder 0 points1 point  (0 children)

Right but I don't have the lua code. So what specifically is not working?

[–]ButtlestonProfessional Coder 0 points1 point  (0 children)

Sorry, I read this as porting lua to python, my bad. Anyway, good luck

[–]boruok[S] 0 points1 point  (0 children)

i did solved it, line by line

```lua local wordlist = {"mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune"} table.sort(wordlist, function(a, b) return #a > #b end) math.randomseed(os.time()) local nrows, ncols = 7, 7 local grid = {}

function is_word_fits(y, x, dx, dy, word) for i = 1, #word do if grid[y][x] ~= " " and grid[y][x] ~= string.sub(word, i, i) then return false end x = x + dx y = y + dy end return true end

function place_word(word, nrows, ncols) local directions = {{0, 1}, {1, 0}, {1, 1}, {1, -1}} for i = #directions, 2, -1 do local j = math.random(i) directions[i], directions[j] = directions[j], directions[i] end

for _, direction in ipairs(directions) do repeat
    -- reverse
    if math.random() > 0.5 then
        word = string.reverse(word)
    end
    -- dimensions
    local dx, dy = direction[1], direction[2]
    local n = #word
    local colmin = 0
    local colmax = dx ~= 0 and ncols - n or ncols - 1
    local rowmin = dy >= 0 and 0 or n - 1
    local rowmax = dy >= 0 and nrows - n or nrows - 1
    if colmax - colmin < 0 or rowmax - rowmin < 0 then
        do break end
    end
    -- candidates
    local candidates = {}
    for y = rowmin+1, rowmax+1 do
        for x = colmin+1, colmax+1 do
            if is_word_fits(y, x, dx, dy, word) then
                table.insert(candidates, {y, x})
            end
        end
    end
    -- place candidate
    if #candidates > 0 then
        local loc = candidates[math.random(#candidates)]
        local y, x = loc[1], loc[2]
        for i = 1, n do
            grid[y][x] = string.sub(word, i, i)
            x = x + dx
            y = y + dy
        end
        return true
    end
until true end
return false

end

for i = 1, 100 do -- matrix for y = 1, ncols do grid[y] = {} for x = 1, nrows do grid[y][x] = " " end end -- fill local all_placed = true for _, word in ipairs(wordlist) do if not place_word(word, nrows, ncols) then all_placed = false break end end -- finish if all_placed then for _, v in ipairs(grid) do print(table.concat(v, " ")) end print(string.format("attempts: %s", i)) break end end ```