JavaScript的(ES6),380 327 311 294字节
(298 282 265字节不包括规则)
感谢@Shaggy提供有用的提示!
((b,d)=>b.replace(/\B[a-z]+\B/gi,f=>(g=>(g.map(j=>(h=d.slice(0,~(rind=d.indexOf(j))?rind:-1),~rind?h.split`,`.length-1:-1)).map((j,k,l,m=[])=>{l.map((n,o)=>n==j?m.push(o):0),sub=m[new Date%(m.length-1)]||k,tmp=g[sub],g[sub]=g[k],g[k]=tmp}),g.join``))([...f])))(s,"aneusvrowxmcz,bhkfdl,gqpy,it");
var f = ((b,d)=>b.replace(/\B[a-z]+\B/gi,f=>(g=>(g.map(j=>(h=d.slice(0,~(rind=d.indexOf(j))?rind:-1),~rind?h.split`,`.length-1:-1)).map((j,k,l,m=[])=>{l.map((n,o)=>n==j?m.push(o):0),sub=m[new Date%(m.length-1)]||k,tmp=g[sub],g[sub]=g[k],g[k]=tmp}),g.join``))([...f])))
var s="Let there be scrambling";
console.log(s);
console.log(f(s,"aneusvrowxmcz,bhkfdl,gqpy,it"))
s="It is well known that a text can still be read while the innards of its words have been scrambled, as long as their first and last letters plus their overall outlines remain constant. Given a printable Ascii+Newline text, scramble each word according to these rules";
console.log(s);
console.log(f(s,"aneusvrowxmcz,bhkfdl,gqpy,it"))
函数f接收任何类型的字符串(单个单词,多个单词,多个带有符号的单词-它将其解释为断字符)和一个数组,该数组包含任意长度的“规则”字符串,这些规则由逗号分隔。
对于您的问题,这一系列规则将是 ["aneusvrowxmcz", "bhkfdl", "gqpy", "it"]
"aneusvrowxmcz,bhkfdl,gqpy,it"
有些字母即使可以混合也不会混在一起,因为您在问题中说字母“可以交换空格”。如果我误解了它,我可以将代码更改为始终对符合规则的字母进行加扰。
我知道这是一个很大的字节,它无法与高尔夫语言竞争,但是我还是想尝试一下,希望您喜欢:)
易读的非丑化代码:
((txt,rules)=>txt.replace(/\B[a-z]+\B/gi,wo=>((w=>(w.map(c=>(h=rules.slice(0, ~(rind=rules.indexOf(c))?rind:-1),~rind?(h.split`,`.length-1):-1)).map((e,i,arr,a=[])=>{
arr.map((x,i)=>(x==e)?a.push(i):0),
sub=a[new Date%(a.length-1)]||i,
tmp=w[sub],
w[sub]=w[i],
w[i]=tmp
}),w.join``))([...wo]))))(str, "aneusvrowxmcz,bhkfdl,gqpy,it")
t
比h
许多人不写的短。