g | x | w | all
Bytes Lang Time Link
029Perl 5 p250514T182859ZXcali
026Juby230725T210333ZJordan
4543Python 2210507T114355ZJakque
046Julia210419T133238ZMarcMush
038Vim210420T133204ZAaroneou
014Pip210518T162421ZDLosc
00705AB1E210518T145749ZKevin Cr
054C gcc210420T170107Za stone
006Arn 1.0210421T014525ZZippyMag
184Scratch210420T001116ZNolan
021K oK210419T154955ZGalen Iv
241Racket210420T125945ZGalen Iv
059PHP F210420T090620ZKaddath
038Zsh o extendedglob210420T105615Zpxeger
019><>210420T110739ZSE - sto
051brainfuck210419T225830ZLevel Ri
038R210419T064104ZDominic
nan210419T191359Zqwatry
021Charcoal210419T191818ZNeil
023Wolfram Language Mathematica210419T054512Zatt
017J210418T231917ZJonah
006Japt210419T083827ZShaggy
043JavaScript ES7210418T235051ZArnauld
048Ruby210419T111618ZG B
059Haskell210419T055303ZDelfad0r
005Husk210419T040213ZRazetime
028Branch210419T033405Zhyperneu
054Retina 0.8.2210419T005346ZNeil
036convey210419T002728Zxash
032Factor210418T235421Zchunes
074Haskell210419T000728Zuser
004Jelly210418T231145ZUnrelate
013APL Dyalog Extended210418T232340Zrak1507

Perl 5 -p, 29 bytes

s|.|$/x(1!=abs$&-$`%10).$&|ge

Try it online!

Input as a single integer. Output has groups delimited by newline. An extra delimiter is placed before the output if the first digit is not 1.

J-uby, 26 bytes

:slice_when+(:-|:| |:!=&1)

Attempt This Online!

Python 2, 47 45 bytes (43 bytes?)

45 bytes :

p=0
for n in input():print`-n`[n-p:]or-n,;p=n

Try it online!

how it work:

With strings:

43 bytes?

I also have a "edgy" solution in 43 bytes, don't know if the output format is "reasonable" :

p=0
for n in input():print(n-p)**2==1,n;p=n

Try it online!

Julia, 47 46 bytes

f(s,p='a')=join(s.|>i->' '^((p-i)^2!=1)*(p=i))

Try it online!

Input is a list of Chars and output is a string with spaces to delimit groups, example:

f(['2','4','1','2','7','6','5','1']) = " 2 4 12 765 1"

Vim, 43 38 bytes

-5 bytes thanks to @DLosc by using clever hyphen evaluating shenanigans.

qqy3ll:if abs(<C-r>0)!=1
norm r|
en
l@qq@q

Try it online!

Takes input as a hyphen-separated list of numbers.

Explanation:

qq                        # Start recording macro 'q'
  y3l                     # Yank this character and the next two; this will be 'X-X'
     l                    # Move right one character
      :if                 # If...
          abs(      )     # The absolute value of...
              <C-r>0      # The previously yanked text, evaluated...
                     !=1  # Does not equal 1:
norm r|                   #   Replace the current character with '|'
en                        #   End if
l                         # Move right one character
 @q                       # Call macro 'q' recursively
   q                      # End macro
    @q                    # Call macro 'q'

Pip, 14 bytes

aWV(k1N_-BMPa)

Takes input via command-line argument as a string of digits. Outputs with separator , between groups and within groups. Try it online!

Explanation

          MPa   Map this function to each pair of digits in the input:
     1N          Count the number of 1's in
       _-B       the first digit minus the second (1 if the difference is +/-1, 0 otherwise)
   (k        )  Use that list of 1's and 0's to index into the preset variable k == ", "
                We now have a list of length len(a)-1 containing spaces and commas
aWV             Weave the input string with that list
                Autoprint (implicit)

05AB1E, 7 bytes

üα≠0šÅ¡

Try it online or verify all test cases.

Explanation:

ü        # For each overlapping pair in the (implicit) input-list:
 α       #  Get the absolute difference
  ≠      # Check for each that they're NOT 1 (0 if 1; 1 otherwise)
   0š    # Prepend a 0 to the list
     Å¡  # Split the (implicit) input-list at the truthy indices

C (gcc), 56 54 bytes

f(char*n){for(;*n;~-abs(putchar(*n)-*++n)&&puts(""));}

Try it online!

Takes a string of digits as input; prints output to STDOUT with each group on a separate line.

-2 thanks to @ceilingcat

Arn 1.0, 6 bytes

PWº··,

Explained

Unpacked: ::-.|=1

  _         Input; implied
::          Sort adjacent by
        _   First value
      -     Minus
        _   Second value
    .|      Absolute value
  =         Equals
    1       One

Scratch, (by @att) 184 bytes

Try it online!

All credit goes to @att

define(x
set[a v]to(
set[i v]to(
repeat(length of(x
change[i v]by(1
set[t v]to(letter(i)of(x
set[a v]to(join(a)((t)*(((2)*<(-1)contains((t)-(letter(length of(a))of(a)))?>)-(1
end
say(a

K (oK), 24 21 bytes

-3 bytes thanks to streetster!

{(?0,&~1=d*d:-':x)_x}

Try it online!

Racket, 241 bytes

(λ(s)(letrec([d(λ(t n c)(if(null?(cdr t))(reverse c)(if(=(sqr(-(car t)(cadr t)))1)(d(cdr t)(+ 1 n)c)(d(cdr t)1(cons n c)))))][g(λ(t c a)(if(null? c)(cons t a)(g(drop t(car c))(cdr c)(cons(take t(car c))a))))])(reverse(g s(d s 1'())'()))))

Try it online!

What a monstrosity! I won't be surprised if it can be done in under 100 bytes.

PHP -F, 59 bytes

for(;$n=$argn[$i++];$m=$n)$a.=abs($n-$m)-1?",$n":$n;echo$a;

Try it online!

Takes a string of digits, outputs a string with delimiter ",". Has a preceding delimiter except when first digit is 1.

Well, at least it beats the Scratch answer :P

Zsh -o extendedglob, 38 bytes

>-1;for a {wc<^*$[b?a-b:1];b=$a;<<<$a}

Try it online!

I really feel like there's some way to shave off 1 or 2 bytes from the $[b?a-b:1].

><>, 19 bytes

:n$:@-:1=$i=+1$-a*o

Looks like TIO's -v flag support broke, and fishlanguage.com is down, so no link.

Takes the list on the stack, from top to bottom.

brainfuck, 51 bytes

This beats some of the mainstream language answers!

(byte count excludes 4 unnecessary newlines added for clarity.)

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

Try it online!

Outputs each group of characters preceded by an ASCII 1 for example as below

123565441

The digits are interpreted as ASCII characters and the initial ASCII 1 separator appears because the ASCII codes 48-57 of the digits are not consecutive to the zeroes which fill the Brainfuck memory at the start. Removing the initial separator would take another few bytes of code (I think it's no more of an issue than a trailing newline separator. Others may disagree.)

Commented code

,[[<->>+<-]<  Take input in cell 0 and copy it to the cell 1 while subtracting from cell minus 1
-[>]>[>+<<]<+ Subtract 1 from cell minus 1 and if it is not now zero add 1 to cell 2 (relies on the fact that cell 0 is zero and cell 1 is positive) 
+[>]>[>+<<]<- Add 1 to cell minus 1 and if it is not now zero add 1 to cell 2
>>>-[.-]      Subtract 1 from cell 2 and if not now zero both tests above failed so output ascii 1 and then zero the cell  
<.>,]         Output the input character now in cell 1 and loop again until a zero byte terminator is encountered
              Second input character will go into cell 2 and will be copied into cell 3 and compared with the previous character in cell 1 and so on advancing 2 cells each time

R, 38 bytes

split(a<-scan(),diffinv(diff(a)^2!=1))

Try it online!

Python 2, 52 bytes

p=0
for n in input():print"X"*p*(abs(n-p)!=1),n,;p=n

The program separates the input with the character "X".

Try it online!

55 bytes

lambda n:reduce(lambda a,b:a+[abs(a[-1]-b)==1,b],n,[0])

Alternate 55 byte solution that uses reduce.

Try it online!

Charcoal, 21 bytes

P§θ⁰FΦθκ«¿⊖↔⁻IKKι⸿→Pι

Try it online! Link is to verbose version of code. Explanation:

P§θ⁰

Output the first character of the input without moving the cursor.

FΦθκ«

Loop over the remaining characters.

¿⊖↔⁻IKKι

If the current character is not a neighbour of the character under the cursor, then ...

⸿

... move to the start of the next line, ...

... otherwise move right one character.

Pι

Output the current character without moving the cursor.

Wolfram Language (Mathematica), 23 bytes

Split[#,!-1!=#2-#!=1&]&

Try it online!

Split can take a second argument, which is applied to adjacent pairs to see if they should be in the same group.

!-1!=#2-#!=1 has the same length as Abs[#2-#]==1, and asserts that -1, #2-#, and 1 are not all unequal - that is, -1==1 || -1==#2-# || #2-#==1.

J, 17 bytes

<;.1~1,1~:&|2-/\]

Try it online!

Thanks to rak for pointing out that repeated elements should start a new group

Japt, 6 bytes

Outputs a 2D array.

òÈaY É

Try it

òÈaY É     :Implicit input of array
ò          :Partition between pairs that return falsey (0)
 È         :When passed through this function as X & Y
  aY       :  Absolute difference
     É     :  Minus 1

JavaScript (ES7), 43 bytes

Saved 4 bytes thanks to @tsh

Expects an array of digit characters. Returns a string with the groups separated with commas.

a=>a.map(p=v=>(p-(p=v))**2-1?[,v]:v).join``

Try it online!

Ruby, 48 bytes

->l{y=0;l.chunk{|x|$.+=(y-y=x)**2-1}.map &:last}

Try it online!

Haskell, 59 bytes

f[]=[]
f(x:y)|(a:b):t<-f y,(x-a)^2==1=(x:a:b):t|0<1=[x]:f y

Try it online!

The relevant function is f, which takes a list as input and returns a list of lists as output.


Unfortunately the builtin

groupBy(\x y->(x-y)^2==1)
import Data.List

doesn't work, since Haskell's groupBy always compares new elements to the first element of the group.

Husk, 5 bytes

ġo=1≠

Try it online!

group on absolute difference = 1.

Branch, 28 bytes

/,^\b[N^-b;c;^*{[10.0]\n.b,]

Try it on the online Branch interpreter!

Takes input as a string of digits. Outputs with groups newline-separated and elements of a group joined in one string on each line. There is a leading newline; hopefully that's acceptable.

Explanation

/,                       Go to the left child and read a character (stores codepoint)
^\                       Go to the right child (just creates this node for operating on)
b                        Go to register B; this is the left child since it was created second
[                     ]  While [the left child] is true
 N                       Save the most recently inputted byte as register N
  ^-                     Go to the root and perform (left child) - (right child); the right child saves the previous input; initially codepoint 0
    b;c;                 Go to register B (left child) and copy parent; go to C (right child) and copy parent
        ^*               Go to parent and perform (left child) * (right child) (this could've been done with b;c2^' as well to square with conventional exponentiation)
          {              Decrement; this is now 0 if the absolute difference was 1 and non-zero otherwise
           [   0]        While statement; zeroes the value at the end so this is transformed into an if statement
            10.          Set value 10 and output as character (outputs a newline if the difference isn't 1 / -1)
                 \n      Go to the right child and save N
                   b,    Go to the left child and read a new character, setting the state for the next iteration of the while loop

Reading from STDIN when nothing is left gives 0, so this terminates once input ends.

Retina 0.8.2, 54 bytes

\B
¶
.
$&$&
%T`d`_d`^.
(?<=(.)(.))¶(\2|.(?=\1))

%`^.

Try it online! No test suite because the multiline output makes it confusing. Explanation:

\B
¶

Split the string into individual digits.

.
$&$&
%T`d`_d`^.

Duplicate each digit and decrement the first digit of each pair.

(?<=(.)(.))¶(\2|.(?=\1))

Merge together consecutive digits, removing the decremented duplicate of the second digit.

%`^.

Remove the remaining decremented duplicates.

convey, 36 bytes

Outputs the groups separated by newlines.

{"">>v>v}
1~?`]>^,`
 >-|=.!`
   _1 _

Try it online!

Copies " each element, delays it by one step 1~, so it matches up at - with the next element. The first element gets discarded with ?`]. Take the absolute value of the difference |_, is it different from 1 1=.? Then take either 0 or 1 _ values, which will be interpreted as newline by the output.

Meanwhile the original outputs trots along the upper line, so its output is aligned with the newlines.

example run

Factor, 33 32 bytes

[ [ - sq 1 = ] monotonic-split ]

Try it online!

Saved a byte by peeking at @xnor's Haskell answer! Take the square instead of the absolute value.

Explanation:

It's a quotation (anonymous function) that takes a sequence from the data stack as input and leaves a sequence (of sequences) on the data stack as output. Assuming { 1 2 4 } is on the data stack when this quotation is called...

Now the next iteration begins, 2 and 4 produce f, so 4 is in a different group from 1 and 2.

Haskell, 74 bytes

p%(a:b:t)|abs(a-b)/=1=(p++[a]):[]%(b:t)|x<-p++[a]=x%(b:t)
p%t=[p++t]
([]%)

Try it online!

I don't like the repetition here, but I can't see a way to get around it.

Jelly, 4 bytes

I²’k

Try it online!

Possibly caird's solution:

Jelly, 4 bytes

IACk

Try it online!

   k    Split the input after
I       the deltas of the input
 ……     equal to ±1:

 ²      square each
  ’     and decrement each

 A      take the absolute value of each
  C     and subtract from 1

APL (Dyalog Extended), 13 bytes

⊢⊂⍨1,1≠∘|2-/⊢
⊢⊂⍨1,1≠∘|2-/⊢
         2-/⊢  pairwise differences
     1≠∘|      1 not equal to absolute value
⊢⊂⍨1,          prepend 1 and partition

Try it online!