|
Post by AxonMega on May 11, 2017 19:48:51 GMT
Here's an interesting challenge I though of! For each of the words in the following string:
"When reading text, the letters of each word can be in any order, but the human brain can still identify the word as long as the first and last letters of the word are still in the correct places."
randomize the order of all the characters in the word except for the first and last characters. Print the string with the randomized words.
|
|
|
Post by Josh on May 11, 2017 20:27:44 GMT
Thanks for contributing, Axon! I think I'll try to golf this in Py3!
|
|
Dankvisky
New Member
Veteran
5 bans and still alive. try to beat that with a single account!
Posts: 17
ROBLOX Username: Dankvisky
|
Post by Dankvisky on May 11, 2017 20:54:43 GMT
RbxLua
A bit longer than i expected but 395 chars
s="When reading text, the letters of each word can be in any order, but the human brain can still identify the word as long as the first and last letters of the word are still in the correct places."
t={}l=string.len w=string.sub for i=2,l(s)-1 do table.insert(t,w(s,i,i))end for i=1,#t do c=math.random(#t)t[i], t[c]=t[c],t[i]end d=""for i=1,#t do d=d..t[i]end warn(w(s,1,1)..d..w(s,l(s),l(s)))
EDIT: Misthought that you need to randomize all the characters except first and last. So i guess this doesnt count.
|
|
|
Post by Josh on May 11, 2017 21:07:19 GMT
Python3, 307 characters
Probably can be shortened a little, I'll update it if I find a method to do so
from random import* for i in"When reading text, the letters of each word can be in any order, but the human brain can still identify the word as long as the first and last letters of the word are still in the correct places.".split():b=len(i)-1;a=list(i[1:b]);shuffle(a);print(end=i[0]+"".join(a)+i[b]+" ") Why does this work?
I iterate through each word and for each word, I use a shuffle function in the random library that decides to change the value of the variable instead of returning a shuffled string like I wanted it to, so I had to make it a bit longer. Additionally, it only takes individual characters as a list so I had to take each character and put them into a list before putting them back into a string again.
|
|
yan
New Member
Posts: 3
|
Post by yan on May 11, 2017 22:40:13 GMT
Lua 5.2
local s, newString = "When reading text, the letters of each word can be in any order, but the human brain can still identify the word as long as the first and last letters of the word are still in the correct places.", ""
for word in s:gmatch("%a+") do local randomString, newRandomString, letterTable, chosenLetters = word:sub(2, #word - 1), "", {}, {} for letter in randomString:gmatch("%a") do letterTable[#letterTable + 1] = letter end for i = 1, #letterTable do ::redo:: local randomLetter = math.random(#letterTable) if chosenLetters[randomLetter] then goto redo else newRandomString = newRandomString .. letterTable[randomLetter] chosenLetters[randomLetter] = true end end newString = newString .. word:sub(1, 1) .. newRandomString .. word:sub(#word) end
for i = 1, #s do if s:sub(i, i):find("%A") then newString = newString:sub(1, i - 1) .. s:sub(i, i) .. newString:sub(i) end end
print(newString)
Output: Wehn rineadg text, the ltreets of each word can be in any oedrr, but the hmaun biarn can sltil idetfniy the wrod as long as the fsirt and lsat ltrtees of the wrod are stlil in the corrcet pecals.
|
|
|
Post by Josh on May 11, 2017 22:45:53 GMT
|
|
yan
New Member
Posts: 3
|
Post by yan on May 11, 2017 22:48:41 GMT
|
|