DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
curtisfenner profile image
Curtis Fenner • Edited

Lua, just as a series of string operations:

local function solution(text)
    return text:lower()
        :gsub("%A+", "")
        :gsub("%a", function(n) return " " .. 1 + n:byte() - string.byte("a") end)
        :sub(2)
end

Lua, written with a loop and so a bit less wasteful:

local function solution(text)
    local ns = {}
    for a in text:gmatch("%a") do
        table.insert(ns, 1 + a:lower():byte() - string.byte("a"))
    end
    return table.concat(ns, " ")
end