从一个开始


18

给定一个严格的正整数n,请按照下列步骤操作:

  1. n 1 s 创建一个数组A。
  2. 如果A仅具有一个元素,则终止。否则,从第一个元素开始,用A的和替换每对A,如果A的长度为奇数,则保留最后一个元素,然后重复此步骤。

在从第一步到最后一步的每一步之后,输出应包含A的状态。禁止使用标准漏洞。这是一个挑战,因此每种语言中字节数最少的解决方案将获胜。

测试用例

这些示例输出中的每一行都是一个状态。您可以通过任何合理的格式输出。

输入: 1

[1]

输入: 4

[1, 1, 1, 1]
[2, 2]
[4]

输入: 13

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 1]
[4, 4, 4, 1]
[8, 5]
[13]

输入: 15

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 1]
[4, 4, 4, 3]
[8, 7]
[15]

我可以按相反的顺序复制这个问题的想法吗?给定数字n,逐步输出A,依此类推,直到达到n 1s?
pixma140 '19

9
@ pixma140这基本上是相同的挑战,只是之后的输出相反。修改是微不足道的。
暴民埃里克(Erik the Outgolfer)

Answers:



4

MATL,10字节

:g`t2estnq

在线尝试!

怎么运行的

:     % Input n (implicit). Range [1 2 ... n]
g     % Convert to logical. Gives [1 1 ... 1]
`     % Do...while
  t   %   Duplicate
  2   %   Push 2
  e   %   Reshape as 2-column matrix, in column-major order, padding with 0 if needed
  s   %   Sum of each column
  t   %   Duplicate
  n   %   Number of elements
  q   %   Subtract 1. This will be used as loop condition
      % End (implicit). If top of the stack is not zero run new iteration
      % Display stack, bottom to top (implicit)

4

Python 3,57字节

def f(i,j=1):print(i//j*[j]+[i%j][:i%j]);i>j and f(i,j*2)

在线尝试!

Python 2,51个字节

def f(i,j=1):print i/j*[j]+[i%j][:i%j];i>j>f(i,j*2)

在线尝试!

由于tsh,总共-6个字节

递归函数。对于每个步骤,它都会构造的幂的列表2,以使总和小于或等于给定的整数。如果大于,则将其追加0


1
Python 3的61字节:def f(i,j=1):l=i//j*[j]+[i%j][:i%j];print(l);i>j and f(i,j*2); Python 2 55字节:def f(i,j=1):l=i/j*[j]+[i%j][:i%j];print l;i>j>f(i,j*2)
tsh

@tsh当然,谢谢!i>j在我以前的解决方案中不起作用,我忘了以后再尝试。
吉特(Jitse)


3

R,65个字节

-1个字节,感谢Giuseppe。

n=scan();while(T<2*n){cat(rep(+T,n%/%T),if(n%%T)n%%T,"\n");T=2*T}

在线尝试!

%/%%%k=2^in%/%kkn%%k2ñ-1个

在这里,我使用T而不是k,因为它被初始化为将TRUE其转换为1的方式。我仍然需要打印+T而不是T避免TRUE在输出中使用s 的向量。


将我击败大约5分钟,几乎要60个字节...但是Giuseppe是正确的,它没有输出最后一步。
Sumner18

@ Sumner18应该现在修复。
罗宾·赖德

+T短于T+0
朱塞佩

@Giuseppe谢谢,我知道我忘记了一些东西。
罗宾·赖德

3

Pyth,10个字节

.u+McN2m1

在线尝试!

.u          # Apply until a result is repeated, return all intermediate steps: lambda N,Y:
  +M        # map by + (reduce list on +):
    cN2     # chop N (current value) into chunks of 2, last one is shorter if needed
       m1Q  # map(1, range(Q)) (implicit Q = input)

-1个字节,感谢FryAmTheEggman







2

JavaScript,55个字节

f=(n,t=1,r=n)=>r>t?t+[,f(n,t,r-t)]:n>t?r+`
`+f(n,t+t):r

在线尝试!

这基本上是以下代码的简化版本:

function f(n) {
  var output = '';
  t = 1;
  for (t = 1; ; t *= 2) {
    for (r = n; r > t; r -= t) {
      output += t + ',';
    }
    output += r;
    if (n <= t) break;
    output += '\n';
  }
  return output;
}


2

Brachylog,17个字节

;1j₍ẹẉ₂{ġ₂+ᵐ}ⁱ.ẉȮ

在线尝试!

由于可怕的长,因为这是,我还是觉得有点巧妙使用.ẉȮ:显而易见的方法来打印一些东西,然后检查其长度为1人ẉ₂l1ẉ₂~g或者ẉ₂≡Ȯ,其中在最后一节是必要的,因为ẉ₂相结合的输入和输出输出它们之前,并且Ȯ预先约束为长度为1的列表,因此,如果输入的内容不是长度为1的列表,则统一操作将失败。但是,在谓词的末尾,ẉ₂可以通过以下方法来绕过此功能:使用输出变量而不是下标.ẉȮ首先将其输入与输出变量统一,然后打印输出变量,然后再将输出变量与统一Ȯ


2

Stax,10 个字节

Çë⌐ⁿ┤5π»Å╡

运行并调试

程序:

  1. 生成基于0的范围。
  2. 重复将每个元素减半,直到所有项目均为零。
  3. 计算每个唯一数组的游程长度。

带注释的来源:

r       main:[0 .. 5] 
{{hmgu  main:[[0 .. 5], [0, 0, 1, 1, 2, 2], [0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0]] 
m:GJ    main:"1 1 1 1 1 1" 

1

木炭,19字节

NθIE↨⊖⊗θ²E⪪Eθ¹X²κLλ

在线尝试!链接是详细版本的代码。使用Charcoal的默认输出格式(每行一个数字),子数组彼此隔开两倍。说明:

Nθ                  Input `n` into a variable
       θ            `n`
      ⊗             Doubled
     ⊖              Decremented
    ↨   ²           Converted to base 2 (i.e. ceil(log2(input)))
   E                Map
           Eθ¹      List of `1`s of length `n`
          ⪪         Split into sublists of length
               ²    Literal `2`
              X     To power
                κ   Loop index
         E          Map over each sublist
                 Lλ Take the length
  I                 Cast to string for implicit print


1

Perl 6、38个字节

{1 xx$_,*.rotor(2,:partial)>>.sum...1}

在线尝试!

我现在不记得有一些局部旋转的捷径...

说明:

{                                    }  # Anonymous code block
                                 ...    # Return a sequence
 1 xx$_,            # Starting with a list of 1s with input length
        *           # Where each element is
         .rotor(2,:partial)        # The previous list split into chunks of 2 or less
                           >>.sum  # And each chunk summed
                                    1  # Until the list is length 1

1

Haskell,75个字节

g.pure
g x|x!!0<2=[x]|1>0=(g$(\z->filter(0/=)[-div(-z)2,div z 2])=<<x)++[x]

在线尝试!

从列表后退 [n]直到到达一个列表。

往前看,我可以使用chunksoffrom 获得80个字节Data.List.Split

import Data.List.Split
f x=g$1<$[1..x]
g[n]=[[n]]
g x=x:(g$map sum$chunksOf 2 x)

在线尝试!



0

盖亚 12字节

ċ)¦⟨:q2/Σ¦⟩ª

在线尝试!

ċ)¦		| generate array of n 1's (really, generate array of n 0's and increment each)
   ⟨      ⟩ª	| do the following until you get to a fixed point:
    :q		| dup and print with a newline
      2/	| split into groups of 2, with last group possibly being smaller
	Σ¦	| take the sum
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.