Skip to content

Instantly share code, notes, and snippets.

@LastTalon
Created December 4, 2016 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LastTalon/a383fff6bb3d78ad4bc5be56930b322b to your computer and use it in GitHub Desktop.
Save LastTalon/a383fff6bb3d78ad4bc5be56930b322b to your computer and use it in GitHub Desktop.
function shuffle(message)
local shuffle = {}
for i=1, string.len(message) do
shuffle[i] = string.sub(message, i, i)
end
local k = 0
local temp = nil
for n = #shuffle, 1, -1 do
k = math.random(1, n)
temp = shuffle[k]
shuffle[k] = shuffle[n]
shuffle[n] = temp
end
return table.concat(shuffle)
end
function rScramble(message)
local done = false
local scramble = ""
local pLoc = 1
local sLoc = 1
local eLoc = 1
local word = nil
while not done do
sLoc, eLoc, word = string.find(message, "(%a+)", pLoc)
if word ~= nil then
if eLoc - sLoc < 1 then
scramble = scramble .. string.sub(message, pLoc, sLoc)
else
scramble = scramble .. string.sub(message, pLoc, sLoc)
.. shuffle(string.sub(message, sLoc + 1, eLoc - 1))
.. string.sub(message, eLoc, eLoc)
end
pLoc = eLoc + 1
else
done = true
end
end
scramble = scramble .. string.sub(message, pLoc)
return scramble
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment