一种非空编码字符串,由可打印的ASCII字符组成(范围为32-126),其中一些丢失的字母已被_替换。
用小写字母解码的长度相同的字符串,包括丢失的字符串。
编辑:正如@Deusovi在注释中提到的,这是培根密码的一个变体。
_替换为字母表的第N个字母(0-索引)。示例:prOGraMMIng PuZZleS & cOde ____
prOGr --> 00110 --> 6 --> 7th letter = 'g'
aMMIn --> 01110 --> 14 --> 15th letter = 'o'
gPuZZ --> 01011 --> 11 --> 12th letter = 'l'
leScO --> 00101 --> 5 --> 6th letter = 'f'通过替换丢失的字母并将所有内容转换回小写,原来的字符串将被揭开:
programming puzzles & code golf这是预期的输出。
_之后永远不会有任何字母。但是,可能还有其他可打印的ASCII字符,如空格和标点符号。_,在这种情况下,您只需返回输入。Input : hello!
Output: hello!
Input : helLO, worl_!
Output: hello, world!
Input : i aM yoUr faTh__.
Output: i am your father.
Input : prOGraMMIng PuZZleS & cOde ____
Output: programming puzzles & code golf
Input : Can YOu gUesS tHE ENd oF This ____?
Output: can you guess the end of this text?
Input : THe qUICk brown FOx JUMps oVEr the la__ ___.
Output: the quick brown fox jumps over the lazy dog.
Input : RoadS? wHERe we're goinG WE doN't need _____.
Output: roads? where we're going we don't need roads.
Input : thE greatESt Trick thE DeVIl EVer PUllEd wAs CONvInciNg tHe WorLD h_ ____'_ _____.
Output: the greatest trick the devil ever pulled was convincing the world he didn't exist.一些额外的测试用例:
Input : BInar_
Output: binary
Input : 12 MonKey_
Output: 12 monkeys
Input : hyPerbolIZ__
Output: hyperbolized
Input : {[One Last Test ca__]}
Output: {[one last test case]}发布于 2018-05-12 18:16:47
áS.u5ôJC>.bv'_y.;l使用05AB1E编码。在网上试试!
á # Remove non-letters from the input string.
S # Split the result into individual characters.
.u # Check if is uppercase for each character.
5ôJ # Split into binary numbers of length 5.
C # Convert from binary to decimal.
> # Add one.
.b # Map 1 → A, 2 → B, 3 → C, ..., 25 → Y, 26 → Z.
v # For each letter:
'_y.; # Replace the first occurrence of '_' with the current letter.
l # Convert the string to lowercase.发布于 2018-05-12 19:01:28
发布于 2018-05-12 18:58:21
def f(s:Array[Char])={var j=0;s.zipWithIndex.collect{case(95,i)=>s(i)=(Integer.parseInt(s.filter(_.isLetter)slice(j,j+5)map(k=>if(k<91)1 else 0)mkString,2)+97)toChar;j+=5};s.map(_.toLower)}解释:
def f(s: Array[Char]) = { // takes a String in input
var j = 0 // j stores at which block of 5 letters we're currently at
s.zipWithIndex.collect { // Array('h', 'e', ...) => Array(('h', 0) ('e', 1), ...) and we apply a collect transformation (filter/map)
case (95, i) => // we only handle cases where the char is '_' (95)
s(i) = ( // we modify the char at index i with the following
Integer.parseInt( // Integer.parseInt("00110", 2) = 6
s //
.filter(_.isLetter) // filter out non letter chars (spaces, punct, figures, ...) from the input string (thanks @Arnauld for the fix)A
.slice(j, j+5) // "substring" the array to the block of 5 letters in question
.map( // map on the current block of 5 letters
k => // the index of the next char in the block f 5 (e.g. 13)
if (k < 91) 1 else 0 // if the current char is upper case (<91) then we replace it by a bit true, otherwise by a bit false
)mkString, // Array(0, 1, 1, ...) => "011..."
2 // cast string to binary
) //
+ 97 // +97 to create a lower case char
)toChar // cast from int to char
j += 5 // update the starting index of the next block of 5 letters
} //
s.map(_.toLower) // return the updated seq of chars all in lower case
} //https://codegolf.stackexchange.com/questions/164647
复制相似问题