来回顺序


18

想象一下,由一个路径<,并>在结尾的@,如

><>@

助行器从最左侧的单元格开始。他将按照以下方式遍历该路径:

  • 如果步行者在@牢房中,则他已达到目标并完成。
  • 如果助行器在>牢房中,则整个路径会周期性地向右移动一步,并随身携带助行器
  • 如果助行器在<牢房中,则整个路径会周期性地向左移动一步,并随身携带助行器
  • 然后,步行者迈出一步。如果他在路径的两端,那么他会从一端移开。否则,他会继续朝最后一步移动的方向移动(忽略旋转),最初向右走。

让我们来研究以上示例。步行者的位置标有^

><>@   --rotate-->  @><>
^                    ^
step right (first step):
@><>   --rotate-->  ><>@
  ^                  ^
step right:
><>@   --rotate-->  @><>
  ^                    ^
step left (dead end):
@><>   --rotate-->  ><>@
  ^                  ^
step left:
><>@   --rotate-->  @><>
^                    ^
step left:
@><>   Goal reached!
^

步行者在此过程中访问了6个单元(包括起始单元以及@,并按访问的频率对每个单元进行计数)。

这是一个小示例,其中步行器通过旋转沿边缘运输:

>>@   --rotate-->  @>>
^                   ^
step right (first step):
@>>   --rotate-->  >@>
  ^                ^
step right (dead end):
>@>   Goal reached!
 ^

这次,步行者访问了3个牢房。

我们可以轻松地将其转换为整数序列:

  • 您将得到一个正整数N,例如9
  • 您可以计算该整数的二进制表示形式,例如1001
  • 然后转1>0<,并附加@><<>@
  • 我们将以这种方式构造的数量与N关联的是步行者访问的小区数量。

结果序列的前几个元素是:

2, 3, 3, 4, 6, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6,
6, 10, 6, 10, 8, 8, 6, 10, 8, 8, 6, 6, 6, 6, 7, 7

这看似很随意,但是结果序列实际上具有很多结构:

在此处输入图片说明

作为参考,您可以在此pastebin中找到序列的前2048个数字。

挑战

您猜对了:您要计算以上序列。您可以通过以下三种方式之一进行操作:

  • 您可以通过连续输出(用非数字字符分隔)值或使用某种形式的支持它们的无限生成器来生成无限序列(在内存允许的情况下)。如果将无限流打印到STDOUT,则不必一一打印数字,但要确保在有限的时间(按顺序)之后打印每个数字。如果使用此选项,则不应进行任何输入。
  • 您可以将整数N用作输入,并产生序列的第N个项。
  • 可以采取的整数Ñ作为输入,并产生一切所述Ñ序列的第术语,无论是作为一个列表或字符串中使用的明确分离。

由于我不想惩罚不能轻易在基数之间转换的语言,因此可以代替整数N,而可以像往常一样使用s和s(作为列表或字符串)使用N的二进制表示形式,-重要的位在前。01

您可以编写程序或函数,通过STDIN(或最接近的替代方案),命令行参数或函数自变量获取输入,并通过STDOUT(或最接近的替代方案),函数返回值或函数(out)参数输出结果。

适用标准规则。

背景

实际上,这计算出了我的神秘编程语言Labyrinth将“路径”解释为源代码所需的直截了当的解释器的“滴答声”的数量。在这种情况下,“行者”只是指令指针(具有位置和方向),该@命令终止程序,<并且>是源代码修改命令。


需要哪一个?1,2或3以及我们的提交内容如何评分
Abr001am 2016年

@ Agawa001“您可以使用以下三种方法之一:”选择您认为最适合您要使用的方法和语言的任何一种。
Martin Ender

为什么今天所有重复的数字!!codegolf.stackexchange.com/questions/78787/...:d

Answers:


6

果冻,10 字节

ð+\ḤiḤoµL‘

该函数接受二进制数列表形式的单个整数作为输入。

该算法等效于@ Agawa001的答案中的一种

在线尝试!生成前2048个数字

背景

列举从0L的路径下面的位置,总共得到L +1个位置。L与编码路径的数字N的二进制数字的数量重合。使用此符号,步行者从位置0开始,目标从位置L开始

步行者每走一步,他就会离目标更近一步(朝他目前的行走方向)。同样,在每个换档步骤中,取决于他是在换挡方向上还是在换挡方向上行走,他将位置增加或减少2L +1,或者保持在当前位置。

要更改方向,他必须降落在位置L-1(面向L)或位置1(面向0)上,然后沿其方向移动。他的下一步将使他回到相反的方向,回到原来的位置。

  • 如果L为偶数,则L-1为奇数,因此他不能直接从初始位置前进到L-1。到达它的唯一方法是经过L,进到0并进行下一步降落到1,然后向右前进。这需要前进2L位置,这可以不少于L步完成。

    但是,在不改变方向的情况下采取L步之后,他将达到目标。在起始单元格中添加一个,在这种情况下,我们总共获得了L + 1个访问过的单元格。

  • 如果L为奇数,则L-1为偶数,因此他可以通过向右移动(L-1)/ 2次来达到该位置。如果当时位置L-11以下,他将移至位置L,转身并踩在位置L-1(朝左)。

    在他达到自己的目标之前,这可能会或可能不会发生,因此有两种情况需要分析:

    • 如果在N的二元展开式中少于(L +1)/ 2出现1,则执行L步将不足以转向。由于这L个步骤将步行者带到了目标,在起始单元格中增加了一个,因此在这种情况下,我们总共获得了L + 1个访问单元。

    • 如果有至少(L + 1)/ 2次出现的1中的二进制展开Ñ,前进到((L + 1)/ 2)发生将需要步骤,其中是发生的初始位置的1

      因此,在迈出I步之后,助行器处于L-1位置,向左。要再次转向,他必须向左走到位置1。但是,与偶数情况一样,由于(L-1)-1是奇数,因此这将需要经过0并采取不小于L的步长。

      由于在左方向上到目标的初始距离是1所以我走了I步后,步行者在改变方向后发现自己与目标的距离为I + 1。由于I <L,我们有I + 1≤L,因此接下来的I + 1步骤将把他带到目标。

      这样总共得到了I + I + 1 = 2I + 1个已采取的步骤。在起始单元格中添加一个,在这种情况下,我们总共获得了2I + 1 + 1 = 2(I + 1)个访问单元。

怎么运行的

ð+\ḤiḤoµL‘  Main link. Argument: x (list of binary digits of N)

       µ    Monadic chain. Argument: x
        L   Compute L, the length of x.
         ‘  Increment to yield L + 1.

ð           Dyadic chain. Left argument: x. Right argument: L + 1
 +\         Compute the cumulative sum of x.
            This replaces the k-th one (and all zeroes to its right) with k.
   Ḥ        Unhalve; multiply all partial sums by 2.
    i       Find the first index of L + 1.
            This either gives I + 1, the 1-based index of the ((L + 1) / 2)-th one
            or 0 if the list doesn't contain L + 1.
            The result will be 0 if x contains less than (L + 1) / 2 ones
            or if L + 1 is an odd integer.
     Ḥ      Unhalve; yield either 2(I + 1) or 0.
      o     Logical OR with L + 1; if the previous operation returned a falsy
            value (i.e., if it yielded 0), replace that value with L + 1.

9

Matlab(分数= 230,n = inf)

function w(s,f),b=[];e=0;for i=s:f,a=dec2bin(i);c=find(a=='1');g=numel(a)+1;if numel(c)>=g/2;if mod(g,2)==1,fprintf('%d ',g);else,d=c(g/2);fprintf('%d ',2*d);end,else,fprintf('%d ',g);end,e=e+1;if(e==100),e=0;fprintf('\n');end;end
  • 该函数将s作为起始索引,将f作为结束索引(inf如果要保持无限,请输入type )。
  • 该功能可以永久运行而不会在任何两个输出类型之间造成任何明显的时间延迟h=1000000000000000000000000000000000000000000000000000;w(h,h+1)
  • 该算法遵循一种数学方法,稍后将对此进行解释,并基于此程序确认了Martin的引用列表:

    stored=[2, 3, 3, 4, 6, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 10, 6, 10, 8, 8, 6, 10, 8, 8, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 14, 8, 8, 8, 14, 8, 14, 12, 12, 8, 8, 8, 14, 8, 14, 12, 12, 8, 14, 12, 12, 10, 10, 10, 10, 8, 8, 8, 14, 8, 14, 12, 12, 8, 14, 12, 12, 10, 10, 10, 10, 8, 14, 12, 12, 10, 10, 10, 10, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 18, 10, 10, 10, 10, 10, 10, 10, 18, 10, 10, 10, 18, 10, 18, 16, 16, 10, 10, 10, 10, 10, 10, 10, 18, 10, 10, 10, 18, 10, 18, 16, 16, 10, 10, 10, 18, 10, 18, 16, 16, 10, 18, 16, 16, 14, 14, 14, 14, 10, 10, 10, 10, 10, 10, 10, 18, 10, 10, 10, 18, 10, 18, 16, 16, 10, 10, 10, 18, 10, 18, 16, 16, 10, 18, 16, 16, 14, 14, 14, 14, 10, 10, 10, 18, 10, 18, 16, 16, 10, 18, 16, 16, 14, 14, 14, 14, 10, 18, 16, 16, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 12, 10, 10, 10, 10, 10, 10, 10, 18, 10, 10, 10, 18, 10, 18, 16, 16, 10, 10, 10, 18, 10, 18, 16, 16, 10, 18, 16, 16, 14, 14, 14, 14, 10, 10, 10, 18, 10, 18, 16, 16, 10, 18, 16, 16, 14, 14, 14, 14, 10, 18, 16, 16, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 12, 10, 10, 10, 18, 10, 18, 16, 16, 10, 18, 16, 16, 14, 14, 14, 14, 10, 18, 16, 16, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 12, 10, 18, 16, 16, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 12, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 22, 12, 12, 12, 22, 12, 22, 20, 20, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 12, 12, 12, 22, 12, 22, 20, 20, 12, 22, 20, 20, 18, 18, 18, 18, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 12, 22, 20, 20, 18, 18, 18, 18, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13];
    b=[];for i=1:numel(stored)
    a=dec2bin(i);
    c=find(a=='1');
    if numel(c)>=(numel(a)+1)/2
    if mod(numel(a)+1,2)==1
    b=[b numel(a)+1];
    else
    d=c((numel(a)+1)/2);
    b=[b 2*d];
    end
    else
    b=[b numel(a)+1];
    end
    end
    for i=1:numel(stored)
    if (b(i))
    if b(i)~=stored(i)
    'error',
    end
    end
    end
    
  • 由于该算法验证了2048个第一个测试用例,因此我会盲目地假设它适用于任何测试用例,因此我的算法可以在此过程中发现的很少的属性上工作,而无需移动和移动指针:

    1-如果二进制转换中1的数目的两倍不超过序列的长度,L则输出为L+1

    2-如果序列长度为偶数且未设置先前条件,则输出相同 L+1

    3-否则,输出是L/2索引1的两倍。


您能否澄清一下“输出是L / 2索引1的两倍”的含义?现在还不清楚。
orlp 2016年

@orlp此序列10010001的1中的第二发生难度4中,由图2是8
Abr001am

1
可以将其压缩到至少89个字节a=dec2bin(input(''));c=find(a=='1');g=nnz(a)+1;if nnz(c)<g/2|mod(g,2);g,else,2*c(g/2),end,这仅给出了序列的单个元素。
大卫

8

Python中,122个 119 113 110 108 107 103字节

def l(b):
 p=e=w=len(b);d=i=1
 while e:p+=1-2*b[w-e];d*=2*(1!=d-p>~w)-1;p-=d;e=(e-d)%-~w;i+=1
 return i

将输入作为二进制数字列表。辅助功能测试:

b = lambda n: [int(d) for d in bin(n)[2:]]

感谢Lynn保存7个字节。


4
皮尤 :D
AdmBorkBork '16

数量不多,但是……我想p-d-1in[-2,w]节省一个字节。
林恩

将语句更改为可d*=2*(1!=d-p>~w)-1节省四个!°v°
Lynn 2016年

@Lynn Nice使用了de Morgan的定律!
orlp 2016年

您能提供与我的产品比较的宽输出范围吗?thanx
Abr001am 2016年

3

Python 2,99个字节

def l(b):l=len(b);return(l>=sum(b)*2or l%2<1)and-~l or[i+1for i,c in enumerate(b)if b[i]][l/2]*2

Python的Agawa001杰出答案。

可读版本:

def l(b):
    if len(b) >= 2*sum(b) or len(b)%2 == 0:
        return len(b) + 1

    return 2*[i+1 for i, c in enumerate(b) if b[i]][len(b)//2]

@ Agawa001我还不了解您的算法,但是我已经通过实验验证了多达1000万个算法。
orlp 2016年

3

MATL,31,25个字节

BXHnQtHsy2/<w2\+~?2/Hfw)E

这只是Agawa001算法的MATL版本,除了它接受整数输入N并返回序列中的第N个项。跟上堆栈中的所有元素非常棘手!我不得不使用剪贴板来避免发疯。您可以在线尝试!

通过:"@在代码之前和]D之后添加,可以使循环打印前N个术语。

感谢Luis Mendo节省了6个字节!


2

朱莉娅0.4,4̷4̷42字节

x->(k=endof(x)+1;try k=2find(x)[k/2]end;k)

该函数接受二进制数列表形式的单个整数作为输入。

该算法等效于@ Agawa001的答案我的Jelly答案中的一种

在线尝试!

怎么运行的

find(x)返回x的所有非零元素的从1开始的索引。我们尝试以索引k / 2访问结果数组,如果成功,则用所选索引的两倍覆盖k

当且仅当以下条件之一为真时,这将失败:

  • k / 2是一个非整数浮点数,因此引发InexactError

  • 索引数组的元素数少于k / 2,因此引发了BoundsError

无论哪种情况,覆盖k都会失败,因此将返回其原始值。


1

JavaScript(ES6),65个字节

s=>(l=s.length+1)%2|!(m=s.match(`(0*1){$l/2}}`))?l:m[0].length*2

接受二进制字符串。使用来自其他各种答案的退回检查。


1

Python 2,74个字节

def f(x):k=len(x)+1;print next((i*2for i in range(k)if k==2*sum(x[:i])),k)

该函数接受二进制数列表形式的单个整数作为输入。

该算法等效于@ Agawa001的答案我的Jelly答案中的一种

在Ideone上进行测试

怎么运行的

next尝试查找第一个整数2i,该整数2ik==2*sum(x[:i])返回true。由于x[:i]包含i个元素,因此得出(k / 2)th 1的从1开始的索引。

next如果k / 2为非整数或x包含小于k / 2的整数,则将失败。在这种情况下,将返回默认值k


0

> <>,63字节

2r11&>}:?v{1->:l2-%?vr{{$>1+$}$:2=$@&101.
 +&?!^&n;>{1+^ .0+bf<

从看到此挑战中的示例模式的那一刻起,我就知道要使用哪种语言:)

使用N获得第N个项。

假定输入在堆栈中为二进制。该解决方案主要依靠将磁带移动到助行器下方,而不是四处移动助听器。

在线尝试!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.