扩展数组


21

给定两个正整数pq,您的任务是返回通过应用以下算法创建的数组A

  1. A = [p,q]d = 2开头
  2. 对于每一对(X,Y)中的连续号码的,其总和为整除d,插入(X + Y)/ d之间Xÿ
  3. 如果找到至少一对匹配的对,则增加d并继续执行步骤#2。否则,停止并返回一个

以下是p = 1q = 21时的详细过程。

  1  21             | Iteration #1: we start with d = 2 and A = [1, 21]
   \/               |               1 + 21  is divisible by 2 -> we insert 11
 22/2=11            |
                    |
  1  11 21          | Iteration #2: d = 3, A = [1, 11, 21]
   \/               |               1 + 11  is divisible by 3 -> we insert 4
 12/3=4             |
                    |
  1 4 11  21        | Iteration #3: d = 4, A = [1, 4, 11, 21]
        \/          |               11 + 21 is divisible by 4 -> we insert 8
      32/4=8        |
                    |
  1    4    11 8 21 | Iteration #4: d = 5, A = [1, 4, 11, 8, 21]
    \/   \/         |               1 + 4   is divisible by 5 -> we insert 1
  5/5=1 15/5=3      |               4 + 11  is divisible by 5 -> we insert 3
                    |
  1 1 4 3 11 8 21   | Iteration #5: d = 6, A = [1, 1, 4, 3, 11, 8, 21]
                    |               no sum of two contiguous numbers is divisible by 6
                    |               -> we stop here

因此,预期输出为:[1、1、4、3、11、8、21]

澄清和规则

  • 输入和输出可以以任何合理的格式处理。保证整数pq大于0。如果有帮助,可以假定q≥p
  • 该算法的第二步骤应被递归地应用到刚刚已经插入在相同的迭代的元件。例如,A = [1,1]d = 2应该导致[1,1,1](不是1的无限列表)。
  • 这是,因此最短答案以字节为单位!

测试用例

  p |   q | Output
----+-----+-------------------------------------------------------------------------------
  1 |   1 | [1,1,1]
  1 |   2 | [1,2]
  1 |   3 | [1,1,2,3]
  2 |   6 | [2,1,2,1,4,1,2,6]
  3 |  13 | [3,1,8,1,3,1,7,1,2,1,5,1,3,2,13]
  9 |   9 | [9,6,9,6,9]
 60 |  68 | [60,13,1,4,31,2,3,5,2,19,64,7,13,1,2,5,2,27,44,3,4,8,2,1,12,1,5,3,28,2,4,16,1,
    |     |  2,12,1,2,1,10,1,6,68]
144 | 336 | [144,68,3,4,8,1,12,1,4,2,28,13,128,44,17,92,240,58,108,5,17,1,2,5,3,28,3,1,11,
    |     |  60,3,6,2,42,2,4,26,192,54,132,7,1,15,1,3,1,18,1,4,2,30,3,1,12,1,9,78,46,336]

如果你想在一个稍微大一点的测试用例测试你的代码,这里是预期的输出:

  • p = 12096(2 6 * 3 3 * 7)
  • q = 24192(2 7 * 3 3 * 7)

Answers:


6

05AB1E28 19 18字节

[Ðü+NÌ/‚ζ˜ʒ.ï}DŠQ#

在线尝试!


嗯,绝对可以提高铁杆。仍在努力重构。

可能和我得到的一样好。

-1感谢,还有其他人,艾米娜!指出交换比寄存器更有效。


[                   // Infinite loop.
 Ð                  // Triplicate [p, ..., q]
  U                 // Pop 1 of 3 copies into register X.
   ü+               // Pairwise addition.
     NÌ/            // Divide by current iteration + 2 (which is d).
        ‚           // Group original [p, ..., q] with pairwise additives.
         ζ˜         // Transpose together and flatten.
           ʒ.ï}     // Filter out non-integer entities (includes the space added by zip).
               DXQ  // Dupe result, see if equal to original.
                  # // If new array is original array, nothing happened, quit & return.

调试转储[p,q] = [1,3]

Full program: [ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> [  ||  stack: []
ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
Full program: ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> Ð  ||  stack: []
current >> U  ||  stack: [[1, 3], [1, 3], [1, 3]]
current >> ü  ||  stack: [[1, 3], [1, 3]]
Full program: +
current >> +  ||  stack: [1, 3]
stack > [4]
current >> N  ||  stack: [[1, 3], [4]]
current >> Ì  ||  stack: [[1, 3], [4], 0]
current >> /  ||  stack: [[1, 3], [4], 2]
current >> ‚  ||  stack: [[1, 3], [2.0]]
current >> ζ  ||  stack: [[[1, 3], [2.0]]]
current >> ˜  ||  stack: [[[1, 2.0], [3, ' ']]]
current >> ʒ  ||  stack: [[1, 2.0, 3, ' ']]
Filter: .ï
Full program: .ï
current >> .  ||  stack: [1]
stack > [1]
Full program: .ï
current >> .  ||  stack: [2.0]
stack > [1]
Full program: .ï
current >> .  ||  stack: [3]
stack > [1]
Full program: .ï
current >> .  ||  stack: [' ']
invalid literal for int() with base 10: ' '
stack > []
current >> D  ||  stack: [[1, 2.0, 3]]
current >> X  ||  stack: [[1, 2.0, 3], [1, 2.0, 3]]
current >> Q  ||  stack: [[1, 2.0, 3], [1, 2.0, 3], [1, 3]]
current >> #  ||  stack: [[1, 2.0, 3], 0]
stack > [[1, 2.0, 3]]
Full program: ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> Ð  ||  stack: [[1, 2.0, 3]]
current >> U  ||  stack: [[1, 2.0, 3], [1, 2.0, 3], [1, 2.0, 3]]
current >> ü  ||  stack: [[1, 2.0, 3], [1, 2.0, 3]]
Full program: +
current >> +  ||  stack: [1, 2.0]
stack > [3.0]
Full program: +
current >> +  ||  stack: [3.0, 2.0, 3]
stack > [3.0, 5.0]
current >> N  ||  stack: [[1, 2.0, 3], [3.0, 5.0]]
current >> Ì  ||  stack: [[1, 2.0, 3], [3.0, 5.0], 1]
current >> /  ||  stack: [[1, 2.0, 3], [3.0, 5.0], 3]
current >> ‚  ||  stack: [[1, 2.0, 3], [1.0, 1.6666666666666667]]
current >> ζ  ||  stack: [[[1, 2.0, 3], [1.0, 1.6666666666666667]]]
current >> ˜  ||  stack: [[[1, 1.0], [2.0, 1.6666666666666667], [3, ' ']]]
current >> ʒ  ||  stack: [[1, 1.0, 2.0, 1.6666666666666667, 3, ' ']]
Filter: .ï
Full program: .ï
current >> .  ||  stack: [1]
stack > [1]
Full program: .ï
current >> .  ||  stack: [1.0]
stack > [1]
Full program: .ï
current >> .  ||  stack: [2.0]
stack > [1]
Full program: .ï
current >> .  ||  stack: [1.6666666666666667]
stack > [0]
Full program: .ï
current >> .  ||  stack: [3]
stack > [1]
Full program: .ï
current >> .  ||  stack: [' ']
invalid literal for int() with base 10: ' '
stack > []
current >> D  ||  stack: [[1, 1.0, 2.0, 3]]
current >> X  ||  stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> Q  ||  stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3], [1, 2.0, 3]]
current >> #  ||  stack: [[1, 1.0, 2.0, 3], 0]
stack > [[1, 1.0, 2.0, 3]]
Full program: ÐUü+NÌ/‚ζ˜ʒ.ï}DXQ#
current >> Ð  ||  stack: [[1, 1.0, 2.0, 3]]
current >> U  ||  stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> ü  ||  stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
Full program: +
current >> +  ||  stack: [1, 1.0]
stack > [2.0]
Full program: +
current >> +  ||  stack: [2.0, 1.0, 2.0]
stack > [2.0, 3.0]
Full program: +
current >> +  ||  stack: [2.0, 3.0, 2.0, 3]
stack > [2.0, 3.0, 5.0]
current >> N  ||  stack: [[1, 1.0, 2.0, 3], [2.0, 3.0, 5.0]]
current >> Ì  ||  stack: [[1, 1.0, 2.0, 3], [2.0, 3.0, 5.0], 2]
current >> /  ||  stack: [[1, 1.0, 2.0, 3], [2.0, 3.0, 5.0], 4]
current >> ‚  ||  stack: [[1, 1.0, 2.0, 3], [0.5, 0.75, 1.25]]
current >> ζ  ||  stack: [[[1, 1.0, 2.0, 3], [0.5, 0.75, 1.25]]]
current >> ˜  ||  stack: [[[1, 0.5], [1.0, 0.75], [2.0, 1.25], [3, ' ']]]
current >> ʒ  ||  stack: [[1, 0.5, 1.0, 0.75, 2.0, 1.25, 3, ' ']]
Filter: .ï
Full program: .ï
current >> .  ||  stack: [1]
stack > [1]
Full program: .ï
current >> .  ||  stack: [0.5]
stack > [0]
Full program: .ï
current >> .  ||  stack: [1.0]
stack > [1]
Full program: .ï
current >> .  ||  stack: [0.75]
stack > [0]
Full program: .ï
current >> .  ||  stack: [2.0]
stack > [1]
Full program: .ï
current >> .  ||  stack: [1.25]
stack > [0]
Full program: .ï
current >> .  ||  stack: [3]
stack > [1]
Full program: .ï
current >> .  ||  stack: [' ']
invalid literal for int() with base 10: ' '
stack > []
current >> D  ||  stack: [[1, 1.0, 2.0, 3]]
current >> X  ||  stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> Q  ||  stack: [[1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3], [1, 1.0, 2.0, 3]]
current >> #  ||  stack: [[1, 1.0, 2.0, 3], 1]
[1, 1.0, 2.0, 3]
stack > [[1, 1.0, 2.0, 3]]

在线尝试调试!


喔,这样的如何ü作品......这让我提高了我以前的答案:-)之一
scottinet

@scottinet [1,2,3,4] ü = [[1,2],[2,3],[3,4]],如果在运行05AB1E时在参数中添加“ -d”,也会产生我在上面附加的“ debug”输出。(也添加了上面的调试链接)。pairwise整洁的原因是因为对于自动矢量化的命令,它仅按对应用命令(ü)在列表上运行显示得很好)。
Magic Octopus Urn'Oct

我知道了,它使我在该答案上节省了1个字节。至于-d...我发现它“调试” ,q到“打印并停止” 之后就太迟了。真痛苦
scottinet

@scottinet我一直使用05AB1E满一年的学习一下吧:(以前我用过的,=因为它没有弹出,只是打印推到堆栈中的最后一个项目。
魔术章鱼金塔

如果删除U,则可以替换XŠ
Emigna '17

8

Mathematica,72 64 59 58字节

(d=2;#//.x_:>Riffle[x,(x+{##2,}&@@x)/d++]~Cases~_Integer)&

在线尝试!

怎么运行的

我们将输入作为列表{p,q}。迭代步骤重新表示为:

  1. (a+b)/d两个元素之间插入ab(x+{##2,}&@@x)计算的序列a+b,并a+Null在末尾添加。我们除以d,然后在和之间Riffle插入每个。增量。(a+b)/dabd
  2. 选择Integer结果列表中的元素。(这也摆脱了Null引入的内容{##2,}。)

重复执行此操作,直到结果不变(这仅是因为我们删除了所有新元素,因为它们都不是整数)才可能发生。

@MartinEnder使用//.而不是-8个字节FixedPoint(并将输入作为列表)。

还有-6个,因为ListConvolve实际上不是那么好


1
//.王牌FixedPoint,而我只是将输入作为一对整数而不是两个单独的整数:(d=2;#//.x_:>x~Riffle~ListConvolve[{1,1}/d++,x]~Cases~_Integer)&
Martin Ender

谢谢!我总是忘记更换//.FixedPoint,因为我真的很喜欢FixedPoint
Misha Lavrov

1
只有64个字节。好像您在其中插入了两个不可打印的内容Integer
马丁·恩德

再次感谢!我不知道为什么我的代码在更改后仍然会失败,而当我做了确实没有什么影响的事情时又回到工作状态。
米莎·拉夫罗夫

1
注释中的代码片段有时具有这些不可打印的字符,尤其是当代码片段带有换行符时。不知道为什么SE插入它们。
马丁·恩德


4

Haskell,85 81字节

(a:b:c)#d=a:[div(a+b)d|mod(a+b)d<1]++(b:c)#d
l#d=l
l%d|l==l#d=l|e<-d+1=l#d%e
(%2)

在线尝试!

输入被视为一个列表,例如[1,2]

编辑:-4字节感谢@Laikoni。


使用保存两个字节l%d|l==l#d=l|e<-d+1=l#d%e
Laikoni '17

@Laikoni:实际上是四个字节。谢谢!
nimi


3

Python 2,98个字节

f=lambda A,B=0,d=2:A*(A==B)or f(sum([[(l+r)/d,r][(l+r)%d>0:]for l,r in zip(A,A[1:])],A[:1]),A,d+1)

调用为f([p,q])在线尝试!

乔纳森·艾伦(Jonathan Allan)保存了12个字节。谢谢〜!

说明

f是一个递归函数:f(A, B, d)评估为f(next_A, A, d+1),除非A == B,在这种情况下它将返回A。(这由A*(A==B)or …以下方法处理:如果A≠B A*(A==B)是空列表,则为false-y,因此将对零件进行求值;如果A = B A*(A==B)则为is A,这是非空的,因此为真,然后将其返回。)

next_A 计算为:

sum([[(l+r)/d,r][(l+r)%d>0:]for l,r in zip(A,A[1:])],A[:1])

最好通过示例来解释。当eg d = 5A = [1, 4, 11, 8, 21]

  sum([[(l+r)/d,r][(l+r)%d>0:]for l,r in zip(A,A[1:])],A[:1])
= sum([[(1+4)/d, 4], [(4+11)/d, 11], [8], [21]], [1])
= [1] + [1, 4] + [3, 11] + [8] + [21]
= [1, 1, 4, 3, 11, 8, 21]

使用zip代替枚举并[A[0]]用作sum的初始值来节省8个字节
乔纳森·艾伦

再节省4使用递归函数
乔纳森·艾伦

@乔纳森·艾伦尼斯!我保存了另一个字节,替换[A[0]]A[:1]:)
Lynn

1
现在由于,我领先了3个字节A*(A==B)
林恩


2

外壳,22个字节

→UG`λf£NΣẊṠeo/⁰+:.)⁰tN

接受2元素的列表,返回整数和浮点数的列表。 在线尝试!

说明

→UG`λf£NΣẊṠeo/⁰+:.)⁰tN  Input is a list L.
  G                 tN  Cumulative reduce over the list [2,3,4..
                   ⁰    with initial value L
   `λ             )     using flipped version of this function:
     f£NΣẊṠeo/⁰+:.       Arguments are a list, say K=[1,3,3], and a number, say d=4.
                :.       Prepend 0.5: [0.5,1,2,3]
         Ẋ               For each adjacent pair,
               +         take their sum,
            o/⁰          divide by d,
          Ṡe             and pair it with the right number in the pair: [[0.375,1],[1.0,3],[1.5,3]]
        Σ                Concatenate: [0.375,1,1.0,3,1.5,3]
     f£N                 Remove non-integers: [1,1.0,3,3]
                        Now we have an infinite list of L threaded through 2,3,4.. using the expansion operation.
 U                      Take longest prefix of unique elements,
→                       then last element of that.

1

Perl 5,92 +1(-a)= 93字节

do{$c=0;@n=shift@F;$.++;push@n,($t=$_+$n[-1])%$.?$_:($c=$t/$.,$_)for@F;@F=@n}while$c;say"@F"

在线尝试!


1

视网膜,111字节

\d+
$*1;
^
11@
{+`(1+); (1+);
$1; $1$2 $2;
(?<=(?=(1+)).*) (\1)+ 
 a$#2$*1; 
 1+ 
 
.*a
1$&
)`a

1+@

1+
$.&
;

在线尝试!

将输入作为空格分隔的数字。天真的遵循给定的算法,唯一值得注意的技术是使用标记符号a,以指出何时保留了任何数字。这用于与Retina有限的循环功能一起使用,该功能仅允许您循环,直到一组阶段对这些阶段的输入没有整体更改为止。

说明:

这将使用与问题相同的示例。

\d+
$*1;

我们将输入的数字数组更改为以分号分隔的一元数组,因此我们需要:

1; 111111111111111111111;

^
11@

d在开始时将代码放入代码中,即可为我们提供:

11@1; 111111111111111111111;

{+`(1+); (1+);
$1; $1$2 $2;

这稍微复杂一些。{启动一组阶段,直到达到固定点为止。然后,+指示该阶段本身应执行到固定点。此阶段将每对相邻的数字相加,但插入时不带附加的分号。现在我们有:

11@1; 1111111111111111111111 111111111111111111111;

(?<=(?=(1+)).*) (\1)+ 
 a$#2$*1;

另一个棘手的阶段,这是将除数累加到第一个捕获组中,并替换列表中没有尾随分号的任何数字,该数字除以d。我们还a为这些数字添加了一个前导,以表示已保留某些内容,并;以指示该内容应永久存在于数组中。现在我们有:

11@1; a11111111111; 111111111111111111111;
 1+ 

这将删除d在这一轮之前不能被数组整除或数组中不可整除的数字。这不会改变我们的示例。

.*a
1&$

这从字符串的开头到a输入的最后一个字母贪婪地匹配。这意味着最多只能有一场比赛。如果进行了任何更改,请向中添加一个d,否则将其保留不变,以便退出循环。

111@1; a11111111111; 111111111111111111111;

)`a

)关通过启动循环{(不要质疑它!)否则这个阶段,只是删除我们先前放下的标记。由于这是循环的结束,因此我们将重复上述步骤很多次,但是我将继续进行,就像我忘记了循环一样,因为这会使示例更加连续。

111@1; 11111111111; 111111111111111111111;

1+@

此阶段从我们的输出中删除:

1; 11111111111; 111111111111111111111;

1+
$.&

此阶段将一进制数替换为十进制数:

1; 11; 21;

;

最后阶段摆脱了分号:

1 11 21

显然,跳过循环会使我们在这里得到不正确的结果,但是希望不会太混乱。


我的标记预览看起来与我看到的输出完全不同-有人有什么想法吗?特别是当我认为不应将一堆代码块连接在一起时。
FryAmTheEggman

1

JavaScript(ES6),89 87 82字节

感谢@Arnauld提供了-2个字节,并帮助节省了5个字节。

f=(a,d=2,r)=>a.map(v=>b.push(v,...(v+=a[++i])%d<1?[r=v/d]:[]),b=i=[])|r?f(b,d+1):a

将输入作为数组:f([p,q])

测试用例

f=(a,d=2,r)=>a.map(v=>b.push(v,...(v+=a[++i])%d<1?[r=v/d]:[]),b=i=[])|r?f(b,d+1):a

;[[1,1],[1,2],[1,3],[2,6],[3,13],[9,9],[60,68],[144,336],[12096,24192]]
.forEach(test=>O.innerText+=JSON.stringify(test)+" -> "+JSON.stringify(f(test))+"\n")
<pre id=O></pre>


我认为您可以更新vv+=b[++i])而不是s保存1个字节。您可以使用|r而不是保存另一个字节&&r(我认为这是安全的,但我没有仔细检查)。
Arnauld

@Arnauld谢谢!使用|r确实通过了所有测试用例。
贾斯汀·马里纳

是使用的85字节变体push()
Arnauld

@Arnauld Nice,我最初想到的是push只使用一次,而不是两次;重新审视这个想法后,我来到了这个为86个字节。也许可以改善?
贾斯汀·马里纳

您可以这样做push(v,...),然后v+=再次使用84个字节
Arnauld


1

Java 8,180字节

import java.util.*;p->q->{List<Integer>r=new Stack();r.add(p);r.add(q);for(int d=1,f=d,i;f==d++;)for(i=1;i<r.size();i++)if((q=r.get(i)+r.get(i-1))%d<1)r.add(i++,q/(f=d));return r;}

说明:

在这里尝试。

import java.util.*;           // Required import for List and Stack

p->q->{                       // Method with two integer parameters and List return-type
  List<Integer>r=new Stack(); //  Result-list
  r.add(p);r.add(q);          //  Add the two input-integers to the List
  for(int d=1,                //  Divisible integer (starting at 1)
          f=d,                //  Temp integer (starting at `d` / also 1)
          i;                  //  Index-integer
      f==d++;)                //  Loop (1) as long as `f` and `d` are equal
                              //  (and raise `d` by 1 so it starts at 2 inside the loop)
    for(i=1;                  //   Reset index-integer to 1
        i<r.size();i++)       //   Inner loop (2) over the List
      if((q=r.get(i)+r.get(i-1)) 
                              //    If the current + previous items (stored in `q`)
         %d<1)                //    are divisible by `d`:
        r.add(i++,q/(f=d));   //     Insert `q` divided by `d` to the List at index `i`
                              //     (and raise `i` by 1 and set `f` to `d` in the process)
                              //   End of inner loop (2) (implicit / single-line body)
                              //  End of loop (1) (implicit / single-line body)
  return r;                   //  Return the result-List
}                             // End of method

1

C#,280个字节

using System.Linq;class A{static void Main(string[] p){var l=new System.Collections.Generic.List<int>(p.Select(int.Parse));int r,d=2,c;do{c=0;for(int i=1;i<l.Count;++i){if((r=l[i-1]+l[i])%d==0){l.Insert(i++,r/d);++c;}}++d;}while(c>0);l.ForEach(v=>System.Console.Write((v+" ")));}}

第一次尝试打高尔夫球,这就是整个程序。 测试一下

尝试2,159个字节

删除脚手架,因为任务是提供一个可以接受一对数字(一个数组起作用)并返回一个数组的函数。假定可以使用Func <int [],int []> F满足要求,只需定义F

F=v=>{var l=new List<int>(v);int i,r,d=2,c;do{c=0;for(i=1;i<l.Count;++i){if((r=l[i-1]+l[i])%d==0){l.Insert(i++,r/d);++c;}}++d;}while(c>0);return l.ToArray();};

在此处测试完整程序

如果将通用列表视为有效输出,则该值可能会较小(删除.ToArray()以节省10个字节)。

如果输入也可以修改,则传入List <int>而不是数组,从而无需初始化输出(输出为126个字节)。

更进一步,在这种情况下,实际上并不需要返回值。相反,使用操作会删除return语句使用的9个字节。


欢迎来到PPCG!不错的第一答案。
Arnauld

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.