增量范围!


14

您的任务是,给定两个正整数X和,返回增量范围序列中的前数字。ñX

增量范围序列首先生成一个从1到含)的范围。例如,如果为,它将生成列表。然后,它将重复的以递增的最后值附加到现有列表中,然后继续。ññ3[1个23]ñ1个

输入例如:ñ=3

n=3
1. Get range 1 to n. List: [1,2,3]
2. Get the last n values of the list. List: [1,2,3]. Last n=3 values: [1,2,3].
3. Increment the last n values by 1. List: [1,2,3]. Last n values: [2,3,4].
4. Append the last n values incremented to the list. List: [1,2,3,2,3,4]
5. Repeat steps 2-5. 2nd time repeat shown below.

2nd repeat:
2. Get the last n values of the list. List: [1,2,3,2,3,4]. Last n=3 values: [2,3,4]
3. Increment the last n values by 1. List: [1,2,3,2,3,4]. Last n values: [3,4,5].
4. Append the last n values incremented to the list. List: [1,2,3,2,3,4,3,4,5]

测试用例:

n,   x,   Output
1,  49,   [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]
2, 100,   [1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40,41,41,42,42,43,43,44,44,45,45,46,46,47,47,48,48,49,49,50,50,51]
3,  13,   [1,2,3,2,3,4,3,4,5,4,5,6,5]

Answers:



7

果冻,4 字节

Ḷd§‘

双向链接x在左侧和n右侧接受两个正整数,这将产生一个正整数列表。

在线尝试!

怎么样?

Ḷd§‘ - Link: x, n              e.g   13, 3
Ḷ    - lowered range (x)             [0,1,2,3,4,5,6,7,8,9,10,11,12]
 d   - divmod (n)                    [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2],[3,0],[3,1],[3,2],[4,0]]
  §  - sums                          [0,1,2,1,2,3,2,3,4,3,4,5,4]
   ‘ - increment (vectorises)        [1,2,3,2,3,4,3,4,5,4,5,6,5]

3
等等...那是divmod吗?聪明!而我当时却在努力挣扎p……
乡下人埃里克(Erik)

6

R,33个字节

function(n,x,z=1:x-1)z%%n+z%/%n+1

在线尝试!

移植Jonathan Allan的Python解决方案

R,36个字节

function(n,x)outer(1:n,0:x,"+")[1:x]

在线尝试!

我原来的解决方案;生成一个ñ×X矩阵,每一列为增量,即1个ñ2ñ+1个,然后获取前X个条目(沿着列向下移动)。


6

05AB1E,6 个字节

L<s‰O>

@JonathanAllan的Jelly答案端口,因此请确保对他进行投票

第一个输入是X,第二个输入是ñ

在线尝试验证所有测试用例

说明:

L       # Push a list in the range [1, (implicit) input]
        #  i.e. 13 → [1,2,3,4,5,6,7,8,9,10,11,12,13]
 <      # Decrease each by 1 to the range [0, input)
        #  → [0,1,2,3,4,5,6,7,8,9,10,11,12]
  s    # Divmod each by the second input
        #  i.e. 3 → [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2],[3,0],[3,1],[3,2],[4,0]]
    O   # Sum each pair
        #  → [0,1,2,1,2,3,2,3,4,3,4,5,4]
     >  # And increase each by 1
        #  → [1,2,3,2,3,4,3,4,5,4,5,6,5]
        # (after which the result is output implicitly)

我自己的初始方法是8 个字节

LI∍εN¹÷+

第一个输入是ñ,第二个输入是X

在线尝试验证所有测试用例

说明:

L         # Push a list in the range [1, (implicit) input]
          #  i.e. 3 → [1,2,3]
 I       # Extend it to the size of the second input
          #  i.e. 13 → [1,2,3,1,2,3,1,2,3,1,2,3,1]
   ε      # Map each value to:
    N¹÷   #  The 0-based index integer-divided by the first input
          #   → [0,0,0,1,1,1,2,2,2,3,3,3,4]
       +  #  Add that to the value
          #   → [1,2,3,2,3,4,3,4,5,4,5,6,5]
          # (after which the result is output implicitly)


4

Brain-Flak,100字节

(<>)<>{({}[()]<(({}))((){[()](<{}>)}{}){{}{}<>(({})<>)(<>)(<>)}{}({}[()]<(<>[]({}())[()]<>)>)>)}{}{}

带有注释和格式:

# Push a zero under the other stack
(<>)<>

# x times
{
    # x - 1
    ({}[()]<

        # Let 'a' be a counter that starts at n
        # Duplicate a and NOT
        (({}))((){[()](<{}>)}{})

        # if a == 0
        {
            # Pop truthy
            {}
            <>

            # Reset n to a
            (({})<>)

            # Push 0 to each
            (<>)(<>)
        }

        # Pop falsy
        {}

        # Decrement A, add one to the other stack, and duplicate that number under this stack
        ({}[()]<
            (<>[]({}())<>)
        >)
    >)
}

在线尝试!


4

J13 12字节

[$[:,1++/&i.

在线尝试!

怎么样

我们以x左边的arg为准n。让我们x = 8n = 3这个例子:

  • +/&i.:通过创建整数范围来转换两个arg i.,即,左侧arg变为0 1 2 3 4 5 6 7,右侧arg变为0 1 2。现在,我们+/从这两个表创建一个“加法表” :

     0 1 2
     1 2 3
     2 3 4
     3 4 5
     4 5 6
     5 6 7
     6 7 8
     7 8 9
    
  • 1 +:将此表的每个元素加1:

     1 2  3
     2 3  4
     3 4  5
     4 5  6
     5 6  7
     6 7  8
     7 8  9
     8 9 10
    
  • [: ,:展平它,

     1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 6 7 8 7 8 9 8 9 10
    
  • [ $:调整形状,$使其具有与原始的未变形的左arg相同数量的元素[,即x

     1 2 3 2 3 4 3 4 
    


4

八度,25字节

@(n,x)((1:n)'+(0:x))(1:x)

输入数字n和的匿名函数x,并输出行向量。

在线尝试!

怎么运行的

考虑n=3x=13

该代码(1:n)'给出了列向量

1
2
3

然后(0:x)给出行向量

0  1  2  3  4  5  6  7  8  9 10 11 12 13

加法(1:n)'+(0:x)是逐个元素进行广播的,因此它给出了具有所有和对的矩阵:

1  2  3  4  5  6  7  8  9 10 11 12 13 14
2  3  4  5  6  7  8  9 10 11 12 13 14 15
3  4  5  6  7  8  9 10 11 12 13 14 15 16

索引为,以列主要线性顺序(从下到上,以跨列)(1:x)检索x此矩阵的第一个元素,作为行向量:

1 2 3 2 3 4 3 4 5 4 5 6 5

3

Haskell,31个字节

n#x=take x$[1..n]++map(+1)(n#x)

在线尝试!

这可能是我最喜欢的一种递归。我们从1到n的值开始,然后(通过自引用)将那些相同的值+1连接在一起。然后我们只取第一个x值。


2

第四(gforth),34个字节

: f 0 do i over /mod + 1+ . loop ;

在线尝试!

代码说明

: f            \ start a new word definition
  0 do         \ start a loop from 0 to x-1
    i          \ put the current loop index on the stack
    over       \ copy n to the top of the stack
    /mod       \ get the quotient and remainder of dividing i by n
    + 1+       \ add them together and add 1
    .          \ output result
  loop         \ end the counted loop
;              \ end the word definition

2

MATL16,10个字节

:!i:q+2G:)

在线尝试!

感谢Guiseppe和Luis Mendo,节省了-6个字节!

说明:

:!          % Push the array [1; 2; ... n;]
  i:q       % Push the array [0 1 2 ... x - 1]
     +      % Add these two arrays with broadcasting
      2G    % Push x again
        :)  % Take the first x elements

@LuisMendo谢谢!显然,我对自己的MATL感到非常生疏:)
詹姆斯








1

Stax,6 个字节

⌐çYæ▄9

运行并调试

打开包装并说明:

rmx|%+^ Full program, implicit input (n, x on stack; n in register X)
r       Range [0 .. x)
 m      Map:
  x|%     Divide & modulo x
     +    Add quotient and remainder
      ^   Add 1
          Implicit output


0

木炭,18字节

NθFN⊞υ⊕⎇‹ιθι§υ±θIυ

在线尝试!链接是详细版本的代码。我曾梦想过将索引范围为零的列表作为种子,然后再次将其切片,但实际上要长2个字节。说明:

Nθ                  Input `n` into variable
   N                Input `x`
  F                 Loop over implicit range
         ι          Current index
        ‹           Less than
          θ         Variable `n`
       ⎇   ι        Then current index else
               θ    Variable `n`
              ±     Negated
            §υ      Cyclically indexed into list
      ⊕             Incremented
    ⊞υ              Pushed to list
                Iυ  Cast list to string for implicit output

0

JS,54个字节

f=(n,x)=>Array.from(Array(x),(_,i)=>i+1-(i/n|0)*(n-1))

在线尝试!


欢迎使用PPCG :)由于这不是递归函数,因此您无需计算f=。您可以通过使用参数(n=>x=>)保存一个字节,而通过扩展和映射数组([...Array(x)].map())保存另一个字节。
毛茸茸的





0

C(clang),843字节

#include <stdlib.h>
main(int argc, char* argv[]){
        int x,n;
        if (argc == 3 && (n = atoi(argv[1])) > 0 && (x = atoi(argv[2])) > 0){ 
                int* ranges = calloc(x, sizeof *ranges);
                for (int i = 0; i < x; i++){
                        if (i < n){ 
                                ranges[i] = i+1;
                        }   
                        else {
                                ranges[i] = ranges[i-n] + 1;
                        }   
                }   
        printf("[");
        for (int j = 0; j < x - 1; j++){
                printf("%d",ranges[j]);
                printf(",");
        }   
        printf("%d",ranges[x - 1]);
        printf("]\n");
        free(ranges);
        }   
        else {
                printf("enter a number greater than 0 for n and x\n");
        }   
}

2
嗨,欢迎来到PPCG!这个挑战标记为[code-golf],这意味着您必须以尽可能少的字节/字符完成挑战。您可以删除空白负载,并将变量名称更改为代码中的单个字符(argcargvranges)。同样,无需添加任何警告消息。.您可以假定输入有效,除非挑战另有说明。
凯文·克鲁伊森



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.