如何使用lua洗牌单词的字母
我在PHP中使用了这个str_shuffle()函数 还有梅卡 我需要做同样的想法,在字母之间用空格洗牌,但要用lua如何使用lua洗牌单词的字母,lua,telegram-bot,Lua,Telegram Bot,我在PHP中使用了这个str_shuffle()函数 还有梅卡 我需要做同样的想法,在字母之间用空格洗牌,但要用lua 为了使用电报机器人,我搜索了很多,但没有发现任何类似的东西。下面是一些代码,可以用注释来解释它的工作原理: function tablelength(table) -- to calculate the length of a table local count = 0 for _ in pairs(table) do count = count + 1 end --
为了使用电报机器人,我搜索了很多,但没有发现任何类似的东西。下面是一些代码,可以用注释来解释它的工作原理:
function tablelength(table) -- to calculate the length of a table
local count = 0
for _ in pairs(table) do count = count + 1 end -- loop through each of its items and add to a counter
return count
end
function stringtoletters(text) -- split a string into its individual characters
local letters = {}
for a = 1, string.len(text) do -- loop through each character in the string and add it to the table
table.insert(letters, string.sub(text, a, a))
end
return letters
end
function randomlyreorganizetext(text) -- the function that actually does the stuff AKA the meat
local letters = stringtoletters(text)
local n = tablelength(letters)
math.randomseed(os.time()) -- initialize the random number generator
math.random(); math.random(); math.random() -- the first few numbers are apparently not very random so get rid of them
for i=n,1,-1 do -- go through each character and switch it out with another one of the of the characters at random
local j = math.floor(math.random() * (i-1)) + 1 -- decide which character to swap with
local tmp = letters[i] -- make a backup of the current value
letters[i] = letters[j] -- change to random characters value
letters[j] = tmp -- put what used to be the current value into the random characters position
end
return table.concat(letters, " ") -- turn the table back into a string and put spaces between each element
end
print(randomlyreorganizetext("hello"))
这是我的照片。第一个
gsub
按照字母在字符串中出现的顺序对其进行索引。然后我们使用索引并使用无序索引将字母与另一个gsub
重新映射
s="How to shuffle the letters of a word using lua"
t={}
n=0
s:gsub("%a",function (c)
if t[c]==nil then
n=n+1
t[n]=c
t[c]=n
end
end)
math.randomseed(os.time())
for i=n,2,-1 do
local j=math.random(i)
t[i],t[j]=t[j],t[i]
end
z=s:gsub("%a",function (c) return t[t[c]] end)
print(s)
print(z)
math.randomseed(os.time())
局部函数洗牌(str)
本地字母={}
对于str:gmatch.[\128-\191]*'中的字母
insert(字母,{letter=letter,rnd=math.random()})
结束
表.sort(字母,函数(a,b)返回a.rnd
您可以在lua中实现。我要尝试一下,但是,我不认识lua,所以可能需要一点时间。谢谢,伙计,等你。我正在做,但很难,我必须先做一些事情调用数学。randomseed
在你的程序中只做一次,不是在图书馆的功能。谢谢,伙计,这很酷,就像魔术一样:)除了阿拉伯字母有问题不起作用,你能帮我吗that@Destroyer您可能应该发布另一个问题,因为我不认识Lua,我认为这是Unicode的问题,我对此也一无所知。@RaphaelFacredyn谢谢男士:是的,因为阿拉伯字母串的长度为2,感谢您所做的一切,并为您的时间非常感谢您♥现在,它可以工作,但不能与阿拉伯语单词,而且之间没有空格letters@Destroyer,要添加空格,请使用z=s:gsub(“%a”,函数(c)返回t[t[c]..”end)
math.randomseed(os.time())
local function shuffle(str)
local letters = {}
for letter in str:gmatch'.[\128-\191]*' do
table.insert(letters, {letter = letter, rnd = math.random()})
end
table.sort(letters, function(a, b) return a.rnd < b.rnd end)
for i, v in ipairs(letters) do letters[i] = v.letter end
return table.concat(letters)
end
print(shuffle("How to shuffle the whole UTF-8 string"))