The forbidden built-in

53

3

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.

Task

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.

Testcases

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]

Scoring

This is . Shortest answer in bytes wins.

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

Leaky Nun

Posted 2017-05-09T04:36:51.567

Reputation: 45 011

33Hmmm... Now I'm really tempted to create MyOwnLanguage and add the x command... :P – James – 2017-05-09T05:55:05.603

6remember not to have a built-in‽ Well, if we already have it, we can use it, no? – Adám – 2017-05-09T06:28:22.903

Is it neccessary to print the Output, or can you just return it as well? – MetaColon – 2017-05-09T06:33:35.407

@MetaColon you can return it as well. – Leaky Nun – 2017-05-09T06:34:08.057

3@Adám According to the standard loopholes, you cannot have a language containing the built-in x specifically performing that function. – Leaky Nun – 2017-05-09T06:34:34.627

35@LeakyNun Yes you can, you just cannot make such a language because of the challenge. If your language predates the challenge, it is acceptable. – Adám – 2017-05-09T06:38:04.123

I'd be surprised if nobody has written a built-in doing exactly this, just in case somebody would post this exact question. A couple of esoteric languages have been created by users from this site after all. – Mast – 2017-05-09T09:21:46.200

@theonlygusti the task is the one defined as an example function in the loophole text... what's not to get? – Baldrickk – 2017-05-09T09:31:05.630

@Mast Only a couple? – boboquack – 2017-05-09T10:42:31.383

is the sequence formatting important? (as in would acceptable input be space separated and output be one per line? or should the output be the same format as the input or...) – Tom Tanner – 2017-05-09T10:55:01.567

@TomTanner Any permissible input/output format is permissible. – Leaky Nun – 2017-05-09T10:55:42.020

9If I call the builtin p, can I use it? – Mindwin – 2017-05-09T16:02:54.880

I think so, also if you call the language "MyOwlLanguage". – G B – 2017-05-10T06:32:57.910

1Take the question's id as source code in whatever base, and execute the first answer, profit. Predates any upcoming questions :p – Caramiriel – 2017-05-10T07:07:19.530

1

@Caramiriel: https://xkcd.com/1185/

– Adam – 2017-05-11T10:37:37.277

1@Adam: Wasn't meant as a serious answer, but that pretty much sums up why that isnt such a good idea. +1 – Caramiriel – 2017-05-11T11:14:05.417

Would returning a nested array be allowed? e.g. [[3],[6],[9]] – Shaggy – 2018-01-12T16:51:38.920

Answers

14

Octave, 32 bytes

@(L)L(x=3:3:end)(diff(L)(x-2)<0)

Try it online!

or

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)

rahnema1

Posted 2017-05-09T04:36:51.567

Reputation: 5 435

13

Jelly, 9 8 bytes

>Ḋm3T×3ị

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.

Dennis

Posted 2017-05-09T04:36:51.567

Reputation: 196 637

12

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

user46863

Posted 2017-05-09T04:36:51.567

Reputation:

4

Nice answer! See https://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

– Julian Wolf – 2017-05-09T16:21:09.010

Clever! I browsed that answer beforehand but must have missed the part where the definition reused the variable – None – 2017-05-09T17:53:40.467

11

Mathematica, 37 bytes

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

BlockMap[If[#>#2,Print@#3]&@@#&,#,3]&

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!

Cases[#~Partition~3,{a__,b_}/;a>0:>b]&

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:

Last/@Select[#~Partition~3,#.{1,-1,0}>0&]&

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.

Greg Martin

Posted 2017-05-09T04:36:51.567

Reputation: 13 940

3The 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]& – Martin Ender – 2017-05-09T13:43:36.793

a>0:> has two kinds of magic in it! – Greg Martin – 2017-05-09T17:39:27.187

BlockMap is tantalizing here. – ngenisis – 2017-05-10T03:33:37.760

BlockMap[If[#>#2,#3,Nothing]&@@#&,#,3]& works and is only 39 bytes ... can we save a couple bytes? – Greg Martin – 2017-05-10T05:28:33.317

1BlockMap[If[#>#2,Print@#3]&@@#&,#,3]& arguably satisfies the spec – ngenisis – 2017-05-10T16:26:14.690

8

Pyth, 10 bytes

eMf>FPTcQ3

Test suite

eMf>FPTcQ3
       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

isaacg

Posted 2017-05-09T04:36:51.567

Reputation: 39 268

8

R, 35 bytes

(x=matrix(scan(),3))[3,x[2,]<x[1,]]

user11599

Posted 2017-05-09T04:36:51.567

Reputation: 191

3Is the leading whitespace necessary? – HyperNeutrino – 2017-05-09T18:24:22.290

1nice elegant solution – MickyT – 2017-05-09T18:34:29.730

5

Brachylog (2), 14 bytes

~c{Ṫ}ᵐ{k>₁&t}ˢ

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.

Explanation

~c{Ṫ}ᵐ{k>₁&t}ˢ
~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

user62131

Posted 2017-05-09T04:36:51.567

Reputation:

Might be worth mentioning that l÷₃;?ḍ₍ is a faster alternative. – Leaky Nun – 2017-05-09T06:41:18.603

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. – None – 2017-05-09T06:43:37.183

4

J, 14 bytes

_3&(>`[/\#]/\)

This evaluates to a monadic verb. Try it online!

Explanation

_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.

Zgarb

Posted 2017-05-09T04:36:51.567

Reputation: 39 083

4

Alice, 12 11 bytes

Thanks to Leo for saving 1 byte.

I.h%I-rI~$O

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.

Explanation

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.

Martin Ender

Posted 2017-05-09T04:36:51.567

Reputation: 184 808

You can golf one byte using r instead of ex. TIO

– Leo – 2017-05-09T21:36:03.080

@Leo that's brilliant, thank you! – Martin Ender – 2017-05-10T12:38:36.250

3

Jelly, 10 bytes

s3µṪWx>/µ€

Try it online!

or

Verify test cases

-3 bytes thanks to @LeakyNun

Explanation

s3µṪWx>/µ€
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.

fireflame241

Posted 2017-05-09T04:36:51.567

Reputation: 7 021

11 bytes: s3µṪ×>/µ€ḟ0

– Leaky Nun – 2017-05-09T05:36:48.303

10 bytes: s3µṪWx>/µ€

– Leaky Nun – 2017-05-09T05:38:23.543

3

JavaScript (ES6), 46 44 42 41 39 bytes

a=>a.filter((_,y)=>y%3>1&a[y-1]<a[y-2])
  • Saved 2 bytes thanks to Neil.

Try It

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

f=
a=>a.filter((_,y)=>y%3>1&a[y-1]<a[y-2])
i.oninput=_=>o.innerText=JSON.stringify(f(i.value.split`,`.map(eval)))
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>

Explanation

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

Shaggy

Posted 2017-05-09T04:36:51.567

Reputation: 24 623

1Does y%3>1&a[y-1]<a[y-2] work? – Neil – 2017-05-09T09:34:25.760

Crossed out 44 is still 44 – Roman Gräf – 2017-05-09T19:15:09.983

What do you mean, @RomanGräf? – Shaggy – 2017-05-09T19:17:14.617

https://codegolf.meta.stackexchange.com/a/7427/56341 – Roman Gräf – 2017-05-09T19:21:34.473

A bug in "Arial,"Helvetica Neue",Helvetica,sans-serif" - well spotted @Roman – flurbius – 2017-05-10T18:07:42.750

3

05AB1E, 8 bytes

Code:

3ôʒR`‹i,

Uses the 05AB1E encoding. Try it online!

Adnan

Posted 2017-05-09T04:36:51.567

Reputation: 41 965

3

dc, 30 bytes

[???sAz1[[lAps.]s.<.dx]s.<.]dx

I/O: one number per line.

eush77

Posted 2017-05-09T04:36:51.567

Reputation: 1 280

3

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.

Dada

Posted 2017-05-09T04:36:51.567

Reputation: 8 279

3

R, 37 bytes

Version with scan() which I do not like, but it makes it shorter.

x=scan();x[(i<--1:1)>0][x[!i]<x[i<0]]

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

f=function(x)x[(i<--1:1)>0][x[!i]<x[i<0]]

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

Test:

f(c())
f(c(1,2,3,4,5,6,7,8,9))
f(c(2,1,3,5,4,6,8,7,9))
f(c(3,1,4,1,5,9,2,6,5))
f(c(100,99,123))
f(c(123,123,456))
f(c(456,123,789))

Output:

> f(c())
NULL
> f(c(1,2,3,4,5,6,7,8,9))
numeric(0)
> 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))
numeric(0)
> f(c(456,123,789))
[1] 789

djhurio

Posted 2017-05-09T04:36:51.567

Reputation: 1 113

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]] – Giuseppe – 2017-05-09T15:31:45.877

Thanks @Giuseppe! I do not like this x=scan() approach as it makes the input very cumbersome. And I can not make it repeatable then. – djhurio – 2017-05-09T19:34:56.393

2Right, but the goal is to generate as short a code as possible. Unfortunately for both of us, somebody else found a better solution! – Giuseppe – 2017-05-09T19:39:18.657

Heh, I had as well idea to use matrix() but somehow I did not believed it will be possible to make so short. – djhurio – 2017-05-09T19:45:56.387

3

CJam, 15 bytes

{3/{)\:>{;}|}%}

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

Try it online! (Runs all test cases)

Explanation

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)

Business Cat

Posted 2017-05-09T04:36:51.567

Reputation: 8 927

3

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
<>

Riley

Posted 2017-05-09T04:36:51.567

Reputation: 11 345

3

Husk, 8 bytes

ṁΓȯΓ↑<C3

Try it online!

Explanation

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:

ṁΓoΓo↑<C3

Zgarb

Posted 2017-05-09T04:36:51.567

Reputation: 39 083

2

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!

Leaky Nun

Posted 2017-05-09T04:36:51.567

Reputation: 45 011

Using splatted input saves a byte. – xnor – 2017-05-09T06:05:03.933

2

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

MetaColon

Posted 2017-05-09T04:36:51.567

Reputation: 391

You can just print those... – Leaky Nun – 2017-05-09T06:52:40.283

@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. – MetaColon – 2017-05-09T06:57:48.067

(int[]i) can just be i no need for the type. – TheLethalCoder – 2017-05-09T15:24:10.127

@TheLethalCoder Updated it. – MetaColon – 2017-05-09T19:05:20.697

@MetaColon You don't need the braces around (i) either. – TheLethalCoder – 2017-05-10T07:49:45.897

2

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

Explanation:

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

Kevin Cruijssen

Posted 2017-05-09T04:36:51.567

Reputation: 67 575

@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). – Kevin Cruijssen – 2017-05-09T20:49:56.590

1

@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!

– PunPun1000 – 2017-05-09T21:00:36.797

@PunPun1000 Ah, you're right, nice catch. Thanks! – Kevin Cruijssen – 2017-05-09T21:16:24.037

2

MATL, 10 bytes

IeI&Y)d0<)

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.

Explanation

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

Luis Mendo

Posted 2017-05-09T04:36:51.567

Reputation: 87 464

2

Röda, 15 bytes

{[_3]if[_2<_1]}

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).

fergusq

Posted 2017-05-09T04:36:51.567

Reputation: 4 867

1

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.

a=>(y=[],a.map((c,i)=>(i+1)%3?0:y.push(a.slice(i-2,i+1))),y.map(v=>v[1]<v[0]?v[2]:null).filter(c=>c|c==0))

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

x=a=>(y=[],a.map((c,i)=>(i+1)%3?0:y.push(a.slice(i-2,i+1))),y.map(v=>v[1]<v[0]?v[2]:null).filter(c=>c|c==0))
martin.oninput = e => { dennis.innerHTML = x(martin.value.split`,`.map(c=>parseInt(c,10))) }
<input type=text id=martin><pre id=dennis>

Arjun

Posted 2017-05-09T04:36:51.567

Reputation: 4 544

What is the point at posting a longer solution, and using martin and dennis as id? – Leaky Nun – 2017-05-09T06:52:01.547

@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. – Arjun – 2017-05-09T07:03:53.003

This doesn't work for [5,4,9,10,5,13]. – Shaggy – 2017-05-09T10:50:56.980

@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! :) – Arjun – 2017-05-09T11:03:21.673

Oh, yeah, that explains the problem! Thank you, @Arjun. – Shaggy – 2017-05-09T11:04:20.787

@Shaggy No, it's I who should be thankful! Thanks, once again! :) – Arjun – 2017-05-09T11:14:47.683

1

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!

ElPedro

Posted 2017-05-09T04:36:51.567

Reputation: 5 301

1

CJam, 16 bytes

q~3/{~@@>S{;}?}%

The output is shown as numbers separated by spaces.

Try it online!

Explanation

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

Luis Mendo

Posted 2017-05-09T04:36:51.567

Reputation: 87 464

1

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!

Jörg Hülsermann

Posted 2017-05-09T04:36:51.567

Reputation: 13 026

1

Ruby, 37 35 bytes

->a{a.map{x,y,z,*a=a;z&&x>y&&p(z)}}

Try it online!

How it works:

->a{
    a.map{                          -> Loop [a.size] times
          x,y,z,*a=a;               -> get and remove 3 elements from a
                     z&&x>y&&p(z)   -> print z if not null and x>y
                                 }} 

G B

Posted 2017-05-09T04:36:51.567

Reputation: 11 099

I think you need each_slice, not each_cons. each_cons yields overlapping groups. – histocrat – 2017-05-10T14:17:10.753

You are right, I'm fixing it – G B – 2017-05-11T06:21:28.097

1

Retina, 42 36 bytes

\d+
$*
M!`1+ 1+ 1+
A`^(1+) \1
%r`1\G

Try it online! Takes input as a space-separated list of numbers and outputs a newline-separated list of numbers. Edit: Saved 6 bytes thanks to @MartinEnder. Explanation:

\d+             Match input numbers
$*              Convert to unary
M!`1+ 1+ 1+     Capture groups of three numbers
A`^(1+) \1      Filter out if the first is less than or equal to the second
%r`1\G          Convert the third to decimal

Neil

Posted 2017-05-09T04:36:51.567

Reputation: 95 035

M! saves a byte over S...(...). The second stage needs a ^ otherwise it can yield false positives where the third is less than the second, but then it's shorter to negate the logic with A\^(1+) \1. And the last stage can be shortened tor`1\G` (count consecutive ones from the end of the string). Link: https://tio.run/nexus/retina#@x@Tos2losXlq5hgqK0ARlyOCXEahtqaCjGGXEUJhjHu//8bKxgqmACxqYKlgpGCmYIpAA – Martin Ender – 2017-05-09T13:47:55.180

@MartinEnder My second stage doesn't need a ^ because there's no space after the third value, but A is a better idea anyway, thanks. – Neil – 2017-05-09T14:06:56.147

oh, I didn't see that the space was part of the capturing group, that works as well then – Martin Ender – 2017-05-09T14:07:39.743

I think the last stage needs a % modifier, otherwise this will return only the last value. TIO

– Leo – 2017-05-09T21:46:12.793

@Leo Thanks, I see what you mean. – Neil – 2017-05-09T22:03:06.737

1

C, 65 bytes

Try Online

i;f(s,l)int*l;{for(;i+2<s;i++)l[i]>l[i+1]&&printf(" %d",l[i+2]);}

Khaled.K

Posted 2017-05-09T04:36:51.567

Reputation: 1 435

This code doesn't work. s is supposed to be the number of groups of 3, right? Doesn't work when s is number of numbers either.

– simon – 2017-05-10T19:39:00.250

@gurka s is the size of array l, I've fixed the code, check here Try Online

– Khaled.K – 2017-05-11T06:38:29.163

1

Perl5.8.9, 73 60 bytes

while(@F){@b=splice@F,0,3;$b[1]<$b[0]&&print$b[2]}print"-"

(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.

Tom Tanner

Posted 2017-05-09T04:36:51.567

Reputation: 251

Nice one! Have your well deserved +1! – Arjun – 2017-05-09T11:13:01.587

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. – Dada – 2017-05-09T12:00:03.597

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 – Tom Tanner – 2017-05-09T14:03:09.617

1

Clojure, 43 bytes

#(for[[a b c](partition 3 %):when(< b a)]c)

Boring :/

NikoNyrh

Posted 2017-05-09T04:36:51.567

Reputation: 2 361

1

Bash, 61 bytes

a=($@)
for((t=0;t<$#;t+=3)){((a[t+1]<a[t]))&&echo ${a[t+2]};}

Try it online!

Maxim Mikhaylov

Posted 2017-05-09T04:36:51.567

Reputation: 571

1

Mathematica, 42 39 bytes

Edit: Saved 3 bytes thanks to Greg Martin

BlockMap[Apply[If[#>#2,#3,Nothing]&],#,3]&

BlockMap[If[#>#2,#3,Nothing]&@@#&,#,3]&

Not shorter than Greg Martin's answer, but I couldn't resist the opportunity to finally use BlockMap. Partitions the input list # into sublists of size 3 and Applys the functions If[#>#2,#3,Nothing]& to each sublist.

Questionable solution, 38 bytes

BlockMap[If[#>#2,Print@#3]&@@#&,#,3]&

Same as above, except it prints the desired elements and returns {Null,...Null}.

ngenisis

Posted 2017-05-09T04:36:51.567

Reputation: 4 600

1

Scala, 74 66 bytes

Edit: Lost 8 bytes thanks to @AlecZorab

The suggestion was to use collect() instead of the equivalent filter() followed by map() (which my for comprehension is equivalent to) and a pattern match, kind of like:

(_:Seq[Int]).grouped(3).collect{case(Seq(a,b,c))if(a>b)=>c}.toList

Another suggestion was to drop the parameter name as it was only used once.

The character/byte count can be dropped to 59 bytes if an iterator is an acceptable output. Usage is similar to the old version.

Old version:

(s:Seq[Int])=>(for(l<-s.grouped(3)if l.size>2&l(0)>l(1))yield l(2)).toList

Straightforward. The toList at the end is to consume the iterator produced - toSeq produces a Stream in my tests, which shows only the first element. If the iterator itself is legal as output, this version comes in at 64 bytes:

(s:Seq[Int])=>for(l<-s.grouped(3)if l.size>2&l(0)>l(1))yield l(2)

Scala allows Unicode operators, so I could lower the character count to 62 at the cost of increasing the byte count to 66:

(s:Seq[Int])⇒for(l←s.grouped(3)if l.size>2&l(0)>l(1))yield l(2)

Lambda, the parameter type is required in the REPL. Enter into the REPL and call the res0 or whatever function object it shows with the Seq[Int] to test with. For ease of testing, you can consider the following function which converts a test case String into a proper Seq[Int]:

def testCaseAsSeq(input: String): Seq[Int] =
    input.drop(1).dropRight(1)
         .split(",")
         .filter(! _.isEmpty)
         .map(_.toInt).toSeq

Prettified version with some obfuscations removed to make it more standard Scala:

(sequence: Seq[Int]): List[Int] => 
                    (for(group <- s.grouped(3) 
                    if group.size == 3 && group(0) > group(1))
                        yield group.last)
                    .toList

Tamoghna Chowdhury

Posted 2017-05-09T04:36:51.567

Reputation: 373

couple of things you can do to tighten that up a little First up, if you write it without the for comprehension you get (s:Seq[Int]) => s.grouped(3).filter(g => g.size==3 && g(0) > g(1)).map(g => g(2)) since s is only used once, you can replace the start with (_: Seq[Int]).grouped, saving a couple of characters. Then we notice that a filter and a map in scala is called collect, so you can merge them into – AlecZorab – 2017-05-11T09:08:24.180

... collect{ case g if g.size == 3 && g(0) > g(1) => g(2)}. Then we can replace that with a pattern match, giving (_:Seq[Int]).grouped(3).collect{case(Seq(a,b,c))if(a>b)=>c}.toList (66 bytes) – AlecZorab – 2017-05-11T09:14:38.437

1

><>, 19 + 3 for -v = 22 bytes

rl?!;(?\~r
naaao~ \

Aaron

Posted 2017-05-09T04:36:51.567

Reputation: 3 689

1

Japt, 18 12 bytes

ò3 f_v >ZvÃc

Try it online

Or 10 bytes if returning a nested array is permitted, e.g. [[3],[6],[9]]

ò3 f_v >Zv

Shaggy

Posted 2017-05-09T04:36:51.567

Reputation: 24 623

0

Mathematica 65 bytes

{K={};Cases[Partition[#,3],{a_,b_,c_}->If[b<a,AppendTo[K,c]]];K}&

J42161217

Posted 2017-05-09T04:36:51.567

Reputation: 15 931

0

Awk, 27 bytes

{a[b=NR%3]=$0}!b&&a[2]<a[1]

muru

Posted 2017-05-09T04:36:51.567

Reputation: 331

0

GolfScript, 17 bytes

~3/{~@@>{}{;}if}%

Try it online!

Explanation

~                 # Evaluate the input
 3/               # Split the input into chunks of 3
   {           }% # Map every item in the input
    ~@@>          # Is the 1st item < second item?
        {}{;}if   # If false, discard the current item

user85052

Posted 2017-05-09T04:36:51.567

Reputation: