首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Levenshtein距离& OEIS (Cops)

Levenshtein距离& OEIS (Cops)
EN

Code Golf用户
提问于 2017-11-02 14:03:23
回答 8查看 1.9K关注 0票数 15

这是警察岗哨。强盗邮报来了

您的任务是接受一个整数输入N并输出序列OEIS A002942中的N个数字。

序列由向后写的正方形数字组成:

代码语言:javascript
复制
1, 4, 9, 61, 52, 63, 94, 46, 18, 1, 121, 441, ...

注意,前导零被裁剪掉(100变成1,而不是001)。将其转换为字符串(或一个长数字给出):

代码语言:javascript
复制
1496152639446181121441

您应该在这个字符串/数字中输出第N位数字。您可以选择以N作为0索引或1索引(请说明您选择哪一个)。

测试用例(1-索引):

代码语言:javascript
复制
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可以更低)。

Cops:

您必须用同一种语言编写两个功能/程序,以完成相同的任务。您必须发布一个函数/程序,以及您编写的两个函数/程序之间的Levenshtein距离。Levenshtein距离是以字符度量的(因此,添加两个字节字符将给出LD = 1)。

未透露的代码不能比原始解决方案长(但可以是相同的大小)。盗贼将尝试用你给出的Levenshtein距离来编写一个代码(只要它能工作,就可以与您未透露的代码不同)。

胜利者将是未提交的,有最低的Levenshtein距离。

你可以检查一下这里的莱文希廷距离!

如果你的意见书被打开7天,那么你可以透露你所写的替代代码,并将你的提交标记为安全。

EN

回答 8

Code Golf用户

发布于 2017-11-02 15:40:00

哈斯克尔,LD = 13,裂开

代码语言:javascript
复制
((snd.span(<'1').reverse.show.(^2)=<<[1..])!!)

在网上试试!

我反复检查前导零是否被裁剪;)

解释:

代码语言:javascript
复制
                    [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
票数 6
EN

Code Golf用户

发布于 2017-11-03 08:20:54

Java 8,(177个字节) LD = 92 (裂成@Arnauld)

(我用过这个在线的LD计算器.)

代码语言:javascript
复制
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);}

如果你只是打高尔夫球的话,这可能不会太难。:)

解释:

在这里试试。

代码语言:javascript
复制
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 method
票数 2
EN

Code Golf用户

发布于 2017-11-03 09:30:29

八度,LD = 63,裂开

代码语言:javascript
复制
@(t)[arrayfun(@(t)num2str(str2num(flip(num2str(t)))),(1:t).^2,'uni',0){:}](t)

在网上试试!

提交是77个字节,所以您需要替换相当多的=)

票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/146926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档