32
\$\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\$
15
  • 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\$ Jun 26, 2017 at 16:40
  • 1
    \$\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, 2017 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\$ Jun 26, 2017 at 16:43
  • 1
    \$\begingroup\$ @Michthan Try installing the PPCG userscript \$\endgroup\$
    – Okx
    Jun 27, 2017 at 10:59
  • 2
    \$\begingroup\$ @Okx Nope, that's very buggy, I'd recommend adding a leaderboard. \$\endgroup\$ Jun 27, 2017 at 11:01

68 Answers 68

1
\$\begingroup\$

Pyth, 5 bytes

>E<QE

Try it here

Takes the arguments in the opposite order. < and > in Pyth trim based on argument order. For example, <Q5 will trim off all values in the input after the fifth one.

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

tcl, 19

lrange $L $x end-$y

where L is the array.

demo

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

CJam, 8 bytes

{_,@-<>}

Anonymous block that takes the inputs from the stack in the order x, y, array, and replaces them by the output array.

Try it online!

Explanation

Consider inputs 2, 1, [10 20 30 40 50 60].

{      }    e# Block
            e# STACK: 2, 1, [10 20 30 40 50 60]
 _          e# Duplicate
            e# STACK: 2, 1, [10 20 30 40 50 60], [10 20 30 40 50 60]
  ,         e# Length
            e# STACK: 2, 1, [10 20 30 40 50 60], 6
   @        e# Rotate
            e# STACK: 2, [10 20 30 40 50 60], 6, 1
    -       e# Subtract
            e# STACK: 2, [10 20 30 40 50 60], 5
     <      e# Slice before
            e# STACK: 2, [10 20 30 40 50]
      >     e# Slice after
            e# STACK: [30 40 50]
\$\endgroup\$
1
1
\$\begingroup\$

q/kdb, 12 bytes

Solution:

{(0-z)_y _x}

Example:

q){(0-z)_y _x}[1 2 3 4 5 6;2;1]
3 4 5
q){(0-z)_y _x}[6 2 4 3 5 1 3;5;0]
1 3
q){(0-z)_y _x}[1 2;0;0]
1 2

Explanation:

{          } / lambda function
          x  / input array
       y _   / drop y elements from .. (takes from start)
 (0-z)       / negative z ()
      _      / drop -z elements from ... (takes from end)
\$\endgroup\$
1
\$\begingroup\$

Racket, 33 bytes

(λ(a i j)(drop-right(drop a i)j))

It can be called like so:

((λ(a i j)(drop-right(drop a i)j)) '(1 2 3 4 5 6) 2 1)
\$\endgroup\$
2
  • \$\begingroup\$ Can I have a link to the language's website? \$\endgroup\$
    – Okx
    Jun 26, 2017 at 22:16
  • \$\begingroup\$ @Okx racket-lang.org \$\endgroup\$
    – user63250
    Jun 26, 2017 at 22:16
1
\$\begingroup\$

Pyth, 4 bytes

<E>E

Demonstration

Put the list last.

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

Java 8, 82 bytes

a->n->m->{int l=a.length-m-n,r[]=new int[l];System.arraycopy(a,n,r,0,l);return r;}

Try it here.

Alternative with same (82) byte-count using a loop:

(a,n,m)->{int l=a.length-m,r[]=new int[l-n],i=0;for(;n<l;r[i++]=a[n++]);return r;}

Try it here.

Explanation:

a->n->m->{                      // Method with integer-array and two integer parameters and integer-array return-type
  int l=a.length-m-n,           //  Length of the array minus the two integers
      r[]=new int[l];           //  Result integer-array
  System.arraycopy(a,n,r,0,l);  //  Java built-in to copy part of an array to another array
  return r;                     //  Return result-String
}                               // End of method

System.arraycopy:

arraycopy(Object src, int srcPos, Object dest, int destPos, int length):

The java.lang.System.arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument.

The components at positions srcPos through srcPos + length - 1 in the source array are copied into positions destPos through destPos + length - 1, respectively, of the destination array.

\$\endgroup\$
7
  • \$\begingroup\$ Can you save bytes by not using currying? \$\endgroup\$ Jun 26, 2017 at 15:14
  • \$\begingroup\$ @TheLethalCoder No, in this case not. (a,n,m)-> has the same byte-count as a->n->m->. Although you're right I could have just used a regular call instead of currying. I'm kinda used of using currying when I have two (or more) parameters.. I've already made the mistake of using currying when I have four parameters a few times.. \$\endgroup\$ Jun 26, 2017 at 16:49
  • \$\begingroup\$ Ahh you're right I miscounted the bytes and I've done that as well currying is definitely a go to now! \$\endgroup\$ Jun 26, 2017 at 17:24
  • \$\begingroup\$ No TIO link? -- \$\endgroup\$ Jun 26, 2017 at 18:48
  • 2
    \$\begingroup\$ Sorry, can't let that pass. I posted my own answer because... there's a built-in (ok, not exactly, but nearly)! :o \$\endgroup\$ Jun 27, 2017 at 15:58
1
\$\begingroup\$

Kona/K4, 12 bytes

{x _(-y)_ z}

Usage:

   {x _(-y)_ z}[2;1;"hello world"]
"llo worl"
\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

Jelly, 5 bytes

‘ṫṚ¥ƒ

Try it online!

Takes [x, y] on the left and the list on the right. Ties the existing Jelly answer, but I'm posting it anyways because:

  • it doesn't use full-program I/O
  • it feels like it should be four bytes
‘        Increment x and y.
    ƒ    With the list as the initial value of the accumulator, reduce [x, y] by:
 ṫ       remove the first (right - 1) elements from the accumulator,
  Ṛ¥     then reverse it.
\$\endgroup\$
1
\$\begingroup\$

Jellyfish, 19 bytes

p
vvj
 -0
 j
 0
j
0

Try it online!

Takes input as

y
array
x

via STDIN.

This program translates to:

p    (v   (j         (0),v   (-    (0,j         (0)),j         (0)))
print(drop(eval_input(0),drop(minus(0,eval_input(0)),eval_input(0)))
\$\endgroup\$
1
\$\begingroup\$

Julia, 18 bytes

a*b*c=a[1+b:end-c]

Try it online!

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

Excel, 32 bytes

=OFFSET(A1,B1,,COUNT(A:A)-B1-C1)

Link the Spreadsheet

The array is entered in column A starting at A1.

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

Vyxal, 3 bytes

N"i

Try it Online!

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

Go, 49 bytes, ints only

func f(t[]int,x,y int)[]int{return t[x:len(t)-y]}

Attempt This Online!

Go, 52 bytes, generic

func f[T any](t[]T,x,y int)[]T{return t[x:len(t)-y]}

Attempt This Online!

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

Thunno, \$5\log_{256}(96)\approx\$ 4.12 bytes

ZxsZy

Attempt This Online!

Takes x then the array then y

Explanation

Zx     # Remove the first x elements from the array
  s    # Swap so y is now on the top of the stack
   Zy  # Remove the last y elements from the array
       # Implicit output
\$\endgroup\$
2
  • \$\begingroup\$ Why is there no slice builtin -.- \$\endgroup\$
    – noodle man
    Jan 28, 2023 at 14:08
  • \$\begingroup\$ @Jacob I forgot to add that... :( \$\endgroup\$
    – The Thonnu
    Jan 28, 2023 at 17:38
1
\$\begingroup\$

Elm, 44 bytes

h x=List.reverse<<List.drop x
f x y=h y<<h x

Try it on Ellie

\$\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\$
3
  • 1
    \$\begingroup\$ Hmm, do you need the comma? \$\endgroup\$ Jun 27, 2017 at 0:55
  • \$\begingroup\$ @ETHproductions A bit late, but the comma isn't needed; I keep forgetting that trick. Thanks! \$\endgroup\$
    – Tom
    Jun 27, 2017 at 9:12
  • \$\begingroup\$ 6 bytes \$\endgroup\$
    – Shaggy
    Jun 27, 2017 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\$
3
  • \$\begingroup\$ You don't need last ñ. \$\endgroup\$ Jun 27, 2017 at 10:46
  • \$\begingroup\$ The first four bytes can become Àd. \$\endgroup\$
    – user41805
    Jun 27, 2017 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, 2017 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\$

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.