JavaScript(ES6),66 79 68 67字节
f=([c,...s],p)=>c?(p?~parseInt(c+p,36)%37:c<'!')?f(s,p):c+f(s,c):''
怎么样?
测试连续字母
因为在JS中将两个字符转换为它们的ASCII代码将是一个相当长的操作,所以我们改用以下公式:
~parseInt(b + a, 36) % 37
假设a和b都在in中[a-zA-Z ]
,则上述表达式等于0
且仅当a和b是连续字母(即以36为底的连续数字)时,无论字符如何。
例如:
~parseInt("Y" + "x", 36) = ~(36 * parseInt("Y", 36) + parseInt("x", 36))
= ~(36 * 34 + 33)
= -(36 * 34 + 33 + 1)
= -(37 * 34)
格式化和评论
f = ([c, // c = current character
...s], // s = array of remaining characters
p) => // p = previous matching letter
c ? ( // if there's still at least 1 character to process:
p ? // if p was already defined:
~parseInt(c + p, 36) % 37 // test if p and c are NON-consecutive letters
: // else:
c < '!' // test if c is a space character
) ? // if the above test passes:
f(s, p) // ignore c and keep the current value of p
: // else:
c + f(s, c) // append c to the final result and update p to c
: // else:
'' // stop recursion
测试用例
f=([c,...s],p)=>c?(p?~parseInt(c+p,36)%37:c<'!')?f(s,p):c+f(s,c):''
console.log(f("codegolf")) // -> cdef
console.log(f("CodEgolf")) // -> CdEf
console.log(f(" codeolfg")) // -> cdefg
console.log(f("ProgrammingPuzzles")) // -> P
console.log(f("Stack Exchange")) // -> St
console.log(f("The quick red fox jumped over the lazy brown dog")) // -> Tuvw
console.log(f("Zebra")) // -> Z
console.log(f("Abcdegfhijkl")) // -> Abcdef
z
我们就停下来,对吧?