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

16
\$\begingroup\$

Haskell, 55 39 33 29 bytes

Saved 16 bytes thanks to Laikoni

Saved 6 more bytes thanks to Laikoni

Saved 4 more bytes thanks to Laikoni

Am sure this could be improved, but as a beginner, gave it my best shot.

r=(reverse.).drop
a#b=r b.r a

Usage

(5#0) [6,5,4,3,2,1,3]

Try it online!

\$\endgroup\$
  • 5
    \$\begingroup\$ Welcome to PPCG and Haskell golfing in particular! The objective is to use as few bytes as possible, so you can for example remove most of the spaces and shorten xs. \$\endgroup\$ – Laikoni Jun 26 '17 at 14:24
  • \$\begingroup\$ @Laikoni Ah, thanks! Edited, can't see myself going any shorter without an anonymous function and using applicative for functions (not sure how that works). \$\endgroup\$ – Henry Jun 26 '17 at 14:28
  • \$\begingroup\$ Looking good now! :) If you change f x a b to f a b x, you can simply drop the x: f a b=reverse.drop b.reverse.drop a. \$\endgroup\$ – Laikoni Jun 26 '17 at 14:32
  • 1
    \$\begingroup\$ @Laikoni Wow, interesting infix trick. Thanks again! I was able to shorten it to 33 bytes, but trying to do a#b=let r=reverse in r.drop b.r.drop a is 38 bytes. Or are we allowed to have a function declared outside this one? \$\endgroup\$ – Henry Jun 26 '17 at 14:42
  • 1
    \$\begingroup\$ @Laikoni Thanks for the introduction, very helpful. Just found this site today, but definitely look forward to playing around here some more! \$\endgroup\$ – Henry Jun 26 '17 at 14:55
7
\$\begingroup\$

Octave, 20 bytes

@(a,x,y)a(x+1:end-y)

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Mathematica, 17 bytes

#[[#2+1;;-#3-1]]&

input

[{1, 2, 3, 4, 5, 6}, 2, 1]

\$\endgroup\$
  • \$\begingroup\$ Nice use of ;;! I managed to tie you with Drop@##2~Drop~-#& (if we take the input in a weird order like 1, {1,2,3,4,5,6}, 2), but no better. \$\endgroup\$ – Greg Martin Jun 26 '17 at 17:30
6
\$\begingroup\$

Python, 28 26 bytes

-2 bytes thanks to @Rod

lambda a,n,m:a[n:len(a)-m]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ save 6... lambda a,n,m:a[n:~m] \$\endgroup\$ – Aaron Jun 26 '17 at 16:48
  • \$\begingroup\$ @Aaron this removes one item too much. \$\endgroup\$ – ovs Jun 26 '17 at 16:52
  • \$\begingroup\$ my bad.. It's a common trick I sometimes use, and didn't fully check against the requirements of the challenge.. \$\endgroup\$ – Aaron Jun 26 '17 at 17:00
  • \$\begingroup\$ @Aaron the slice has a higher operator precedence than the + and is therefore applied to [0]. You would need brackets: (a+[0])[n:~m]. \$\endgroup\$ – ovs Jun 26 '17 at 17:07
  • \$\begingroup\$ yah, realized that later.. I'm trying to make my idea work \$\endgroup\$ – Aaron Jun 26 '17 at 17:08
6
\$\begingroup\$

C# (.NET Core), 55 54 bytes

using System.Linq;(a,x,y)=>a.Skip(x).Take(a.Count-x-y)

Try it online!

Uses a List<int> as input.

  • 1 byte saved thanks to TheLethalCoder!
\$\endgroup\$
  • 1
    \$\begingroup\$ I was just about to answer this +1. However, you can save a byte by taking a List as input so you can use Count instead of Length. \$\endgroup\$ – TheLethalCoder Jun 26 '17 at 14:45
  • \$\begingroup\$ I came up with a solution using Where that is only slightly longer than this way that I'm quite happy with as well :) \$\endgroup\$ – TheLethalCoder Jun 26 '17 at 15:09
  • \$\begingroup\$ You don't need to add using System.Linq; to the byte count :) \$\endgroup\$ – Stefan Jun 27 '17 at 13:19
  • \$\begingroup\$ @Stefan I need to count every using I add in my answer, and the methods Skip and Take need that using. \$\endgroup\$ – Charlie Jun 27 '17 at 13:22
  • \$\begingroup\$ hm. okay. On some other challenge I was told that those usings where not necessary. \$\endgroup\$ – Stefan Jun 27 '17 at 13:24
5
\$\begingroup\$

Perl 5, 21 bytes

19 bytes of code + -ap flags.

$_="@F[<>..$#F-<>]"

Try it online!

Uses -a to autosplit the input inside @F, then only keep a slice of it according to the other inputs: from index <> (second input) to index $#F-<> (size of the array minus third input). And $_ is implicitly printed thanks to -p flag.

\$\endgroup\$
5
\$\begingroup\$

Rust, 29 bytes

|n,i,j|&n[i..<[_]>::len(n)-j]

Call it as follows:

let a = &[1, 2, 3, 4, 5, 6];
let f = |n,i,j|&n[i..<[_]>::len(n)-j];
f(a, 2, 1)

I had a lot of fun fighting with the borrow checker figuring out what the shortest approach was in order to have it infer the lifetime of a returned slice. Its behavior around closures is somewhat erratic, as it will infer the lifetimes, but only if you do not actually declare the parameter as a reference type. Unfortunately this conflicts with being required to define the argument type in the signature as the n.len method call needs to know the type it's operating on.

Other approaches I tried working around this issue:

fn f<T>(n:&[T],i:usize,j:usize)->&[T]{&n[i..n.len()-j]}     // full function, elided lifetimes
let f:for<'a>fn(&'a[_],_,_)->&'a[_]=|n,i,j|&n[i..n.len()-j] // type annotation only for lifetimes. Currently in beta.
|n:&[_],i,j|n[i..n.len()-j].to_vec()                        // returns an owned value
|n,i,j|&(n as&[_])[i..(n as&[_]).len()-j]                   // casts to determine the type
|n,i,j|&(n:&[_])[i..n.len()-j]                              // type ascription (unstable feature)
|n,i,j|{let b:&[_]=n;&b[i..b.len()-j]}                      // re-assignment to declare the type
\$\endgroup\$
4
\$\begingroup\$

Neim, 3 bytes

𝕘₃𝕙

Try it here

Thanks to Okx for encouraging me to do this...:)

\$\endgroup\$
4
\$\begingroup\$

C#, 62 bytes

using System.Linq;(l,x,y)=>l.Where((n,i)=>i>=x&i<=l.Count-y-1)

Takes a List<int> as input and returns an IEnumerable<int>.


This also works for 64 bytes:

using System.Linq;(l,x,y)=>l.Skip(x).Reverse().Skip(y).Reverse()
\$\endgroup\$
4
\$\begingroup\$

TIS-100, 413 405 Bytes

472 cycles, 5 nodes, 35 lines of code

m4,6
@0
MOV 0 ANY
S:MOV UP ACC
JEZ A
MOV ACC ANY
JMP S
A:MOV RIGHT ACC
L:JEZ B
MOV DOWN NIL
SUB 1
JMP L
B:MOV 0 RIGHT
MOV RIGHT NIL
@1
MOV RIGHT LEFT
MOV LEFT DOWN
MOV RIGHT DOWN
MOV DOWN LEFT
@2
MOV UP ACC
MOV UP LEFT
MOV ACC LEFT
@4
MOV 0 RIGHT
MOV UP NIL
S:MOV LEFT ACC
JEZ A
MOV ACC RIGHT
JMP S
A:MOV UP ACC
L:JEZ B
MOV RIGHT NIL
SUB 1
JMP L
B:MOV 0 UP
K:MOV RIGHT ACC
MOV ACC DOWN
JNZ K
@7
MOV UP ANY

The m4,6 at the top is not part of the code, but signals the placement of the memory modules.

enter image description here

Play this level yourself by pasting this into the game:


function get_name()
    return "ARRAY TRIMMER"
end
function get_description()
    return { "RECIEVE AN ARRAY FROM IN.A", "RECIEVE TWO VALUES A THEN B FROM IN.T", "REMOVE THE FIRST A TERMS AND LAST B TERMS FROM IN.A", "ARRAYS ARE 0 TERMINATED" }
end

function get_streams()
    input = {}
    trim = {}
    output = {}

  arrayLengths = {}

    a = math.random(1,5) - 3

    b = math.random(1,7) - 4

    arrayLengths[1] = 9+a
    arrayLengths[2] = 9+b
    arrayLengths[3] = 8-a
    arrayLengths[4] = 9-b

    s = 0

    trimIndex = 1

  for i = 1,4 do
      for k = 1,arrayLengths[i] do
          x = math.random(1,999)
      input[k+s] = x
            output[k+s] = x
        end

        input[s + arrayLengths[i] + 1]= 0
        output[s + arrayLengths[i] + 1]= 0

        a = math.random(0,3)
        b = math.random(0,arrayLengths[i]-a)

        trim[trimIndex] = a
        trim[trimIndex+1] = b

        trimIndex = trimIndex + 2

    s = s + arrayLengths[i] + 1
    end

    s = 1
    trimIndex = 1

    for i = 1,4 do

      for i = s,s+trim[trimIndex]-1 do
          output[i]=-99
        end

        for i = s + arrayLengths[i] - trim[trimIndex+1], s + arrayLengths[i]-1 do
      output[i]=-99
        end

  trimIndex = trimIndex +2
  s = s + arrayLengths[i] + 1
    end

    trimmedOut = {}
    for i = 1,39 do
            if(output[i] ~= -99) then
                    table.insert(trimmedOut, output[i])
            end
    end

    return {
        { STREAM_INPUT, "IN.A", 0, input },
        { STREAM_INPUT, "IN.T", 2, trim },
        { STREAM_OUTPUT, "OUT.A", 1, trimmedOut },
    }
end
function get_layout()
    return {
        TILE_COMPUTE,   TILE_COMPUTE,   TILE_COMPUTE,   TILE_COMPUTE,
        TILE_MEMORY,    TILE_COMPUTE,    TILE_MEMORY,   TILE_COMPUTE,
        TILE_COMPUTE,   TILE_COMPUTE,   TILE_COMPUTE,   TILE_COMPUTE,
    }
end

So I suppose this also counts as a lua answer...

\$\endgroup\$
  • \$\begingroup\$ You can now try it online! Note: I had to do a clever, and use the top of the code file as one source of input, since TIO currently only provides a single input file. \$\endgroup\$ – Phlarx May 2 '18 at 20:17
4
\$\begingroup\$

MATL, 6 bytes

QJi-h)

Try it online!

Input is given as 1) number of elements to trim from the start; 2) number of elements to trim from the end; 3) array. Explanation

Q   % Implicit input (1). Increment by 1, since MATL indexing is 1-based.
Ji- % Complex 1i minus real input (2). In MATL, the end of the array is given by `1i`.
h   % Concatenate indices to get range-based indexing 1+(1):end-(2).
)   % Index into (implicitly taken) input array. Implicit display.
\$\endgroup\$
4
\$\begingroup\$

Java (OpenJDK 8), 32 bytes

(l,i,j)->l.subList(i,l.size()-j)

Try it online!

If we really restrict to arrays, then it's 53 bytes:

(a,i,j)->java.util.Arrays.copyOfRange(a,i,a.length-j)

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 27 bytes

(a,n,m)=>a.slice(n,-m||1/m)

A negative second parameter to slice stops slicing m from the end, however when m is zero we have to pass a placeholder (Infinity here, although (a,n,m,o)=>a.slice(n,-m||o) also works).

\$\endgroup\$
3
\$\begingroup\$

R, 32 31 30 bytes

-1 byte thanks to Rift

-1 byte thanks to Jarko Dubbeldam

pryr::f(n[(1+l):(sum(n|1)-r)])

Evaluates to an anonymous function:

function (l, n, r) 
    n[(1 + l):(sum(n|1) - r)]

1+l is necessary since R has 1-based indexing. sum(n|1) is equivalent to length(n) but it's a byte shorter.

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ saving 1 byte with pryr::f(n[(1+l):(length(n)-r)]) \$\endgroup\$ – Rift Jun 26 '17 at 15:30
  • 1
    \$\begingroup\$ Sum(n|1) is shorter than length(n) \$\endgroup\$ – JAD Jun 26 '17 at 18:07
  • \$\begingroup\$ @JarkoDubbeldam excellent, thank you. \$\endgroup\$ – Giuseppe Jun 26 '17 at 18:15
3
\$\begingroup\$

MATL, 10 bytes

tniQwi-&:)

Try it online!

Explanation:

It's a bit long for just 11 bytes, but I'm writing it out in detail, to learn it myself too.

---- Input ----
[1 2 3 4 5 6]
2
1
----- Code ----
           % Implicit first input
t          % Duplicate input.
           % Stack: [1 2 3 4 5 6], [1 2 3 4 5 6]
 n         % Number of elements
           % Stack: [1 2 3 4 5 6], 6
  i        % Second input
           % Stack: [1 2 3 4 5 6], 6, 2
   Q       % Increment: [1 2 3 4 5 6], 6, 3
    w      % Swap last two elements
           % Stack: [1 2 3 4 5 6], 3, 6
     i     % Third input
           % Stack: [1 2 3 4 5 6], 3, 6, 1
      -    % Subtract
           % Stack: [1 2 3 4 5 6], 3, 5
       &:  % Range with two input arguments, [3 4 5]
           % Stack: [1 2 3 4 5 6], [3 4 5]
         ) % Use as index
           % Stack: [3 4 5]
           % Implicit display
\$\endgroup\$
  • \$\begingroup\$ You forgot about end-based indexing ;) \$\endgroup\$ – Sanchises Jun 27 '17 at 8:52
  • \$\begingroup\$ (still, have an upvote - I believe this is well golfed and explained considering the method you used) \$\endgroup\$ – Sanchises Jun 27 '17 at 9:04
  • \$\begingroup\$ Nope, I didn't forget it! I tried, but I didn't figure out how to make it work, (and I really tried). I concluded that it was impossible to subtract something from J, when used like this. I suspected I was wrong, I just couldn't figure it out for the life of me... Thanks for linking to your answer, I'm very much a MATL novice... \$\endgroup\$ – Stewie Griffin Jun 27 '17 at 9:09
  • \$\begingroup\$ Don't worry, I'm also very much learning still - e.g., the order of the inputs to ) and more notoriously ( shiver... \$\endgroup\$ – Sanchises Jun 27 '17 at 9:18
  • \$\begingroup\$ @Sanchises Very late comment, but I'm glad it's not just me that finds the input order to ( confusing. :) I've taken to reciting "ddi" (= "destination, data, indices" from the manual) every time, and still get it wrong sometimes. \$\endgroup\$ – sundar Jul 23 '18 at 21:38
3
\$\begingroup\$

C++, 96 95 bytes

Thanks to @Tas for saving a byte!

#import<list>
int f(std::list<int>&l,int x,int y){for(l.resize(l.size()-y);x--;)l.pop_front();}

Try it online!

C++ (MinGW), 91 bytes

#import<list>
f(std::list<int>&l,int x,int y){for(l.resize(l.size()-y);x--;)l.pop_front();}
\$\endgroup\$
  • \$\begingroup\$ Did you mean #include<list>? You could shave a byte by having int f. Compilers will allow a function to not return, but they'll warn against it \$\endgroup\$ – Tas Jun 28 '17 at 2:23
  • \$\begingroup\$ Yeah, thanks, int f will work on most compilers, I'll edit that in. On MinGW, even completely omitting the type of the function works. And yes, #include<list> would be a standard-compliant way to include the header, but #import<list> should work at least on GCC, MinGW and MSVC, so it should be fine too. \$\endgroup\$ – Steadybox Jun 28 '17 at 2:51
2
\$\begingroup\$

APL (Dyalog), 8 7 bytes

⌽⎕↓⌽⎕↓⎕

Try it online!

This takes the array as the first input, followed by the two numbers separately.

Explanation

⎕            from the input array
⎕↓           drop the first input elements
⌽            reverse the array
⎕↓           drop first input elements
⌽            reverse again to go back to the original array
\$\endgroup\$
  • \$\begingroup\$ Alternative 7 byte solution: ⎕↓⎕↓⍨-⎕ \$\endgroup\$ – Adám Jun 26 '17 at 22:54
2
\$\begingroup\$

PHP>=7.1, 59 bytes

<?[$a,$b,$e]=$_GET;print_r(array_slice($a,$b,$e?-$e:NULL));

PHP Sandbox Online

\$\endgroup\$
2
\$\begingroup\$

Brain-Flak, 60 bytes

(()()){({}<{({}<{}>[()])}{}([]){{}({}<>)<>([])}{}<>>[()])}{}

Try it online!

Input is in this format:

x

a
r
r
a
y

y

Where x is the number to take from the front, y is the number to take from the back, and the array is just however many numbers you want, separated by newlines. Here are my first two (longer) attempts:

({}<>)<>{({}<{}>[()])}([])<>({}<><{{}({}<>)<>([])}{}><>){({}<{}>[()])}{}([]){{}({}<>)<>([])}<>{}
{({}<{}>[()])}{}([]){{}({}<>)<>([])}{}<>{({}<{}>[()])}{}([]){{}({}<>)<>([])}<>

And here is an explanation:

#Two times:
(()()){({}<

    #Remove *n* numbers from the top of the stack
    {({}<{}>[()])}{}

    #Reverse the whole stack
    ([]){{}({}<>)<>([])}{}<>

>)[()]}{}
\$\endgroup\$
  • 1
    \$\begingroup\$ Nice to see a turing tarpit solution every now and then. \$\endgroup\$ – Okx Jun 26 '17 at 15:53
2
\$\begingroup\$

APL (Dyalog), 5 bytes

(⌽↓)/

Try it online!


Input format is y x A

Explanation

/ is Reduce, which inserts the function to the left between each pair of elements of the argument

(⌽↓) is a function train equivalent to {⌽⍺↓⍵}, which removes the first elements of the array and then reverses the array. ( is the left argument and is the right argument)

Thus, (⌽↓)/y x A is equivalent to ⌽y↓⌽x↓A, which is what is needed.

\$\endgroup\$
2
\$\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\$
  • \$\begingroup\$ Can you save bytes by not using currying? \$\endgroup\$ – TheLethalCoder Jun 26 '17 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\$ – Kevin Cruijssen Jun 26 '17 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\$ – TheLethalCoder Jun 26 '17 at 17:24
  • \$\begingroup\$ No TIO link? -- \$\endgroup\$ – totallyhuman Jun 26 '17 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\$ – Olivier Grégoire Jun 27 '17 at 15:58
2
\$\begingroup\$

C++, 50 48 46 bytes

#define f(a,x,y)decltype(a)(&a[x],&*a.end()-y)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Kotlin, 30 bytes

{a,s,e->a.drop(s).dropLast(e)}

Try it online!

Takes List<Int> as input and drops from begin and then from end.

\$\endgroup\$
  • 1
    \$\begingroup\$ I have not access to try it online. Can you add a caller code? how compile lambda without type definitions in Kotlin? Thanks. \$\endgroup\$ – mazzy Jul 25 '18 at 7:29
  • 1
    \$\begingroup\$ @mazzy it could probably be a hack, but you can specify types in variable type definition as val f: (List<Int>, Int, Int) -> List<Int> \$\endgroup\$ – YGolybev Jul 25 '18 at 7:49
  • \$\begingroup\$ Got it! Nice. I don't know if this is valid in CodeGolf. \$\endgroup\$ – mazzy Jul 25 '18 at 7:57
2
\$\begingroup\$

Brachylog, 11 10 bytes

kb₍B&t;Bk₍

Try it online!

Takes input as [x, A, y] where A is the array to trim.

(-1 byte thanks to @Fatalize.)

\$\endgroup\$
  • \$\begingroup\$ You can shorten it by 1 byte as such: kb₍B&t;Bk₍. , does append (see the result of this partial program), it doesn't act like . Also do not try to copy things from old (2016-early 2017) Brachylog answers because it was the first version of the language, and programs are not retrocompatible (in particular, , in Brachylog v1 is now in Brachylog v2) \$\endgroup\$ – Fatalize Jul 25 '18 at 6:38
  • \$\begingroup\$ @Fatalize Thanks, updated. So , did append in the previous version, but it just didn't matter in this case because there was a t after it anyway - lucky coincidence. And yeah, I realized the version differences after I posted this, I was still figuring things out and bumbling around at this stage. :) \$\endgroup\$ – sundar Jul 25 '18 at 20:45
1
\$\begingroup\$

Dyalog APL, 16 bytes

{(⍵↓⍨⊃⍺)↓⍨-⍺[2]}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ why was this downvoted? \$\endgroup\$ – Uriel Jun 26 '17 at 22:55
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
\$\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\$
  • \$\begingroup\$ Can I have a link to the language's website? \$\endgroup\$ – Okx Jun 26 '17 at 22:16
  • \$\begingroup\$ @Okx racket-lang.org \$\endgroup\$ – Ultimate Hawk Jun 26 '17 at 22:16

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.