| Bytes | Lang | Time | Link |
|---|---|---|---|
| 041 | Ruby | 240707T085242Z | int 21h |
| 010 | Uiua | 231105T071814Z | chunes |
| nan | TypeScript 4.9.4 79 Bytes | 230118T044929Z | Asleepac |
| 021 | Raku | 230118T073801Z | Sean |
| 035 | Arturo | 230118T015730Z | chunes |
| 026 | Zsh | 210829T110614Z | roblogic |
| 112 | BrainFlak | 210828T083147Z | Wheat Wi |
| nan | TIBASIC | 210729T182622Z | orangey |
| 008 | Japt | 210728T165835Z | Etheryte |
| 005 | Vyxal ṡ | 210728T161957Z | Aaroneou |
| 007 | 05AB1E | 210728T130142Z | Kevin Cr |
| 151 | Kotlin | 210728T150806Z | David Ba |
| 057 | C gcc | 210605T084454Z | gastropn |
| 036 | R | 210604T115137Z | Dominic |
| 089 | PHP | 210606T152705Z | Hackinet |
| 011 | Pip p | 210606T005541Z | DLosc |
| 093 | Desmos | 210605T180525Z | Aiden Ch |
| 030 | Scala | 210603T212153Z | user |
| 003 | Vim | 210605T000336Z | D. Ben K |
| 169 | Lolwho.Cares | 210605T080450Z | Robot |
| 011 | BQN | 210603T232151Z | frasiyav |
| 006 | Jelly | 210604T224436Z | Unrelate |
| 038 | PowerShell | 210604T203754Z | mazzy |
| 041 | Python 3 | 210604T210226Z | hyper-ne |
| 018 | ><> | 210604T201219Z | kops |
| 044 | Clojure | 210604T192408Z | Kirill L |
| 007 | Jelly | 210603T210947Z | hyper-ne |
| 040 | Python 3.8 | 210604T143335Z | Griffin |
| 046 | Red | 210604T133555Z | 9214 |
| 013 | APL Dyalog Unicode | 210604T113255Z | Adá |
| 035 | Zsh | 210604T084426Z | pxeger |
| 021 | ><> | 210604T102502Z | Jo King |
| 010 | Stax | 210604T085649Z | Razetime |
| 031 | JavaScript ES6 | 210604T015013Z | tsh |
| 007 | Vyxal | 210603T210810Z | emanresu |
| 008 | Vyxal | 210604T091851Z | Wasif |
| 014 | AWK | 210604T003638Z | cnamejj |
| 012 | Husk | 210604T090817Z | Razetime |
| 017 | Retina 0.8.2 | 210603T235716Z | Neil |
| 026 | Wolfram Language Mathematica | 210603T211731Z | ZaMoC |
| 045 | Python 2 | 210604T082009Z | ElPedro |
| 067 | Red | 210604T072736Z | Galen Iv |
| 066 | C gcc | 210604T071617Z | att |
| 026 | Haskell | 210604T050416Z | xnor |
| 057 | PowerShell Core | 210603T232109Z | Julian |
| 063 | MATLAB/Octave | 210603T223911Z | elementi |
| 035 | Factor | 210603T223925Z | chunes |
| 011 | K ngn/k | 210603T223947Z | coltim |
| 1716 | J | 210603T214842Z | Jonah |
| 056 | Haskell | 210603T211743Z | user |
| 034 | JavaScript ES6 | 210603T212235Z | Arnauld |
| 054 | JavaScript V8 | 210603T211044Z | Rydwolf |
| 041 | Python 3.8 | 210603T211734Z | Noodle9 |
Ruby, 41 bytes
->a{[a[..x=a[1..].index(a[0])],a[x+1..]]}
No tricks: find the index of the second occurence -> output an array of two parts of the original array.
Uiua, 10 bytes
⊃↘↙⊡1⊚=⊢..
⊃↘↙⊡1⊚=⊢..
.. # duplicate twice
⊢ # first
= # mask where input is equal to its first element
⊚ # which indices are true?
⊡1 # get the second one
⊃↘↙ # split input at this index
TypeScript (4.9.4) - 161 79 Bytes
Note this is only using the type system, so computation is done at compile time
- 82 Bytes thanks to @ASCII-only!
type S<N,H=[]>=N extends[,...infer R]?N[0]extends H[0]?[H,N]:S<R,[...H,N[0]]>:0
example usage:
type TestOne = Split<[1, 1, 9]>
ungolfed version (original):
type Split<Nums extends any[], Comp extends any = null, Head extends any[] = []> =
// get first element of nums and rest
Nums extends [infer First, ...infer Rest] ?
// check if compare is not set
Comp extends null ?
// set comp to first and nums to rest call recursively
Split<Rest, First> :
// check if first and comp are equal
First extends Comp ?
// return [Head, Nums] if true
[[Comp, ...Head], Nums] :
// move First into Head and call recursively
Split<Rest, Comp, [...Head, First]> :
// return null if nothing
null
Raku, 21 bytes
{S[(.).*?<()>$0]="
"}
A regex-based approach. Input is a string of space-separated digits, output is the same string but with the space between the digits where the list should be broken replaced with a newline.
Arturo, 35 bytes
$[a][split.at:+1index drop a 1a\0a]
$[a][ ; a function taking an arg a
split.at: ... a ; split a at a particular index
+1 ; add one to
index ... a\0 ; the index of the first elt of a in ...
drop a 1 ; a without its first elt
] ; end function
Zsh, 37 26 bytes
Using Zsh array parsing. The separator is an underscore, _
A=$@
<<<${(SI:2:)A/$1/_$1}
Brain-Flak, 112 bytes
((({})<>)<>){{}({}<(({})<>)<>>)({<({}[()]<({}[()])>)>()}{}<>)<>({}<<>({}<>)>)}{}((()()()()()){})<>{}{({}<>)<>}<>
TI-BASIC, 69 bytes (on-calc) / 104 bytes (as text)
Ans→A
2→I
While ⌊A(I)≠⌊A(1
I+1→I
End
Disp seq(⌊A(J),J,1,I-1
seq(⌊A(J),J,I,dim(⌊A
Explanation
Ans→A: The program stores a list in⌊AfromAnsby calling it like this:{list}:prgmL2→I: InitializeIwith 2While ⌊A(I)≠⌊A(1: While the current element is not equal to the firstI+1→I: IncrementI
EndDisp seq(⌊A(J),J,1,I-1: Display the first chunk of the listseq(⌊A(J),J,I,dim(⌊A: Implicitly display the second chunk of the list
Earlier overcomplicated solution, 78 bytes (on-calc) / 130 bytes (as text)
Ans→A
Ans=Ans(1:Ansseq(I,I,1,dim(Ans→B
SortD(⌊B
⌊B(sum(Ans>0)-1
Disp seq(⌊A(I),I,1,Ans-1
seq(⌊A(I),I,Ans,dim(⌊A
Japt, 8 bytes
Feels like there should be a shorter way for this, but can't seem to find it right now.
Selecting the right-hand element ensures we don't select the first letter itself, the check on the incremented variable ensures we only split once.
óϦUΪT°
ó // Split the input string between char pairs where it's not true that
Ï // the right-hand element
¦ // is different than
UÎ // the first char of the input
ª // or
T° // T, initially equals zero, plus plus.
Vyxal ṡ, 5 bytes
hẆḢḣf
Explanation:
h # Get the first element of the list
Ẇ # Split list on head, without removing it from the list
Ḣḣ # Get the first element from the resulting list
f # Flatten the rest of the resulting list
# 'ṡ' flag - print both lists, separated by a space
05AB1E, 8 7 bytes
ćk>Ig‚£
Try it online or verify all test cases.
7 bytes alternative provided by @ovs:
ćk>°RÅ¡
Try it online or verify all test cases.
Explanation:
ć # Extract head of the (implicit) input-list; pop and push remainder-list and
# first item separated to the stack
k # Get the first 0-based index of this item in the remainder-list
> # Increase it by 1 to make it a 1-based index
Ig # Get the length of the input-list
‚ # Pair them together
£ # And split the (implicit) input-list into parts of that size
# (after which the result is output implicitly as result)
ćk> # Same as above
° # Pop and push 10 to the power this 1-based index
R # Reverse it, so we have a 1 with some leading 0s
Å¡ # Split the (implicit) input-list at the truthy indices (or singular index in
# this case: the 1)
# (after which the result is output implicitly as result)
Kotlin, 151 bytes
Takes a List<Int> as input and returns a Pair<List<Int>, List<Int>>
{l:List<Int>->val u=mutableListOf<Int>()
var d=l.size
l.forEachIndexed{i,n->if(!u.contains(n))u.add(n)else d=i}
l.slice(0..d-1)to l.slice(d..l.size-1)}
C (gcc), 58 57 bytes
-1 byte thanks to ceilingcat
Takes input as a string. Outputs to STDOUT.
l;f(char*s){l=index(s+1,*s)-s;printf("%.*s %s",l,s,s+l);}
R, 38 37 36 bytes
Edit: -1 byte thanks to pajonk, as well as outputting the right-way-around now, and then -1 byte thanks to digEmAll
function(l)by(l,cumsum(l==l[1])>1,c)
PHP, 89 bytes
function($x){$y=array_keys($x,$x[0])[1];return[array_slice($x,0,$y),array_slice($x,$y)];}
Explanation:
array_keysreturns all index positions of an element. Accessing$x[1], we get the second occurrence.array_sliceGets you a subpart of the array. Param => (array, start, length[optional])
Pip -p, 11 bytes
a^@(a@*@a1)
Takes a string of digits and outputs a list of two digit-strings. Try it online!
Explanation
a is first command-line arg (implicit)
a@* In a, find all indices of
@a the first character of a
( 1) Get the second index in the list (0-indexed)
a^@ Split a at that index
Desmos, 93 bytes
a=length(l)
b=\min([2...a](9sign(l[2...a]-l[1])^2+1))
f(l)=join(l[1...b-1],join(-1,l[b...a]))
Test on \$f(l)\$, where \$l\$ is the inputted list.
Outputs the two lists combined into one, separated by a \$-1\$ element in between. This is allowed.
For example, [1,1,9] -> [[1], [1,9]] -> [1,-1,1,9]
Scala, 35 30 bytes
Saved 5 bytes thanks to @cubic lettuce!
x=>x splitAt x.indexOf(x(0),1)
Hopefully, I'm not FGITW'ing this.
Vim, 3 bytes/keystrokes
*O<esc>
Jump to next occurrence of word and make a new line.
V (vim), 2 bytes
*O
Since V has implicit escape after O (?), we can use just 2 bytes.
Old (general) 8-byter:
Y/<C-r>"<BS><CR>O<esc>
Input is each list item on a single line. Output is two lists separated by a blank line.
Uses vim notation for the keystrokes (<C-r> is Ctrl-R, etc.; see :help key-notation).
Explanation:
Yyank the first line/<C-r>"<BS><CR>search for the next occurrence by inserting the yanked text into a search/. Yanking into the search register with"/Ydoesn't work (:help quote/). The backspace deletes the line-ending, which is yanked. (Alternately, usey$/<C-r>"<CR>.)O<esc>new blank line above.
This generalizes to any type of list as long as each element is a single line.
I don't think we can use POSIX vi for this, since I don't think it has <C-r> to insert registers. It doesn't have the search register "/, but that doesn't end up mattering.
Lolwho.Cares, 169 bytes
*2*+210021**+102201*1*1>*2+011021`02**+2101,12000002001v v<0210002,<
^120<210<021<<<<<<<<`---020-----,120120*101+201021>*2+00210>^
0210002
Note: The last line is not counted as code, it is the input.
BQN, 12 11 bytesSBCS
⊢⊔˜·∨`·»⊑=«
Explanation:
⊢⊔˜·∨`·»⊑=« # tacit function which can take input as either a list or a string
« # the input list shifted left
= # equality comparison with
⊑ # the first element of the input
·» # shift the result right
·∨` # 'or' scan
⊢⊔˜ # group the input according to those values
Jelly, 6 bytes
CiḢ$Ṭk
Took me embarrassingly long to realize that œṖ splits before, but k splits after...
C Subtract each element from 1, creating a new list object.
Ḣ Remove its first element and then
i $ find its first index in that list.
Ṭ Create an array containing a 1 at that index,
k and split the original array after that 1.
With no mutation at all:
Jelly, 6 bytes
Ḋiḷ/Ṭk
Python 3, 41 bytes
def f(a):a.insert(a[1:].index(a[0])+1,-1)
Another Python approach using the allowed flexible output format. Not any shorter than the existing solution.
><>, 18 bytes
i:vi
=?\o:i:@
oa<^
Saves a copy of the first character x at the bottom of the stack, compares the read character to that, and then prints a line feed and enters an infinite io loop when it finds the second instance of x.
This was a nice instance of being able to use the 2D-ness of ><> by reusing the singular o for both a horizontal and a vertical loop.
Jelly, 7 bytes
œṡḢ©®;Ɱ
œṡḢ©®;Ɱ Main Link
œṡ Split at the first occurence of
Ḣ The first element (pops the element)
© (also copies that element to the register)
Ɱ For each block
®; Prepend the register
As pointed out by Nick Kennedy in the comments (full credit to them), Ḣ;Ɱœṡ@¥ also works and is slightly more functionally pure (though Ḣ still modifies the list itself, so it's not entirely pure):
Ḣ;Ɱœṡ@¥ Main Link
Ḣ Cut off and return the first element
¥ Last two as a dyad (for chaining; this makes the right argument the modified list for both inner dyads rather than applying consecutively)
;Ɱ Prepend the first element to each of
œṡ@ The modified list, split at the first occurrence of the first element
Python 3.8, 40 chars
If outputting a list of two strings is allowed...
lambda s:[s[:(i:=s.find(s[0],1))],s[i:]]
APL (Dyalog Unicode), 13 bytes (SBCS)
Anonymous tacit prefix function
⊢⊂⍨1,⊃<\⍤=1↓⊢
⊢ the argument
1↓ drop first element
⊃… apply the following tacit infix function to that, with the first element of the argument as left argument:
⍤= Boolean mask indicating where they are equal
<\ cumulative right-associative less-than scan (effectively zeroes any one after the first one)
1, prepend a one
⊢⊂⍨ use that to partition the argument, starting a new segment on every 1
Zsh, 35 bytes
shift>$1
ls
for x
(rm $x&&od;<<<$x)
Explanation:
>$1: create a file named the first input itemshift: shift the input array, removing the first itemls: list the directory. Since we created$1, this prints the first itemfor x: for each item$xin the input array:(rm $x: try to remove the item. If the item$xis the same as the first input, and the item has not already been removed, this will succeed&&: if that succeeds:od: print0000000as a separator
<<<$x: print the item
With some loose interpretation of what is allowed as a separator, we could have:
Zsh, 33 bytes
shift>$1
ls
for x
rm -v $x||<<<$x
rm -visverbose; if the removal is successful, a messageremoving 'x'is printed which is arguably a separator, as well as printing the item||: if that fails:<<<$x: print the item. We only need to do this if removal fails, becauserm -vprints the item already
><>, 21 bytes
<0o{oa.0*3+)3l=}:{::i
Takes input as a list of characters (though you are free to put whatever you want in-between digits). Managed to reuse my check that we're not splitting on the first input with the jump quite nicely.
Explanation
< Go left from the start
::i Get the input and duplicate it twice
=}:{ Compare it with the first character of the input
)3l Check if this is not the first char
*3+ Add these checks together and multiply by 3
This will be 3 if the digit is not the same, or if it is the first digit, otherwise 6
.0 Jump to that point on the first line
{oa If it is the split point, print a newline and clear the first digit
o Print the current digit
0 Push a zero to increase the stack height for the first digit check
Stax, 10 bytes
τÄ∩T╕(û▒(Ç
annoyingly long, but i guess it works.
Stax, 12 bytes(regex)
êt┴≈∟·M╤\+6)
Stax, 22 bytes(tsh's regex)
"^(.).*?(?=\1)|.+$"|Fm
AWK, 17 14 bytes
sub(FS$1,RS$1)
Thanks to Pedro Maimere for the hint to lop off 3 bytes
The interactions of the rules in the contest allow for pretty trivial AWK solution... Assuming the input can be a blank delimited string of numbers, this will work. If it has to include the brackets and commas (which I wasn't sure about from the linked article about convenient input), then it would be this instead.
sub(", "(a=substr($1,2)),"]\n["a)
And here's one in ><> which might be shrinkable still... It's the first time I've managed to get something to work in that language, so I wouldn't be surprised to learn there some trick I don't know yet.
><>, 41 bytes
i:o&0v
?(0:i<o$v?<=1:+{=&:&:;
0+1o+19< ^
I can add a more detail description if anyone is interested, but here's an overview of how it works.
The input it expects is a string of digits. Since the challenge specified that the list was made up of single digit numbers, I chose not to include a delimiter. If that's a requirement, some additional stuff would have to be added...
The first line reads in the first number, uses it to set the register (for comparison as the rest of the list is read in), then pushes a counter that tracks the number of times the first number has been seen. Then it passed control down to the next line.
i:o&0v
That line reads one digit at a time, starting with the second digit, and prints it. There's a conditional code to add a \n when the counter hits 1 (meaning it found the second occurrence of the first digit). The code is interpreted right to left to save characters.
?(0:i<o$v?<=1:+{=&:&:;
The last line is effectively like a "function" call to print a linefeed and tweak the counter so that it will never be called again.
0+1o+19< ^
Husk, 12 bytes
Fȯ:;:←¹↕≠←¹t
no split at index builtin, but ↕ (span) helps a bit.
it's similar to xnor(and Zgarb)'s answer, but argument destructuring and functors don't exits, so it just uses a fold.
Retina 0.8.2, 18 17 bytes
^((.).*?)\2
$1¶$2
Try it online! Link is to test suite that double-spaces the output for convenience. Takes input as a string of digits. Explanation: Simply finds the earliest next match of the first character and inserts a newline before it. Edit: Saved 1 byte thanks to @Jakque.
Haskell, 26 bytes
f(x:y)=([x],0)*>span(/=x)y
Shortens Zgarb's OG solution
f(x:y)|(a,b)<-span(/=x)y=(x:a,b)
by prepending x to the first element of (a,b) in a pointfree way, that is without explicitly binding (a,b).
It would be nice it we could do (x:)<$>(a,b), but that gives (a,x:b) -- the Functor instance of tuples lets us act on the second element but not the first.
However, Applicative lets us combine tuples as:
(p, f) <*> (a, b) = (p++a, f b)
([x], id) <*> (a, b) = (x:a, b)
It suffices to use *> which ignores f and leaves b unchanged.
((x:), 0) *> (a, b) = (x:a, b)
The 0 could be anything -- it doesn't matter. It would also work to use >> in place of *>.
26 bytes
f(x:y)=([x],y)>>=span(/=x)
A alternative, this time using the Monad instance and (>>=) :: Monoid a => (a, a0) -> (a0 -> (a, b)) -> (a, b)
27 bytes
f(x:y)=([x],[])<>span(/=x)y
Using <> to do concatenate elementwise (a, b) <> (c, d) = (a++c, b++d). This is available in Prelude without an import starting in version 8.4.1.
PowerShell Core, 60 57 bytes
$a,$b=$args
$r=(,$a),$y
$b|%{$r[$r[1]-or$_-eq$a]+=,$_}
$r
It takes the input as an array of ints, returns two arrays
Another approach for 59 bytes
param($a)$a[0..(($i=$a|% i*f $a[0] 1)-1)],$a[$i..$a.Length]
Takes the input as a string, returns two arrays
MATLAB/Octave, 63 bytes
function y=f(x)
l=find(x==x(1));y={x(1:l(2)-1),x(l(2):end)};end
Try it online!
Outputs cell aray with 2 cells, which hold appropriate vectors. I've chosen such output because rules say 2 lists must be distinguished, not necessarily be separate variables. And outputting 2 variables turned out to give a little longer code.
Ungolfed/explained:
function y = f(x)
l = find( x==x(1) ); % indices of elements equal to first element
l2 = l(2); % index of second occurence
y = { x(1:(l2-1)),... % vector containing elements before 2nd occurence
x(l2:end) }; % vector containing elements from 2nd occurence
end
Interestingly, it's also possible to create anonymous function that does the same, but it's 2 bytes longer:
@(x){x(1:find(x(2:end)==x(1),1)),x(find(x(2:end)==x(1),1)+1:end)}
Try it online!
It is possible to shorten it more as flawr noticed, resulting in 42 bytes but it's an Octave-only solution, not working for MATLAB:
@(x){x(1:(l=find(x==x(1))(2))-1),x(l:end)}
Factor, 37 35 bytes
[ dup first 1 pick index-from cut ]
Explanation:
dupDuplicate the input.Stack: (e.g.)
{ 0 2 2 3 0 1 0 1 } { 0 2 2 3 0 1 0 1 }firstGet first element.Stack:
{ 0 2 2 3 0 1 0 1 } 01Push1.Stack:
{ 0 2 2 3 0 1 0 1 } 0 1pickPut a copy of the object third from the top on top of the stack.Stack:
{ 0 2 2 3 0 1 0 1 } 0 1 { 0 2 2 3 0 1 0 1 }index-fromFind the index of0starting from index1in the sequence on top of the stack.Stack:
{ 0 2 2 3 0 1 0 1 } 4cutSplit a sequence in two at an index.Stack:
{ 0 2 2 3 } { 0 1 0 1 }
K (ngn/k), 11 bytes
{(2#*=x)_x}
(...)_xcut the input (x) at...2#*=xthe indices of the first two occurrences of the first value in the input
J, 17 16 bytes
(]{.,<@;@}.)<;.1
In a sentence:
Cut on first element and then meld together the tail elements.
Consider f 0 2 2 3 0 1 0 1
<;.1Cut using the first element as the fret:┌───────┬───┬───┐ │0 2 2 3│0 1│0 1│ └───────┴───┴───┘{.,<@;@}.First element of that result{.catted with tail of that result}., razed;and then reboxed<@.┌───────┬───────┐ │0 2 2 3│0 1 0 1│ └───────┴───────┘
Haskell, 56 bytes
f(h:t)=(h#t)[h]
(h#(x:y))a|x==h=(a,x:y)|0<1=(h#y)$a++[x]
Because why not?
flawr's suggestion, 33 bytes
f(x:y)|(a,b)<-break(==x)y=(x:a,b)
This is based on Zgarb's solution (f(x:y)|(a,b)<-span(/=x)y=(x:a,b)).