从数组创建块


21

您的任务是编写一个给定数组和数字的程序,您需要将数组拆分为大小为number的块。

规则

您的程序将收到一个数组A以及一个正整数n。然后n,如果该字符串的长度不能被n结尾处的剩余数整除,则应将数组拆分为一定长度的块,应将其视为自己的块。

  • 如果n大于array的长度A,则需要返回array A,例如:如果n = 4array A = [1,2,3],则应返回[1,2,3]

  • 该数组可以包含任何类型,而不是数字。

  • 您不应从左到右更改任何项目的顺序(或方向)。例如if n = 2A= [1,2,3]。任何结果而不是[[1,2],[3]]无效的。

测试用例

n   A               Output

2   [1,2,3,4,5,6]   [[1,2],[3,4],[5,6]]
3   [1,2,3,4,5,6]   [[1,2,3],[4,5,6]]
4   [1,2,3,4,5,6]   [[1,2,3,4],[5,6]]

这是,因此每种语言的最短字节将成为赢家。


4
如果n大于A我们需要的长度Ayou您确定不是[A]吗?
亚当

9
@chaugiang我仍然认为n应该返回一个太大的值[A],例如[[1,2,3]]。如果n正好是长度A呢?
1:42

4
@chaugiang亚当是正确的imo。返回值应保持一致。
约拿

1
@chaugiang灿ñ以往等于1
DJMcMayhem

4
在强类型语言中,根本不可能返回A而不是[A] ,这将排除很多语言。
dfeuer

Answers:



9

JavaScript(ES6),36个字节

将输入作为(n)(array)

n=>g=a=>a+a&&[a.splice(0,n),...g(a)]

在线尝试!

已评论

n =>                  // n = chunk size
  g = a =>            // g = recursive function taking the array a[]
    a + a             // if a[] is empty, stop recursion and return an empty string
    &&                // otherwise, return an array made of:
    [ a.splice(0, n), //   the next chunk
      ...g(a)         //   followed by the result of a recursive call
    ]                 //   (the last call leads to ...'', which adds nothing)

现在这是一个干净整洁的解决方案,我也了解了递归匿名函数!

9

APL(Dyalog Unicode),12 字节SBCS

⊢⊂⍨(⍴⊢)⍴1↑⍨⊣

非常感谢Adám从事基本上所有的高尔夫球运动(以及我现在> _>拥有的所有APL知识)。

说明

 ⊂⍨           Partitioned enclose (commuted, i.e. left and right switched) - for each  in left,  in right, if  = 0, create a new sub-array, push  to latest sub-array
             Right argument of entire expression
             Reshape - Change size of right into dimensions specified by left
   (⍴ )       Shape of (here, there is only one dimension - length)
             Right argument of entire expression
         ↑⍨   Take (commuted) - takes  elements from left where  is right. Extra elements (zeroes here) are automatically added
        1     1
             Left argument of entire expression

执行

参数21 2 3 4 5 6 7。请注意,APL数组的形式为a b c,并带有可选的括号。

             2
        1     1
         ↑⍨   12 = 1 0
             1 2 3 4 5 6 7
   (⍴ )       1 2 3 4 5 6 7 = 7
             71 0 = 1 0 1 0 1 0 1
             1 2 3 4 5 6 7
 ⊂⍨           1 0 1 0 1 0 11 2 3 4 5 6 7 = (1 2)(3 4)(5 6)(7)

在线尝试!


7
祝贺您第一个APL答案。并且很好地解释了!在这里,有一个APL饼图:🥧
Adám


7

Prolog(SWI)90 84 61字节

码:

[]*_*[].
L*N*[P|R]:-length(P,N),append(P,T,L),T*N*R;P=L,R=[].

输入格式可能有点奇怪,但它是:

A * n * Result.

例如,对于输入:

n = 2
 A = [1、2、3、4、5、6]

您将需要使用[1, 2, 3, 4, 5, 6] * 2 * Result.

在线尝试!


非高尔夫版本:

divide([], _, []).
divide(List, N, [Prefix | Result]) :-
    length(Prefix, N), append(Prefix, Remaining, List), divide(Remaining, N, Result) 
  ; Prefix = List, Result = [].

在线尝试!


6

PHP,15个字节

$f=array_chunk;

需要PHP7。使用调用$f(ARRAY, N)


6
我不认为您需要给内建函数取另一个名字,所以这个分数只有11分,不是吗?
Neil

@Neil我认为这可能是一个被禁止的漏洞;但是你可能是对的。
泰特斯




5

Brainfuck,71个字节

,[>+>+<<-]>>>,[<[>.,<-]>>>++++[<++++++++>-]<.[-]<<<[<+>>+<-]<[->+<]>>>]

Dunno(如果不重要)...输入格式:

<character whose ascii is n>AAAAAAAAAAAAA
For example, in the input:
 1234567890123492034
n is 32 since the ASCII value of space is 32

接受输入并每次都放在一个空格中 n字符时

说明(不使用逗号,因为这会破坏程序):

, take n
[>+>+<<-] copy into next two cells (destroys original)
>>>, take first of A into next cell
[ while that input exists
<[>.,<-] if n is nonzero output take next of A subtract one from n
>>>++++[<++++++++>-]<.[-]< n is zero so put a space
<<[<+>>+<-] copy the old n into surrounding cells
<[->+<] move from first cell to second
>>>] take input, do again

2
删除71个字符的空格
MilkyWay90

大声笑,我以为我删除了所有这些,但我没有注意到,谢谢!
vityavv

尝试重新组织单元格,以使使用更多的单元格更易于访问(例如,如果输入单元格(使用,更多信息的单元格)被更多地使用,则可以放置一个比放置它更容易访问的单元格在其他单元格中)或使用暴力破解者。我不擅长打高尔夫球,因此这些建议可能无济于事。
MilkyWay90

到目前为止,我已经完成n n n A space了单元设置,如果您能想到更好的方法……
vityavv

可以A space n n n ...工作(或space A n n n...)吗?
MilkyWay90





4

木炭,1字节

在线尝试!木炭的默认I / O使得使用字符串以外的任何东西都很难演示。如果要使用数字列表并输出格式化列表的完整程序,则可以按以下步骤完成:

E⪪AN⪫ι,

在线尝试!链接是详细版本的代码。说明:

  A      Input array
 ⪪       Split into chunks of
   N     Input number
E       Map over chunks
     ι  Current chunk
    ⪫   Joined with
      , Literal `,`
        Implicitly print each chunk on its own line



4

J,4个字节

<\~-

在线尝试!

将数组作为左arg,将块大小作为右arg。

使用二元钩子和带有否定参数的infix副词,这符合我们的定义。

注意:返回类型必须装箱,因为J仅允许使用大小相等的表。




3

Java 10,106 80字节

L->n->{for(int l=L.size(),i=0;i<l;)System.out.print(L.subList(i,(i+=n)<l?i:l));}

打印不带分隔符的块。

在线尝试。

106个字节:

L->n->{var r=new java.util.Stack();for(int l=L.size(),i=0;i<l;)r.add(L.subList(i,(i+=n)<l?i:l));return r;}

实际返回列表列表。

在线尝试。

说明:

L->n->{                       // Method with List and integer parameters and List return-type
  var r=new java.util.Stack();//  Create an empty List
  for(int l=L.size(),         //  Determine the size of the input-List
      i=0;i<l;)               //  Loop `i` in the range [0, size):
    r.add(                    //   Add to the result-List:
      L.subList(i,            //    A sublist of the input-list in the range from `i`
        Math.min(i+=n,l)));   //    to the minimum of: `i` + input-integer or the size
                              //    (and increase `i` by the input-integer at the same)
  return r;}                  //  Return the List of Lists of integers as result





3

V,6个字节

òÀf,r

在线尝试!

十六进制转储:

00000000: f2c0 662c 720a                           ..f,r.

说明:

ò           " Until an error happens:
  f         "   (f)ind the...
 À          "     n'th...
   ,        "     ","
            "   (If there are less than n commas after the cursor, throw an error)
    r       "   Replace the char under the cursor with...
     <cr>   "     A newline

3

Clojure,14个字节

#(partition %)

我猜内置


你好欢迎。该函数应采用两个参数:要分区的数组和块的长度。如果使用分区时最后一个块未“满”,还会发生什么?
NikoNyrh



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.