Trim the array!

Given an integer array and two numbers as input, remove a certain amount of the first and last elements, specified by the numbers. The input can be in any order you want.

You should remove the first x elements, where x is the first numerical input, and also remove the last y elements, where y is the second numerical input.

The resulting array is guaranteed to have a length of at least two.

Examples:

[1 2 3 4 5 6] 2 1 -> [3 4 5]
[6 2 4 3 5 1 3] 5 0 -> [1 3]
[1 2] 0 0 -> [1 2]

• What, exactly, does it mean to "remove" values from an array—especially to remove them from the end? In languages like C, where an array is just a pointer to the first element and a length, can we just change the length to truncate the array? That's what would normally be done in real-world programming, but the challenge is unclear to me. – Cody Gray Jun 26 '17 at 16:40
• @CodyGray Removing values from the array is what it should look like, but not necessarily what goes on behind the scenes. – Okx Jun 26 '17 at 16:42
• What do you mean by "look like"? Arrays don't have a look – it is all behind the scenes! – Cody Gray Jun 26 '17 at 16:43
• @Michthan Try installing the PPCG userscript – Okx Jun 27 '17 at 10:59
• @Okx Nope, that's very buggy, I'd recommend adding a leaderboard. – Erik the Outgolfer Jun 27 '17 at 11:01

Pyth, 4 bytes

<E>E


Demonstration

Put the list last.

Ruby, 17 bytes

->a,b,c{a[b..~c]}


Try it online!

Kona/K4, 12 bytes

{x _(-y)_ z}


Usage:

   {x _(-y)_ z}[2;1;"hello world"]
"llo worl"


K (oK), 10 bytes

{(-z)_y_x}


Try it online!

Jelly, 5 bytes

Ṗ¡Ḋ⁴¡


Try it online!

• Also 5 bytes :) – HyperNeutrino Jun 26 '17 at 14:37
• @HyperNeutrino I had this version too...but I decided this is more elegant. :) – Erik the Outgolfer Jun 26 '17 at 16:50

05AB1E, 6 bytes

F¦}sF¨


Try it online!

F }    # For 0 .. number of elements to remove from front
¦     #   Remove the first element
s   # Get the next input
F  # For 0 .. number of elements to remove from back
¨ #   Remove the last element


Clojure, 28 bytes

#(subvec % %2(-(count %)%3))


Well there is the built-in.

NewStack, 5 bytes

ᵢqᵢpᵢ


The symmetry is an amazing coincidence

The breakdown:

ᵢ      Append an input (the array).
qᵢ    Pop off an input's amount off the bottom.
pᵢ  Pop off an input's amount off the top.


F#, 39 bytes

let f(i:int[])s e=i.[s..(i.Length-1-e)]


Try it online!

Braingolf, 22 21 bytes

1-<1-[v$_R][v<$_R]v=;


Try it online!

Japt, 8 7 bytes

sVUl -W


Try it online!

Saved a byte thanks to ETHproductions

• Hmm, do you need the comma? – ETHproductions Jun 27 '17 at 0:55
• @ETHproductions A bit late, but the comma isn't needed; I keep forgetting that trick. Thanks! – Tom Jun 27 '17 at 9:12
• 6 bytes – Shaggy Jun 27 '17 at 16:08

LOGO, 33 bytes

[cascade ?2 "bf cascade ?3 "bl ?]


Try it on FMSLogo.

This is a template-list in LOGO, which is the equivalent of lambda (anonymous) function in many other languages.

Where:

• cascade outputs the result of applying a template repeatedly, in this case is ?2 times bf and ?3 times bl.
• bf and bl stands for ButFirst and ButLast respectively.
• ? is used to access parameters. So ?2 is the second parameter, ?3 is the third parameter, etc.

Test:

show apply [cascade ?2 "bf cascade ?3 "bl ?] [[1 2 3 4 5 6] 2 1]


print [3 4 5].

Clojure, 26 bytes

#(drop %2(drop-last %3 %))


V, 9 8 bytes

ÀñdñGÀñd


Try it online!

One byte saved thanks to Erik the golfer!

• You don't need last ñ. – Erik the Outgolfer Jun 27 '17 at 10:46
• The first four bytes can become Àd. – Kritixi Lithos Jun 27 '17 at 16:51
• @kritixilithos Unfortunately, that doesn't work for two reasons. 1) d by itself doesn't work. You'd need dd and 2) That doesn't work if the first input is 0. – DJMcMayhem Jun 27 '17 at 19:33

Perl 6, 18 bytes

{@^a[$^x..^*-$^y]}


@^a is the input array (first parameter), $^x is the number of elements to remove from the front (second parameter), and $^y is the number of elements to remove from the end (third parameter).

$^x ..^ *-$^y is a slicing subscript that selects a range of elements from the one with index $^x until one prior to the one with index *-$^y (* meaning "the length of the array" in a subscript context).

C++, 81 bytes

#include<vector>
int f(std::vector<int>&v,int x,int y){v={v.begin()+x,v.end()-y};}


Not as short as this use of a macro.

std::vector provides random access, and we simply create a new vector from the start point v.begin() + x, to the new end point v.end() - y elements.

Uses the iterator overload v.begin() and v.end() since it's the same length as the pointer one &v[0] and &*(v.end()), but more readable.

Function doesn't return an int like it promises, which is only a compiler warning.

8th, 33 bytes

Code

This code leaves resulting array on TOS

( a:rev swap -1 a:slice ) 2 times


Usage

ok> 2 1 [1,2,3,4,5] ( a:rev swap -1 a:slice ) 2 times .
[3,4]
ok> 5 0 [6,2,4,3,5,1,3] ( a:rev swap -1 a:slice ) 2 times .
[1,3]
ok>
ok> 0 0 [1,2] ( a:rev swap -1 a:slice ) 2 times .
[1,2]
ok>


Common Lisp, 41 bytes

(lambda(a x y)(subseq a x(-(length a)y)))


Try it online!

J, 17 bytes

(-@{:@[}.{.@[}.])


Usage:

   2 1 (-@{:@[}.{.@[}.]) 11 12 13 14 15 16
13 14 15


Husk, 3 bytes

↓↓_


Try it online!

Explanation

                                               |  1 [1,2,3,4,5,6] 2
_  -- negate y                               | -1 [1,2,3,4,5,6] 2
↓   -- drop y from end (negative argument)    |  [1,2,3,4,5] 2
↓    -- drop x from beginning                  |  [3,4,5]


Japt, 6 bytes

oW UsV


Try it

sVWnUl


Try it

C++, 50 bytes

[](auto&v,int x,int y){v={v.begin()+x,v.end()-y};}


Just construct a new container from the existing one. This requires v to be of a type with random-access iterators and constructible from an iterator pair - std::vector is ideally suited.

Demo

auto const trim =
[](auto&v,int x,int y){v={v.begin()+x,v.end()-y};}
;

#include<iostream>
#include<vector>
int main(int argc, char **argv)
{
if (argc < 4) {
std::cerr << "Usage: " << argv[0] << " X Y ELE...";
return 1;
}

auto const x = std::atoi(argv[1]);
auto const y = std::atoi(argv[2]);
std::vector<int> v;
v.reserve(argc - 3);
for (int i = 3;  i < argc;  ++i)
v.push_back(std::atoi(argv[i]));

auto const print_vector = [](auto v){for(auto i:v)std::cout<<i<<" ";std::cout<<'\n';};

print_vector(v);
trim(v, x, y);
print_vector(v);
}


Scala, 52 bytes

def f(a:Seq[Int],c:Int,d:Int)=a.dropRight(d).drop(c)


Try it online!

Lua, 94 bytes

n,s=loadstring('return{},'..p[1])()for x=1,#s do n[#n+1]=x>0+p[2]and#s>=x+p[3]and s[x]or _ end


Try it online!

If I used something that I couldn't please tell me.

Explanation

p = {...} -- (Header) It gets the 3 arguments (the list and the 2 numbers and stores in a table)

n,s=loadstring('return{},'..p[1])() -- is the same as (i) and (ii):
(i) n = {} -- initializes an empty table
(ii) s = loadstring('return '..p[1])() -- makes the string '{1,2,3}' become a table with 3 elements (1, 2 and 3)

for x=1,#s do -- from 1 to the number of elements in s, repeat:
n[#n+1] = x>0+p[2] and #s>=x+p[3] and s[x] or _
n[#n+1]   -- append to n
0+p[2]   -- convert "2" to the number 2
x>0+p[2] and #s>=x+p[3]   -- if we're past the start limit and before the end limit
and s[x]   -- then the element to be appended is the one with index x of the original list
or _   -- else append nil (the same as doing nothing)
end

print(table.concat(n, ", ")) -- (Footer) print the elements of n separated by commas


I used _ as nil just to be a little bit more clear (although this is not the point of these answers hahah). I could've used any variable that wasn't previously declared. _ doesn't have anything special in Lua.
And the comparisons are really messy because I tried to organize the math in a way that special characters would help me to use less space. ... and a>#b uses 1 more byte than ... and#b>a.

If you have any questions ask and I'll try to answer them.

Recursiva, 7 bytes

Zba-Lac


Try it online!

C (gcc), 16 bytes

The compiler instruction:

-DX(a,x,y)=(a+x)

As most would know, an array in C is just a known length and a pointer to the beginning of an array. Thus, we get an array with x members removed from the left and y members removed from the right by just moving the pointer to the array by +x.

Try it online!