这是警察岗哨。强盗邮报来了。
您的任务是接受一个整数输入N并输出序列OEIS A002942中的N个数字。
序列由向后写的正方形数字组成:
1, 4, 9, 61, 52, 63, 94, 46, 18, 1, 121, 441, ...注意,前导零被裁剪掉(100变成1,而不是001)。将其转换为字符串(或一个长数字给出):
1496152639446181121441您应该在这个字符串/数字中输出第N位数字。您可以选择以N作为0索引或1索引(请说明您选择哪一个)。
N = 1, ==> 1
N = 5, ==> 1
N = 17, ==> 1 <- Important test case! It's not zero.
N = 20, ==> 4
N = 78, ==> 0
N = 100, ==> 4您的代码应该适用于不超过N= 2^15的数字(除非您的语言默认不能处理32位整数,在这种情况下,N可以更低)。
您必须用同一种语言编写两个功能/程序,以完成相同的任务。您必须发布一个函数/程序,以及您编写的两个函数/程序之间的Levenshtein距离。Levenshtein距离是以字符度量的(因此,添加两个字节字符将给出LD = 1)。
未透露的代码不能比原始解决方案长(但可以是相同的大小)。盗贼将尝试用你给出的Levenshtein距离来编写一个代码(只要它能工作,就可以与您未透露的代码不同)。
胜利者将是未提交的,有最低的Levenshtein距离。
如果你的意见书被打开7天,那么你可以透露你所写的替代代码,并将你的提交标记为安全。
发布于 2017-11-02 15:40:00
((snd.span(<'1').reverse.show.(^2)=<<[1..])!!)我反复检查前导零是否被裁剪;)
[1..] -- for each element in [1,2,3,4,5,...]
=<< -- apply the following functions
(^2) -- square [1,4,9,16,25,...]
show. -- convert to string ["1","4","9","16","25",...]
reverse. -- reverse ["1","4","9","61","52",...,"001",...]
span(<'1'). -- split into leading zeros and remainder [("","1"),("","4"),...,("00","1"),...]
snd. -- only keep remainder ["1","4","9","61","52",...,"1",...]
-- and concatenate the result "1496152..."
(( )!!) -- index into the sequence发布于 2017-11-03 08:20:54
n->{String r="",t=r;for(int i=1,j;r.length()<=n+1;i++)if(Math.sqrt(i)%1==0){for(t="",j=(i+"").length();j>0;t+=(i+"").charAt(--j));r+=t.replaceAll("^0+","");}return r.charAt(n);}如果你只是打高尔夫球的话,这可能不会太难。:)
n->{ // Method with integer parameter and character return-type
String r="", // Result-String, starting empty
t=r; // Temp-String, starting empty
for(int i=1,j; // Index-integers
r.length()<=n+1;i++) // Loop (1) as long as the length is at least n+1
if(Math.sqrt(i)%1==0){ // If the current number `i` is a perfect square:
for(t="", // Reset the temp-String to empty
j=(i+"").length(); // Set `j` to the length of the current number
j>0; // Inner loop (2) as long as `j` is larger than 0
t+= // Append the temp-String with:
(i+"").charAt(--j) // The digit of integer `i` at index `j-1`
// (by first decrease `j` with 1 with `--j`)
); // End of inner loop (2)
r+=t // And then append the temp-String to the result-String
.replaceAll("^0+","");}// after we've removed any leading zeroes
// End of loop (1) (implicit / single-line body)
return r.charAt(n); // Return the `n`'th character of the result-String
} // End of methodhttps://codegolf.stackexchange.com/questions/146926
复制相似问题