27
\$\begingroup\$

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]
\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Cody Gray Jun 26 '17 at 16:40
  • \$\begingroup\$ @CodyGray Removing values from the array is what it should look like, but not necessarily what goes on behind the scenes. \$\endgroup\$ – Okx Jun 26 '17 at 16:42
  • 4
    \$\begingroup\$ What do you mean by "look like"? Arrays don't have a look – it is all behind the scenes! \$\endgroup\$ – Cody Gray Jun 26 '17 at 16:43
  • 1
    \$\begingroup\$ @Michthan Try installing the PPCG userscript \$\endgroup\$ – Okx Jun 27 '17 at 10:59
  • 2
    \$\begingroup\$ @Okx Nope, that's very buggy, I'd recommend adding a leaderboard. \$\endgroup\$ – Erik the Outgolfer Jun 27 '17 at 11:01

56 Answers 56

1
\$\begingroup\$

Pyth, 4 bytes

<E>E

Demonstration

Put the list last.

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

Ruby, 17 bytes

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

Try it online!

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

Kona/K4, 12 bytes

{x _(-y)_ z}

Usage:

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

K (oK), 10 bytes

{(-z)_y_x}

Try it online!

Port of my q/kdb+ answer...

\$\endgroup\$
0
\$\begingroup\$

Jelly, 5 bytes

Ṗ¡Ḋ⁴¡

Try it online!

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

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

Clojure, 28 bytes

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

Well there is the built-in.

\$\endgroup\$
0
\$\begingroup\$

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

F#, 39 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Braingolf, 22 21 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 8 7 bytes

sVUl -W

Try it online!

Saved a byte thanks to ETHproductions

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

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

\$\endgroup\$
0
\$\begingroup\$

Clojure, 26 bytes

#(drop %2(drop-last %3 %))
\$\endgroup\$
0
\$\begingroup\$

V, 9 8 bytes

ÀñdñGÀñd

Try it online!

One byte saved thanks to Erik the golfer!

\$\endgroup\$
  • \$\begingroup\$ You don't need last ñ. \$\endgroup\$ – Erik the Outgolfer Jun 27 '17 at 10:46
  • \$\begingroup\$ The first four bytes can become Àd. \$\endgroup\$ – Kritixi Lithos Jun 27 '17 at 16:51
  • \$\begingroup\$ @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. \$\endgroup\$ – DJMcMayhem Jun 27 '17 at 19:33
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

Common Lisp, 41 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

J, 17 bytes

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

Usage:

   2 1 (-@{:@[}.{.@[}.]) 11 12 13 14 15 16
13 14 15
\$\endgroup\$
0
\$\begingroup\$

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

Japt, 6 bytes

oW UsV

Try it

sVWnUl

Try it

\$\endgroup\$
0
\$\begingroup\$

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

Scala, 52 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Recursiva, 7 bytes

Zba-Lac

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$

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.