Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function scrambleString(str)
- -- Return a scrambled version of str
- -- using Fisher-Yates shuffle
- local result = ""
- while str:len() > 0 do
- local i = math.random(1, str:len())
- result = result .. str:sub(i, i)
- str = str:sub(1, i - 1) .. str:sub(i + 1)
- end
- return result
- end
- local function scrambleWords(str)
- return str:gsub("(%w+)", function (word)
- if word:len() > 3 then
- local middle
- repeat
- -- Scramble the middle until it's not the same as the original middle
- middle = scrambleString(word:sub(2, word:len() - 1))
- until middle ~= word:sub(2, word:len() - 1)
- return
- word:sub(1, 1)
- .. middle
- .. word:sub(word:len())
- else
- -- Words 1, 2 or 3 letters long canot be scrambled
- return word
- end
- end)
- end
- print(scrambleWords([[
- When Derek was three, he received his first
- pair of pyjama bottoms. When he was five,
- he loudly decreed that he would never take
- them off. When he reached eight, his parents
- realized that Derek wasn't lying. At twelve
- he was bullied; at fifteen ignored; at
- seventeen, briefly, Derek became a fashion
- icon. Now Derek works at home, and his pyjama
- bottoms work with him. Theirs is the healthiest
- relationship Derek will ever have.]]))
Advertisement
Add Comment
Please, Sign In to add comment