In the standard loopholes, the following is forbidden:

Claiming that your answer is written in "MyOwnLanguage", where the command x means "read a sequence of numbers, split them into groups of three, and print the last numbers of those groups where the second number is less than the first"

Here, we are going to do the exact same thing.


Given a sequence of positive integers, whose length is divisible by 3, split them into groups of three, and print the last numbers of those groups where the second number is less than the first.


Input               Output
[]                  []
[1,2,3,4,5,6,7,8,9] []
[2,1,3,5,4,6,8,7,9] [3,6,9]
[3,1,4,1,5,9,2,6,5] [4]
[100,99,123]        [123]
[123,123,456]       []
[456,123,789]       [789]


This is . Shortest answer in bytes wins.

Standard loopholes apply, so remember not to have a built-in command x that does this task.

  • 39
    \$\begingroup\$ Hmmm... Now I'm really tempted to create MyOwnLanguage and add the x command... :P \$\endgroup\$
    – DJMcMayhem
    Commented May 9, 2017 at 5:55
  • 7
    \$\begingroup\$ *remember not to have a built-in*‽ Well, if we already have it, we can use it, no? \$\endgroup\$
    – Adám
    Commented May 9, 2017 at 6:28
  • 4
    \$\begingroup\$ @Adám According to the standard loopholes, you cannot have a language containing the built-in x specifically performing that function. \$\endgroup\$
    – Leaky Nun
    Commented May 9, 2017 at 6:34
  • 37
    \$\begingroup\$ @LeakyNun Yes you can, you just cannot make such a language because of the challenge. If your language predates the challenge, it is acceptable. \$\endgroup\$
    – Adám
    Commented May 9, 2017 at 6:38
  • 14
    \$\begingroup\$ If I call the builtin p, can I use it? \$\endgroup\$ Commented May 9, 2017 at 16:02

51 Answers 51


Octave, 32 bytes


Try it online!


Verify test cases!

L3 = L(3:3:end)  %extract last elements of groups
d= diff(L)       % first difference of the list
y=d(1:3:end)     %extract first elements of each subgroup of the difference
idx = y<0        %check for negative numbers  
result = L3(idx)

Haskell, 30 29 bytes

x(a:b:c:l)=[c|b<a]++x l
x d=d

My first attempt at golfing Haskell, so I may have missed an optimization or two

-1 byte thanks to @JulianWolf

  • 4
    \$\begingroup\$ Nice answer! See codegolf.stackexchange.com/a/60884/66904 for a relevant tip; in particular, swapping the two definitions and writing the second (now first) as x d=d can save you a byte \$\endgroup\$ Commented May 9, 2017 at 16:21
  • \$\begingroup\$ Clever! I browsed that answer beforehand but must have missed the part where the definition reused the variable \$\endgroup\$
    – user46863
    Commented May 9, 2017 at 17:53

Jelly, 9 8 bytes


Try it online!

How it works

>Ḋm3T×3ị  Main link. Argument: A (array)

 Ḋ        Dequeue; yield A without its first element.
>         Compare the elements of A with the elements of the result.
  m3      Select each third element, starting with the first.
    T     Truth; get all indices of truthy elements.
     ×3   Multiply those indices by 3.
       ị  Unindex; retrieve the elements at the redulting indices.

Mathematica, 37 bytes

Assuming this does satisfy the spec, ngenisis gets credit for this approach leading to a 1-byte saving!


Pure function. BlockMap[...,#,3]& splits the input list into sublists of length 3 and then operates on each sublist with the function If[#>#2,Print@#3]&@@#&. The result is that each qualifying last number is printed. The function also returns a value (namely a list of Nulls a third as long as the input list), which seems to be allowed behavior.

Mathematica, 42 38 bytes

Thanks to Martin Ender for saving 4 bytes!


Pure function. #~Partition~3 does what you think. Cases[X,P:>Q] selects all the elements of X matching the pattern P, and returns the result of the transformation rule :>Q applied to each instance. Here, the pattern being matched is {a__,b_}/;a>0: b_ will match the last element of the list and a__ all the other elements (in this case, the first two); call them y and z for now. The sneaky a>0 then expands to y>z>0, which is the test we want to apply (valid because the spec says everything will be a positive integer). And the transformation rule is :>b, which simply replaces each matching ordered triple with its last element.

Original submission:


Pure function; pretty much a straightforward implementation, other than #.{1,-1,0} which calculates the difference between the first and second elements of each 3-element sublist.

  • 3
    \$\begingroup\$ The dot product is neat, but #>#2&@@#& is shorter. But overall it's still shorter to use Cases instead of Select: Cases[#~Partition~3,{a__,b_}/;a>0:>b]& \$\endgroup\$ Commented May 9, 2017 at 13:43
  • \$\begingroup\$ a>0:> has two kinds of magic in it! \$\endgroup\$ Commented May 9, 2017 at 17:39
  • \$\begingroup\$ BlockMap is tantalizing here. \$\endgroup\$
    – user61980
    Commented May 10, 2017 at 3:33
  • \$\begingroup\$ BlockMap[If[#>#2,#3,Nothing]&@@#&,#,3]& works and is only 39 bytes ... can we save a couple bytes? \$\endgroup\$ Commented May 10, 2017 at 5:28
  • 1
    \$\begingroup\$ BlockMap[If[#>#2,Print@#3]&@@#&,#,3]& arguably satisfies the spec \$\endgroup\$
    – user61980
    Commented May 10, 2017 at 16:26

R, 35 bytes

  • 3
    \$\begingroup\$ Is the leading whitespace necessary? \$\endgroup\$
    – hyper-neutrino
    Commented May 9, 2017 at 18:24
  • 1
    \$\begingroup\$ nice elegant solution \$\endgroup\$
    – MickyT
    Commented May 9, 2017 at 18:34

Pyth, 10 bytes


Test suite

       cQ3    Chop the input into groups of size 3
  f           Filter on
     PT       All but the last element
   >F         Apply the greater than function
eM            Map to the last element

Brachylog (2), 14 bytes


Try it online!

Brachylog rather struggles with this sort of problem. Note that this program has horrible computational complexity, as it brute-forces splitting the input into groups of 3 (having no "split into groups" builtin); it runs quickly with four groups but very slowly with five.


~c              Split into groups
  { }ᵐ          such that each group
   Ṫ            has three elements
      {     }ˢ  then on each element, skipping that element on error:
       k          with the list minus its last element
        >₁        assert that it's strictly decreasing
          &       and with the original list
           t      keep only its last element
  • 1
    \$\begingroup\$ Might be worth mentioning that l÷₃;?ḍ₍ is a faster alternative. \$\endgroup\$
    – Leaky Nun
    Commented May 9, 2017 at 6:41
  • \$\begingroup\$ I had that in an earlier attempt (using / not ÷; they're equivalent here), but it's a byte longer so I discarded it while golfing it down. \$\endgroup\$
    – user62131
    Commented May 9, 2017 at 6:43

JavaScript (ES6), 46 44 42 41 39 bytes

  • Saved 2 bytes thanks to Neil.

Try It

Input a comma separated list of numbers, without any spaces.

console.log(JSON.stringify(f([])))                  // []
console.log(JSON.stringify(f([1,2,3,4,5,6,7,8,9]))) // []
console.log(JSON.stringify(f([2,1,3,5,4,6,8,7,9]))) // [3,6,9]
console.log(JSON.stringify(f([3,1,4,1,5,9,2,6,5]))) // [4]
console.log(JSON.stringify(f([100,99,123])))        // [123]
console.log(JSON.stringify(f([123,123,456])))       // []
console.log(JSON.stringify(f([456,123,789])))       // [789]
<input id=i><pre id=o>


a=>              :Anonymous function taking the input array as an argument via parameter a
a.filter((_,y)=> :Filter the array by executing a callback function on each element,
                  with the index of the current element passed through parameter y.
                  If the function returns 0 for any element, remove it from the array.
y%3>1            :Check if the modulo of the current index is greater than 1.
                  (JS uses 0 indexing, therefore the index of the 3rd element is 2; 2%3=2)
&                :Bitwise AND.
a[y-1]<a[y-2]    :Check if the element at index y-1 in array a
                  is less than the element at index y-2
)                :End filtering method
  • 1
    \$\begingroup\$ Does y%3>1&a[y-1]<a[y-2] work? \$\endgroup\$
    – Neil
    Commented May 9, 2017 at 9:34
  • \$\begingroup\$ Crossed out 44 is still 44 \$\endgroup\$ Commented May 9, 2017 at 19:15
  • \$\begingroup\$ What do you mean, @RomanGräf? \$\endgroup\$
    – Shaggy
    Commented May 9, 2017 at 19:17
  • \$\begingroup\$ codegolf.meta.stackexchange.com/a/7427/56341 \$\endgroup\$ Commented May 9, 2017 at 19:21
  • \$\begingroup\$ A bug in "Arial,"Helvetica Neue",Helvetica,sans-serif" - well spotted @Roman \$\endgroup\$
    – flurbius
    Commented May 10, 2017 at 18:07

J, 14 bytes


This evaluates to a monadic verb. Try it online!


_3&(>`[/\#]/\)  Input is y.
_3&(    \    )  For each non-overlapping 3-element chunk of y,
    >`[/        check if first element is greater than second.
                Call the resulting array x.
_3&(        \)  For each non-overlapping 3-element chunk of y,
          ]/    take the last element.
         #      Keep those where the corresponding element of x is 1.

05AB1E, 8 bytes



Uses the 05AB1E encoding. Try it online!


Alice, 12 11 bytes

Thanks to Leo for saving 1 byte.


Try it online!

Uses the code points of a string as the input list and outputs the character corresponding to the outputs that should be kept.


I      Read x. Pushes -1 on EOF.
.h%    Compute x%(x+1). This terminates the program due to division by zero at EOF,
       but does nothing for non-negative x.
I      Read y.
-      Compute x-y. We only want to output z is this is positive.
r      Range. Pushes 0 1 ... n for positive n, and -n ... 1 0 for negative n
       (and simply 0 for n = 0). So this results in a positive number on top
       of the stack iff x-y is positive.
I      Read z.
~      Swap it with x-y > 0.
$O     Output z iff x-y > 0.
       Then the IP wraps to the beginning of the program to process the next triplet.
  • \$\begingroup\$ You can golf one byte using r instead of ex. TIO \$\endgroup\$
    – Leo
    Commented May 9, 2017 at 21:36
  • \$\begingroup\$ @Leo that's brilliant, thank you! \$\endgroup\$ Commented May 10, 2017 at 12:38

Husk, 8 bytes


Try it online!


This program is a bit involved, so bear with me.

ṁΓȯΓ↑<C3  Implicit input (list of integers).
      C3  Split into slices of length 3.
ṁ         Map over slices and concatenate results
 ΓȯΓ↑<    of this function, explained below.

The function ΓȯΓ↑< takes a list of length 3, x = [a,b,c]. The first Γ splits x into a and [b,c], and feeds them as arguments to the function ȯΓ↑<. This should be equivalent to ((Γ↑)<), but due to a bug/feature of the interpreter, it's actually equivalent to (Γ(↑<)), interpreted as a composition of Γ and ↑<. Now, a is fed to the latter function using partial application, the resulting function ↑<a is given to Γ, which deconstructs [b,c] into b and [c]. Then b is fed to ↑<a, resulting in a function that takes the first b<a elements from a list. This function is finally applied to [c]; the result is [c] if a>b, and [] otherwise. These lists are concatenated by to form the final result, which is printed implicitly.

Without the "feature", I would have 9 bytes:


Python 3, 43 42 bytes

1 byte thanks to xnor.

f=lambda a,b,c,*l:(b<a)*(c,)+(l and f(*l))

Try it online!


dc, 30 bytes


I/O: one number per line.


Perl 5, 31 bytes

30 bytes of code + -p flag.

s/\d+ (\d+) (\d+)/$2if$1<$&/ge

Try it online!

Replaces each group of 3 numbers (\d+ (\d+) (\d+)) by the third ($2) if the second ($1) is less than the first ($&), and nothing otherwise.


CJam, 15 bytes


Anonymous block which expects argument on the stack, and leaves the result on the stack.

Try it online! (Runs all test cases)


3/             e# Split the list into length-3 chunks.
  {            e# For each chunk:
   )           e#  Remove the last element.
    \:>        e#  Reduce the first 2 elements by greater than.
       {;}|    e#  If the first is not larger than the second, delete the third.
           }%  e# (end for)

Brain-Flak, 82 bytes


Try it online!

# Until the stack is empty (input is guaranteed to not contain 0)

  # Push 1 for greater than or equal to 0
  #  ^------^  This part is Top - (Second + 1)

  # If the second number was less than the first...

     # Get ready to push 2 zeros

       # Move the next number to the other stack

     # Push those 2 zeros

     # Pop 2 values.
     # This is either 2 zeros, or a 0 and a "last number" that shouldn't be printed

# End loop

# Switch to the stack where we stored the numbers to be printed

Jelly, 10 bytes


Try it online!


Verify test cases

-3 bytes thanks to @LeakyNun


s3         - split into groups of three
  µ     µ€ - on each group, do:
   ṪW      - return the third element as the only element of a list
     x     - repeat each element in that list the number of times
      >/   - corresponding to 1 if the second element of the group is greater than the first; 0 otherwise.

R, 31 bytes

Thanks to @Giuseppe again -1 byte:


Thanks to @Giuseppe again -2 bytes:


With the R 4.1 it is a new game play (34 bytes). ;)


where \(x) is a shorthand for the function(x).

37 bytes version with scan() which I do not like, but it makes it shorter.


Version with function() which is easier to test (41 byte)


Thanks to the @Giuseppe! Nice idea to use recycling of index.




> f(c())
> f(c(1,2,3,4,5,6,7,8,9))
> f(c(2,1,3,5,4,6,8,7,9))
[1] 3 6 9
> f(c(3,1,4,1,5,9,2,6,5))
[1] 4
> f(c(100,99,123))
[1] 123
> f(c(123,123,456))
> f(c(456,123,789))
[1] 789
  • \$\begingroup\$ you read x in from stdin by using x=scan() at the beginning instead of defining a function, You can also simply set i=c(1,2,0) since logical indices get recycled i.e., x=scan();i=c(1,2,0);x[!i][x[i>1]<x[i==1]] \$\endgroup\$
    – Giuseppe
    Commented May 9, 2017 at 15:31
  • \$\begingroup\$ tio.run/nexus/… \$\endgroup\$
    – Giuseppe
    Commented May 9, 2017 at 15:32
  • 2
    \$\begingroup\$ Right, but the goal is to generate as short a code as possible. Unfortunately for both of us, somebody else found a better solution! \$\endgroup\$
    – Giuseppe
    Commented May 9, 2017 at 19:39
  • 1
    \$\begingroup\$ 35 bytes \$\endgroup\$
    – Giuseppe
    Commented Jan 13, 2022 at 21:13
  • 1
    \$\begingroup\$ Actually, use 2:0 instead of -2:0! I was clearly hung up on the symmetry. \$\endgroup\$
    – Giuseppe
    Commented Jan 14, 2022 at 21:53

Python 2, 57 bytes

lambda i:[i[c+2]for c in range(0,len(i),3)if i[c+1]<i[c]]

Try it online!


MATL, 10 bytes


The result is displayed as numbers separated by spaces.

Try it online!

Or verify all test cases. This displays a string representation of the output, so that an empty array is actually seen as []. Note that in MATL a number is the same as a singleton array, so [4] is shown as 4.


Ie    % Implicit input. Reshape as a 3-row matrix (column-major order)
I&Y)  % Split into the third row and a submatrix with the other two rows
d     % Consecutive difference along each column of the submatrix
0<    % True for negative values
)     % Use as logical index into the original third row. Implicitly display

Röda, 15 bytes


Röda is nearly as short as the golfing languages...

This takes three values from the stream, and pushes the third (_3) back, if the second (_2) is less than the first (_1).

The underscores are syntax sugar for for loops, so the program could be written as {{[a]if[b<c]}for a,b,c} or even {[a]for a,b,c if[b<c]}.

No TIO link, because it doesn't work on TIO for some reason (although works with the latest version of Röda that predates the challenge).


Java 7, 86 85 bytes

void c(int[]a){for(int i=-1;++i<a.length;)if(a[i++]>a[i++])System.out.println(a[i]);}

-1 byte thanks to @PunPun1000


Try it here.

void c(int[]a){                  // Method with integer-array parameter and no return
  for(int i=-1;++i<a.length;)    //  Loop over the array in steps of three at a time
    if(a[i++]>a[i++])            //   If the value of the current index is larger than the next:
      System.out.println(a[i]);  //    Print the value on the third index
                                 //  End of loop (implicit / single-line body)
}                                // End of method
  • \$\begingroup\$ @PunPun1000 Now you've only incremented the iteration by 2 instead of 3 though, and therefore giving incorrect results (like 3,9 for the test case 1,2,3,4,5,6,7,8,9 instead of 3,6,9). \$\endgroup\$ Commented May 9, 2017 at 20:49
  • 1
    \$\begingroup\$ @Kevin_Cruijssen Oops you're right. You can still save a byte by using the increment operator though. You just have to start at -1 Try it online! \$\endgroup\$
    – PunPun1000
    Commented May 9, 2017 at 21:00
  • \$\begingroup\$ @PunPun1000 Ah, you're right, nice catch. Thanks! \$\endgroup\$ Commented May 9, 2017 at 21:16

C#, 126 Bytes

using System.Linq;i=>Enumerable.Range(0,i.Length/3).Select(u=>3*u).Where(u=>i[u]>i[u+1]).Select(u=>i[u+2]);

If you want a whole program with the method it'd be 175 Bytes:

using System.Linq;namespace S{class P{static System.Collections.IEnumerable X(int[]i)=>Enumerable.Range(0,i.Length/3).Select(u=>3*u).Where(u=>i[u]>i[u+1]).Select(u=>i[u+2]);}}

Saved 7 Bytes with the help of TheLethalCoder

  • \$\begingroup\$ You can just print those... \$\endgroup\$
    – Leaky Nun
    Commented May 9, 2017 at 6:52
  • \$\begingroup\$ @LeakyNun of course I could - but why should I? I asked wether it's neccessary, it's not, and it'd be more bytes, I guess. \$\endgroup\$
    – MetaColon
    Commented May 9, 2017 at 6:57
  • \$\begingroup\$ (int[]i) can just be i no need for the type. \$\endgroup\$ Commented May 9, 2017 at 15:24
  • \$\begingroup\$ @TheLethalCoder Updated it. \$\endgroup\$
    – MetaColon
    Commented May 9, 2017 at 19:05
  • \$\begingroup\$ @MetaColon You don't need the braces around (i) either. \$\endgroup\$ Commented May 10, 2017 at 7:49

Awk, 27 bytes


Vyxal, 8 bytes


Try it Online!

There are a ton of 8-byters but I haven't been able to find a single 7-byter.

3ẇ       # Cut into chunks of length 3
  R      # Reverse each chunk
   ∩÷    # Transpose and push each row to the stack
     <   # For each, check if second > first
      Tİ # Only keep third elements where ^ is truthy

CJam, 16 bytes


The output is shown as numbers separated by spaces.

Try it online!


q~               e# Read input list
  3/             e# List of sublists of length 3
   {         }%  e# Apply this to each sublist
    ~            e# Push sublist contents: 3 numbers
     @@          e# Rotate twice. This moves first two numbers to top
       >         e# Greater than?
        S{;}?    e# If so: push space (used as separator). Else: pop the third number
                 e# Implicitly display

PHP, 89 Bytes

<?print_r(array_filter($_GET,function($v,$k){return $k%3>1&&$_GET[$k-1]<$_GET[$k-2];},1));

Try it online!


JavaScript, 108 107 108 bytes

This is a valid JS anonymous (lambda) function. Add x= at the beginning and invoke like x([5,4,9,10,5,13]). Outputs as function return.


The snippet takes in the input as a list of comma separated integers.

martin.oninput = e => { dennis.innerHTML = x(martin.value.split`,`.map(c=>parseInt(c,10))) }
<input type=text id=martin><pre id=dennis>

  • \$\begingroup\$ What is the point at posting a longer solution, and using martin and dennis as id? \$\endgroup\$
    – Leaky Nun
    Commented May 9, 2017 at 6:52
  • \$\begingroup\$ @LeakyNun Shaggy posted his solution while I was working on mine. But that was no reason for me to not post my solution. As for using the names as id's, I thought it'd be funny. \$\endgroup\$
    – Arjun
    Commented May 9, 2017 at 7:03
  • \$\begingroup\$ This doesn't work for [5,4,9,10,5,13]. \$\endgroup\$
    – Shaggy
    Commented May 9, 2017 at 10:50
  • \$\begingroup\$ @Shaggy That was a problem with the implementation of the test case snippet; nothing wrong with the solution. Actually, the value of the input element is always a string. So, splitting the string on , resulted it into being an array of strings rather than numbers! The solution is perfectly fine. Only the test case snippet was wrong. I have fixed that, now. Thanks for pointing that out! :) \$\endgroup\$
    – Arjun
    Commented May 9, 2017 at 11:03
  • \$\begingroup\$ Oh, yeah, that explains the problem! Thank you, @Arjun. \$\endgroup\$
    – Shaggy
    Commented May 9, 2017 at 11:04

Perl5.8.9, 73 60 bytes


(58+2 for the 'n' flag to read the whole file and a to autosplit). Assumes the input is lines of space separated numbers

Reduction thanks to Dada. Including the print at the end for visibility, that'd save 8 bytes if not.

  • \$\begingroup\$ Nice one! Have your well deserved +1! \$\endgroup\$
    – Arjun
    Commented May 9, 2017 at 11:13
  • \$\begingroup\$ The output format being quite flexible, you don't really have to put that print"\n" at the end. Also, you can do $b[1]<$b[0]&&print"$b[2] "while@b=splice@a,0,3 to save 7 bytes. Finally, you can use -a flag instead of doing @a=split (it will do the same automatically and store the result in @F instead of @a); with Perl 5.8.9, you'll need -na while with recent Perls, -a is enough. That should get you to 47-48 bytes. \$\endgroup\$
    – Dada
    Commented May 9, 2017 at 12:00
  • \$\begingroup\$ oh, i didn't know about -a. I still think I should do one output line per input line, the output is pretty incomprehensbile otherwise \$\endgroup\$
    – Tom Tanner
    Commented May 9, 2017 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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