31
\$\begingroup\$

Why should you golf in Haskell? Find out here. In that answer, Zgarb defines a task:

Let's define a function f that splits a list at the second occurrence of the first element, e.g. [0,2,2,3,0,1,0,1] -> ([0,2,2,3],[0,1,0,1]):

Alright then, lets!

You are to take a non-empty list consisting of digits, and output a pair of lists, clearly distinguished, such that the output is the input split before the second occurrence of the first element.

For example, you may output two strings of digits with a non-digit separator. The separator you choose between the elements of each pair and the pairs themselves must be distinct (e.g. space and newline), consistent and not contain digits.

The output must only contain 2 elements however. You may not output empty lists in the output. If you choose to use newlines as a separator between pairs, you may not have a leading newline. You may have trailing newlines no matter what, and you may have trailing whitespace, so long as its sensible.

You may assume:

  • The input will be provided in any convenient method, and you may output in any convenient method
  • The input will either be a list of digits, or a string
  • The input will only contain the integers 0 though to 9
  • The first element of the input will always occur at least twice
  • The input will always have 3 or more elements in it

Additionally, this is , so the shortest code in bytes wins

Test cases

[1, 1, 9] -> [[1], [1, 9]]
[4, 9, 4] -> [[4, 9], [4]]
[5, 7, 5, 5] -> [[5, 7], [5, 5]]
[8, 8, 0, 7] -> [[8], [8, 0, 7]]
[7, 1, 5, 7, 4, 2] -> [[7, 1, 5], [7, 4, 2]]
[0, 6, 9, 1, 1, 0, 2] -> [[0, 6, 9, 1, 1], [0, 2]]
[2, 9, 3, 2, 4, 2, 5, 9] -> [[2, 9, 3], [2, 4, 2, 5, 9]]
[0, 2, 2, 3, 0, 1, 0, 1] -> [[0, 2, 2, 3], [0, 1, 0, 1]]
[2, 7, 4, 6, 2, 6, 6, 4, 8, 2] -> [[2, 7, 4, 6], [2, 6, 6, 4, 8, 2]]
[8, 2, 2, 7, 5, 4, 7, 0, 8, 0, 7] -> [[8, 2, 2, 7, 5, 4, 7, 0], [8, 0, 7]]
[8, 7, 8, 9, 4, 2, 9, 4, 5, 7, 5, 1, 9] -> [[8, 7], [8, 9, 4, 2, 9, 4, 5, 7, 5, 1, 9]]
[3, 8, 1, 1, 7, 3, 6, 9, 7, 1, 4, 3, 4] -> [[3, 8, 1, 1, 7], [3, 6, 9, 7, 1, 4, 3, 4]]
[4, 7, 0, 5, 6, 5, 0, 1, 7, 8, 7, 8, 4, 1] -> [[4, 7, 0, 5, 6, 5, 0, 1, 7, 8, 7, 8], [4, 1]]
[2, 1, 8, 0, 3, 2, 2, 5, 7, 9, 4, 3, 5, 1, 9, 6, 9] -> [[2, 1, 8, 0, 3], [2, 2, 5, 7, 9, 4, 3, 5, 1, 9, 6, 9]]
[1, 1, 4, 1, 2, 5, 5, 3, 3, 4, 3, 2, 0, 8, 6, 0, 3] -> [[1], [1, 4, 1, 2, 5, 5, 3, 3, 4, 3, 2, 0, 8, 6, 0, 3]]
[4, 3, 5, 2, 2, 0, 6, 4, 8, 6, 6, 6, 7, 3, 4, 8, 7, 6] -> [[4, 3, 5, 2, 2, 0, 6], [4, 8, 6, 6, 6, 7, 3, 4, 8, 7, 6]]
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Brownie points for beating my quickly hacked together 8 byte Jelly answer :) \$\endgroup\$ Jun 3 '21 at 21:04
  • \$\begingroup\$ If I'm outputting each value as a single digit on its own line, can I use a separator that contains digits but is clearly distinct from the other values. Like this? \$\endgroup\$
    – pxeger
    Jun 4 '21 at 8:29
  • 1
    \$\begingroup\$ @AidenChow You can output a single string with a clear separator between the parts (see Neil's Retina answer), does that work? \$\endgroup\$ Jun 4 '21 at 20:33
  • 1
    \$\begingroup\$ @AidenChow If your language supports lists of integers, I believe that allowing people to output a flat list with -1 as a "separator" shouldn't break the challenge (e.g. [0,2,2,3,0,1,0,1] -> [0,2,2,3,-1,0,1,0,1]), so you're welcome to use that format. Otherwise, I don't see how you could use that language here :( \$\endgroup\$ Jun 4 '21 at 20:48
  • 1
    \$\begingroup\$ @cairdcoinheringaahing Yes that can work! Thanks for being flexible with your specifications regarding I/O. \$\endgroup\$
    – Aiden Chow
    Jun 5 '21 at 4:30

48 Answers 48

1
2
2
\$\begingroup\$

><>, 21 bytes

<0o{oa.0*3+)3l=}:{::i

Try it online!

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
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 13 bytes (SBCS)

Anonymous tacit prefix function

⊢⊂⍨1,⊃<\⍤=1↓⊢

Try it online!

 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

\$\endgroup\$
2
\$\begingroup\$

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:]]
\$\endgroup\$
2
\$\begingroup\$

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);}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 55 bytes \$\endgroup\$
    – jdt
    Aug 28 '21 at 13:11
2
\$\begingroup\$

Brain-Flak, 112 bytes

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I don't think this correct, based on the TIO output. The split should happen before the second 0, so both lines would start with 0. Looks like you're splitting one place to early. Also, it looks like the first 1 is being turned into a 0? \$\endgroup\$ Aug 28 '21 at 10:45
  • 1
    \$\begingroup\$ @Dudecoinheringaahing Whoops. I posted the wrong version. That version did something very odd. \$\endgroup\$
    – Wheat Wizard
    Aug 28 '21 at 11:09
1
\$\begingroup\$

C (gcc), 66 bytes

k;f(d,l)int*d;{k=printf(" %d"+(!l||d[l]-f(d,l)),k=d[--l])-1?-1:k;}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Clojure, 44 bytes

#(split-at(inc(.indexOf(rest %)(first %)))%)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

><>, 18 bytes

i:vi
=?\o:i:@
oa<^

Try it online!

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.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 41 bytes

def f(a):a.insert(a[1:].index(a[0])+1,-1)

Try it online!

Another Python approach using the allowed flexible output format. Not any shorter than the existing solution.

\$\endgroup\$
1
\$\begingroup\$

PowerShell, 47 42 39 38 bytes

$args-split"(?<=.)(?=$("$args"[0]))",2

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Looks like you can remove the $z= bit: Try it online! \$\endgroup\$ Jun 4 '21 at 20:40
  • \$\begingroup\$ Thanks. No, the operator -match returns a boolean value as result. The $z= catches this boolean value and does not return from the function \$\endgroup\$
    – mazzy
    Jun 4 '21 at 20:54
  • \$\begingroup\$ Thanks angain. I've found another solution. \$\endgroup\$
    – mazzy
    Jun 4 '21 at 21:26
1
\$\begingroup\$

Jelly, 6 bytes

CiḢ$Ṭk

Try it online!

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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]

Try It On Desmos!

Try It On Desmos! - Prettified

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

PHP, 89 bytes

function($x){$y=array_keys($x,$x[0])[1];return[array_slice($x,0,$y),array_slice($x,$y)];}

Try it online!

Explanation:

  • array_keys returns all index positions of an element. Accessing $x[1], we get the second occurrence.

  • array_slice Gets you a subpart of the array. Param => (array, start, length[optional])

\$\endgroup\$
1
\$\begingroup\$

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)}
\$\endgroup\$
1
\$\begingroup\$

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 ⌊A from Ans by calling it like this: {list}:prgmL
  • 2→I: Initialize I with 2
  • While ⌊A(I)≠⌊A(1: While the current element is not equal to the first
    • I+1→I: Increment I
  • End
  • Disp seq(⌊A(J),J,1,I-1: Display the first chunk of the list
  • seq(⌊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
\$\endgroup\$
2
  • \$\begingroup\$ As TI-BASIC uses a tokenised encoding, I think you can just count this as 69 bytes and get rid of the text count. Additionally, you have a typo in your explanation ("Set I to 1" but it has 2) \$\endgroup\$ Jul 29 '21 at 18:34
  • \$\begingroup\$ Alright, thanks for pointing that out lol \$\endgroup\$
    – orangey
    Jul 30 '21 at 0:12
0
\$\begingroup\$

Zsh, 37 26 bytes

Using Zsh array parsing. The separator is an underscore, _

A=$@
<<<${(SI:2:)A/$1/_$1}

Try it Online! 37 bytes

\$\endgroup\$
1
  • \$\begingroup\$ Equals the Haskell solution! 👍🏼 \$\endgroup\$
    – roblogic
    Aug 29 '21 at 22:32
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.