| Bytes | Lang | Time | Link |
|---|---|---|---|
| 056 | Red | 250512T141324Z | Galen Iv |
| 012 | APLNARS | 250512T090229Z | RosLuP |
| 003 | Uiua | 240703T124054Z | noodle p |
| 009 | Pyth | 250218T184125Z | Glory2Uk |
| 004 | Husk | 210812T113852Z | Dominic |
| 003 | Uiua | 231014T055322Z | chunes |
| 004 | Nekomata | 231014T150057Z | alephalp |
| 036 | Arturo | 230117T225002Z | chunes |
| 025 | WolframLanguage Mathematica | 210807T022421Z | theorist |
| 039 | PowerShell Core | 220308T011348Z | Julian |
| 002 | Vyxal | 210807T013400Z | lyxal |
| 008 | K ngn/k | 210810T135410Z | coltim |
| 117 | Desmos | 210809T062939Z | Aiden Ch |
| 005 | MATL | 210807T123120Z | Luis Men |
| 025 | Julia 1.0 | 210807T202134Z | Kirill L |
| 003 | Japt m | 210807T073734Z | Shaggy |
| 064 | Scala | 210807T174154Z | user |
| 029 | R >= 4.1 | 210807T061551Z | Nick Ken |
| 003 | Jelly | 210807T012406Z | hyperneu |
| 003 | APL Dyalog Extended | 210807T013934Z | rak1507 |
| 034 | Haskell | 210807T130318Z | Wheat Wi |
| 058 | C gcc | 210807T130723Z | Noodle9 |
| 034 | Haskell | 210807T130406Z | orthocre |
| 044 | JavaScript ES6 | 210807T063733Z | Arnauld |
| 010 | Charcoal | 210807T103941Z | Neil |
| 039 | Python 2 | 210807T075611Z | xnor |
| 4426 | Ruby | 210807T064051Z | Jonah |
| 003 | Stax | 210807T062729Z | Razetime |
| 005 | 05AB1E | 210807T054136Z | ovs |
| 007 | Brachylog | 210807T044955Z | xash |
| 042 | Python 2 | 210807T043401Z | dingledo |
| 024 | Factor | 210807T014336Z | chunes |
| 006 | J | 210807T014233Z | Jonah |
| 076 | Python 3 | 210807T014214Z | wasif |
APL(NARS), 12 chars
{⍺⊂⍨∊⍵⍴¨⍳≢⍵}
Input array to break in ⍺, the array of lenghts in ⍵, where +/⍵ has to be the same number of ≢⍺.
test:
1 2 3 4 5 6 7 8 9 7 8 {⍺⊂⍨∊⍵⍴¨⍳≢⍵}2 4 5
┌3─────────────────────────────┐
│┌2───┐ ┌4───────┐ ┌5─────────┐│
││ 1 2│ │ 3 4 5 6│ │ 7 8 9 7 8││
│└~───┘ └~───────┘ └~─────────┘2
└∊─────────────────────────────┘
1 2 3 4 5 6 7 8 9 7 8 {⍺⊂⍨∊⍵⍴¨⍳≢⍵}2 5 4
┌3─────────────────────────────┐
│┌2───┐ ┌5─────────┐ ┌4───────┐│
││ 1 2│ │ 3 4 5 6 7│ │ 8 9 7 8││
│└~───┘ └~─────────┘ └~───────┘2
└∊─────────────────────────────┘
it seems
2 3 4⍴¨1 2 3
┌3─────────────────────────┐
│┌2───┐ ┌3─────┐ ┌4───────┐│
││ 1 1│ │ 2 2 2│ │ 3 3 3 3││
│└~───┘ └~─────┘ └~───────┘2
└∊─────────────────────────┘
Uiua, 3 bytes
⊕□⊚
Read "group by boxing where".
⊚ (where) takes a nonnegative integer list and for each gives that many of the corresponding nonnegative integer:
⊚ [3 2 5 1] # -> [0 0 0 1 1 2 2 2 2 2 3]
# 3 zeroes, 2 ones, 5 twos, 1 three
⊕□ (group by boxing) takes a nonnegative integer list B and a list A with the same length, and groups the elements of A into boxes based on those indices in B:
⊕□ [0 0 0 1 1 2 2 2 2 2 3] "abcdefghijk"
# -> {"abc" "de" "fghij" "k"}
# curly braces mean the items are boxed
Pyth, 9 bytes
fqlMTQ./E
Commented:
./E ## create every possible partition
f ## filter the partition,
lMT ## where the lengths of its elements
q ## are equal to
Q ## the first input
Husk, 5 4 bytes
CmR¹
CmR¹ # 5-byte program, but this is equivalent to
CzR⁰⁰² # 6-byte program using the ¹ shortcut to use the 1st arg twice and implicit 2nd arg
m # map over
⁰ # the list of the 1st argument
R ⁰ # by repeating arg1 this number of times;
# now use the lengths of this list of lists to
C ² # cut the list of the 2nd argument
Nekomata, 4 bytes
J$ᶻL
J$ᶻL Input L and R
J Non-deterministically split L into parts
$ Swap to get R
ᶻL Check that the length of each part is equal to the corresponding element of R
Arturo, 36 bytes
$[a,b][map b'i[take a i a:drop a i]]
$[a,b][ ; a function taking two arguments a and b
map b'i[ ; map over b and assign current elt to i
take a i ; the first i elts of a
a:drop a i ; assign a sans the first i elts to a
] ; end map
] ; end function
WolframLanguage (Mathematica), 28 25 bytes
TakeDrop~FoldPairList~##&
–3 bytes from att.
With the prohibitied built-in (introduced in 2017 with v. 11.2), it's 8 bytes:
TakeList
PowerShell Core, 39 bytes
param($u,$v)$u|%{,$v[$s..(($s+=$_)-1)]}
Two test cases are not working because PowerShell flattens simple jagged arrays, i.e. [[7]] becomes [7]
Vyxal, 2 bytes
ɾ•
If generators weren't so broken, it'd just be Thank goodness for Vyxal 2.8 lol.vɾ• and flagless.
Explained
ɾ•
ɾ # vectorise range for each item in the shape list
• # and mold that to the shape list
So essentially, a port of hyper's jelly
K (ngn/k), 8 bytes
{.=x!&y}
Takes the list of values as x and the lengths of the chunks as y.
&yconvert length of chunks from e.g.2 4 3to0 0 1 1 1 1 2 2 2x!make a dictionary mapping the original input to these indices="group" that dictionary (i.e., map the distinct values of the dictionary to their corresponding keys).return just the values (i.e., the original values, split into chunks of the correct lengths)
Desmos, 128 117 bytes
a=length(B)
g(l)=\sum_{k=1}^{[1...length(l)]}l[k]
f=\prod_{n=1}^a\{g(B)[n]+n=[1...\length(L)+a]:0,1\}
h(L,B)=L[g(f)]f
Implements a function \$h(L,B)\$. Output is a list of numbers, with each sublist separated by a zero.
Explanation:
a=length(B): A helper value that stores the length of the list B specified in the challenge description.
g(l)=\sum_{k=1}^{[1...length(l)]}l[k]: A helper function that returns the running total(as a list) of the list argument l.
f=\prod_{n=1}^a\{g(B)[n]+n=[1...\length(L)+a]:0,1\}: Creates a list of 0's and 1's, which will be used later to determine the format of the final outputted list(0's mean sublist separator, 1's mean to put an element of L there, where L is specified in the challenge description.).
h(L,B)=L[g(f)]f: The actual function that outputs the answer, with each sublist separated by a zero.
MATL, 5 bytes
"@:&)
Inputs B, then L. Displays each sublist on a separate line.
Explanation
" % Implicit input: numeric vector B. For each k in B
@ % Push k
: % Range [1 2 ... k]
&) % Two-ouput indexing. The first time this takes as implicit input
% the numeric vector L; in subsequent iterations it uses as input
% what remains of L. Pushes a smaller vector according to the
% specified indices, and another vector with the remaining entries
% Implicit end. Implicit display stack
Julia 1.0, 33 30 25 bytes
l/b=b.|>a->splice!(l,1:a)
Thanks to dingledooper for -3 bytes and to MarcMush to further -5.
Japt -m, 3 bytes
Takes B as the first input.
VvU
VvU :Implicit map of each U in first input (B)
V :Second input (L)
vU :Remove and return the first U elements
Scala, 64 bytes
s=>_./:(Seq[Any]()->s){case(a->s,x)=>(a:+s.take(x),s drop x)}._1
Called as f(fullList)(sublistSizes)
s => //The sequence of integers to be split
_ //The sizes for it to be split into
./: //Fold over it,
(Seq[Any]()->s) //Starting with an empty list of sublists, and the original list
{case(a->s,x) => //a is the current list of sublists, s is the current full list, x is the current element
(a:+s.take(x), //Add the first x elements of s as a sublist
s drop x) //Drop x elements from the full list
}._1 //Only keep the list of sublists
R >= 4.1, 30 29 bytes
\(L,B)split(L,rep(seq(!B),B))
An anonymous function taking two vectors and returning a list of vectors.
Thanks to @pajonk for saving a byte!
Jelly, 3 bytes
Rṁ@
Equivalently, ṁR} but with the arguments the other way around.
R Range (vectorizes)
ṁ@ Mold the right argument to the shape of the ranges
ṁ Mold the left argument to the shape of:
R} Range (vectorizes) of the right argument
APL (Dyalog Extended), 3 bytes
-2 thanks to ovs!
⍸⍛⊆
How does it work?
Firstly ⍸2 4 3 is 1 1 2 2 2 2 3 3 3.
We can then use this to partition (⊆) the right argument into chunks.
Haskell, 34 bytes
(zipWith take<*>).scanl(flip drop)
scanl(flip drop) takes the input and sucessively drops off prefixes.
Prelude> scanl(flip drop) "Test string, test string, 123" [6,1,2,3]
[ "Test string, test string, 123"
, "tring, test string, 123"
, "ring, test string, 123"
, "ng, test string, 123"
, " test string, 123"
]
Note that the first result will always be the input string.
Once we have that we apply zipWith take to then take off the prefixes for each size.
C (gcc), 65 58 bytes
f(s,b,l)int*s,*b;{for(;l--;s+=*b++)printf("%.*ls ",*b,s);}
Inputs a pointer to a list of digits as a wide-character string, a pointer to a list of integer lengths and that list's length (since pointers in C carry no length info).
Outputs the sub-lists separated by spaces.
JavaScript (ES6), 44 bytes
Expects (a)(b).
a=>g=([v,...b])=>v?[a.splice(0,v),...g(b)]:b
(or 43 bytes with linefeeds)
Charcoal, 10 bytes
≔⮌θθEη⭆ι⊟θ
Try it online! Link is to verbose version of code. Outputs a newline-separated list of digit strings. Explanation:
≔⮌θθ
Reverse L.
Eη⭆ι⊟θ
For each element of B, pop that many elements from L, and concatenate the digits into a string for output.
Stax, 3 bytes
m%s
Takes a string of digits, and the partitions as an integer array.
outputs each slice with a newline.
05AB1E, 5 bytes
I've tried to implement £ manually way too often because the docs just say £ - Head. Push a[0:b], which is exactly what this builtin does if you just pass a single integer.
L˜Å¡¦
L convert each length n into a range [1 .. n]
˜ flatten into a list of integers
Å¡ split the sequence on 1's (the only truthy value in 05AB1E)
¦ remove the leading empty list that was created by splitting at index 0
Brachylog, 7 bytes
~cʰ\l₎ᵐ
~cʰ split the left argument into groups, \ transpose so we have a list of (group, length), l₎ᵐ map each pair: the right element is the length of the left element (which is also the output of l).
Python 2, 42 bytes
Inputs \$ L \$ and \$ B \$ as two comma separated lists from STDIN, and outputs sublists each on one line.
L,B=input()
for x in B:print L[:x];L=L[x:]
Factor, 24 bytes
[ [ cut swap ] map nip ]
Mapping the lengths to the part of the input that gets 'cut off.' Cutting a sequence into two parts and getting at the results is efficient in bytes because both pieces just end up on the data stack, instead of locked away inside some collection. Process looks something like this:
"hello world" { 1 2 3 4 1 }"ello world" { "h" 2 3 4 1 }"lo world" { "h" "el" 3 4 1 }"world" { "h" "el" "lo " 4 1 }"d" { "h" "el" "lo " "worl" 1 }"" { "h" "el" "lo " "worl" "d" }{ "h" "el" "lo " "worl" "d" }
J, 6 bytes
</.~I.
Called with the list as left arg, grouping mask as right arg.
The whole thing is a dyadic hook, so that...
I.applies only to the grouping mask, and expands it in place using unique elements. Eg,I. 2 4 3becomes:0 0 1 1 1 1 2 2 2</.~That mask is used to group/.the list itself, putting each group into a box<.
Python 3, 76 bytes
def f(x,n):x=iter(x);return[[*islice(x,a)]for a in n]
from itertools import*