0
\$\begingroup\$

For 2 arrays each with X elements, what is the SHORTEST code to subtract every element in the second from every element in the first, 1 to 1

so, for example:

A1 = [10,11,12]

A2 = [1,2,3]

A1 "-" A2 should == [9,9,9]

\$\endgroup\$
4
  • 14
    \$\begingroup\$ Come on. Code golf should have enough complexity for the competitors to exercise some creativity. Really. \$\endgroup\$ Apr 13 '11 at 18:47
  • \$\begingroup\$ dmckee: Exactly my thoughts. This one is way too trivial to make a good task (see the R solution, for example). Now if only people could stop by the Sandbox on meta before posting a task ... \$\endgroup\$
    – Joey
    Apr 13 '11 at 19:23
  • \$\begingroup\$ Though I agree that this question is lousy for golf I find it very unfair that it has at the same time a negative vote count but 5 questions of +14. \$\endgroup\$
    – Eelvex
    Apr 14 '11 at 14:35
  • 2
    \$\begingroup\$ GP, GAP, J, Fortran, Maxima, ... solution: "A1 - A2" \$\endgroup\$
    – Eelvex
    Apr 14 '11 at 14:39

23 Answers 23

12
\$\begingroup\$

R, 3 chars

(following Chris' counting method that includes the variable names in the count)

a-b

a and b can be defined as follows:

a <- c(10,11,12)
b <- c(1,2,3)

(c is the array construction function in R)

\$\endgroup\$
2
  • \$\begingroup\$ Is this the normal way for defining arrays in R? and if not, what would the code be, if you have a and b already defined. \$\endgroup\$
    – Samer Buna
    Apr 13 '11 at 18:23
  • \$\begingroup\$ @Samer, yes that's the normal way. The code would be exactly the same. In fact, you can even do c(10,11,12)-c(1,2,3). So theoretically it's only a single character. \$\endgroup\$
    – Joey
    Apr 13 '11 at 19:03
7
\$\begingroup\$

J, 1 char

I will answer too because finally I don't have to use 2 bytes worth of enclosing brackets for J. :)

   -

   10 11 12 - 1 2 3
9 9 9
\$\endgroup\$
5
\$\begingroup\$

Scheme, 11 chars

Assume the lists are named a and b:

(map - a b)

Example:

(map - '(10 11 12) '(1 2 3))  ; => (9 9 9)
\$\endgroup\$
4
\$\begingroup\$

In Haskell (10):

zipWith(-)

will do the job. Please note, that the lists will be truncated, if their length isn't equal.

\$\endgroup\$
4
  • \$\begingroup\$ Well, the task states that they are of equal length x, so I guess that can be neglected. \$\endgroup\$
    – Joey
    Apr 13 '11 at 18:22
  • \$\begingroup\$ Lists? Doesn't Haskell have Arrays? \$\endgroup\$ Apr 21 '11 at 2:56
  • \$\begingroup\$ @user unknown: They exists, but aren't used very often. In this case, using arrays would blow up my solution by about 30 chars. \$\endgroup\$
    – FUZxxl
    Apr 21 '11 at 10:49
  • \$\begingroup\$ :) That's why I'm asking. :) \$\endgroup\$ Apr 21 '11 at 14:03
4
\$\begingroup\$
>>> class L(list):
...  def __sub__(self, other):
...   return map(int.__sub__,self, other)
... 
>>> a=L([10,11,12])
>>> b=L([1,2,3])
>>> a-b
[9, 9, 9]
>>> 
\$\endgroup\$
4
\$\begingroup\$

ES6, 20

a.map((x,i)=>x-b[i])
\$\endgroup\$
3
\$\begingroup\$

In Python (24):

map(lambda a,b:a-b,a,b)

Smaller (21):

map(int.__sub__,a,b)
\$\endgroup\$
2
\$\begingroup\$

Groovy:

List a = [1,2,3]
List b=[2,7,9]
[0,1,2].each { println(a[it]-b[it]) }
\$\endgroup\$
2
\$\begingroup\$

Scala, 51 chars

Array(10,11,12) zip Array(1,2,3)map(x=>x._1-x._2)

(but includes initialisation, Array-Declaration)

(10 to 12).zip (1 to 3)map(x=>x._1-x._2)

is a bit shorter, but produces an Vector.

a zip b.map(x=>x._1-x._2)

is even shorter, but of course I could do

a zip b.map f

then. Or just

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

CJam, 10 bytes

CJam is newer than this challenge, but I thought it's funny how uncompetitive it is for once:

l~]z{~-}%p

This is an STDIN to STDOUT program, reading input like [10 9 8] [1 2 3]. I know everyone else is answering in snippets, but that wouldn't really be shorter here, because you'd have to replace ] by something like a\a\+.

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

TI-BASIC, 5 bytes

L1-L2

Input lists are stored in the L1 and L2 tokens. (TI-BASIC doesn't have STDIN or equivalent.)
Answer is stored in Ans.

Example:

{10,11,12→L1
      {10 11 12}
{1,2,3→L2
         {1 2 3}
L1-L2
         {9 9 9}
\$\endgroup\$
0
\$\begingroup\$

Not counting USE: syntax math sequences ; ! because it's not needed (on the REPL).

Factor: [ - ] 2map

\$\endgroup\$
1
  • 1
    \$\begingroup\$ And... what language should that be? \$\endgroup\$
    – FUZxxl
    Apr 21 '11 at 10:49
0
\$\begingroup\$

Not the shortest, but this is what I came up with in javascript...

t=0;
ar=[];
for(i=0;i<ar.length;i++){
   t=a[i]-b[i];
   ar.push(temp);
};

Input: a = [10,11,12], b = [1,2,3]
Output: [9,9,9]

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can shorten your code by removing unnecessary whitespace, using one-character variable names, and dropping the var keyword when creating a variable. \$\endgroup\$
    – ProgramFOX
    Feb 1 '15 at 9:05
  • 1
    \$\begingroup\$ You're welcome! Actually, you don't need newlines and indentation in JavaScript, so you can save some more characters if you remove some whitespace and if you use c instead of ar. Also, please include the character count in your answer. \$\endgroup\$
    – ProgramFOX
    Feb 2 '15 at 16:57
0
\$\begingroup\$

8th, 10 bytes

Code

' n:- a:op

Explanation

a:op takes two arrays and invokes the word n:- to the corresponding elements of "a1" and "a2", producing a new array "a3".

Example

ok> [10,11,12] [1,2,3] ' n:- a:op .
[9,9,9]
\$\endgroup\$
2
0
\$\begingroup\$

Java 8, 45 bytes

a->b->{for(int i=0;i<a.length;a[i]-=b[i++]);}

Modifies the first input-array instead of returning a new one to save bytes.

Explanation:

Try it here.

a->b->{            // Method with two integer-array parameters and no return-type
  for(int i=0;     //  Start at index 0
      i<a.length;  //  Loop over the input arrays by index
    a[i]-=b[i++]   //   Modify array `a` by subtracting the values of `b` on the same index
  );               //  End of loop
}                  // End of method
\$\endgroup\$
0
\$\begingroup\$

Java 8, 29 bytes

I got creative with I/O types for this. It's a lambda (curried) from an ordered IntStream (A1) to a lambda from a mutable List<Integer> (implementing remove(int), e.g. ArrayList) (A2) to IntStream. Assign to Function<IntStream, Function<List<Integer>, IntStream>>.

s->l->s.map(n->n-l.remove(0))

Try It Online

Yeah, that's right; I'm using stateful lambdas. I could lose 4 byes by taking A2 as a Stack and using pop, but I'm hesitant to stray that far from the List interface.

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

Pyt, 3 bytes

←←-

Takes arrays as input; alternatively, if the arrays are already on the stack, with the second one on the top, it's one character:

-

Explanation:

←        Get first array
 ←       Get second array
  -      Get elementwise difference (as an array)

Try it online!

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

Funky, 27 bytes

a=>b=>a::map((n i)=>n-b[i])

Try it online!

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

Jelly, 1 byte

_

Try it online!

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

Add++, 7 bytes

L*,BcB_

Try it online!

Auto vectorisation is so boring.

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

Zsh, 27 bytes

That's right, you can zip arrays in your shell scripts!

Input: arrays a and b
Output: stdout

for x y (${a:^b}) <<<$[x-y]

Try it online! (It takes more code to read stdin into arrays than it does to pairwise subtract!)

If the output must be another array, either 27 or 29 bytes.

for x y (${a:^b}) c+=$[x-y]    # if c was declared as an array
for x y (${a:^b}) c+=($[x-y])  # if c was not declared
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 13 bytes

ARYOP 1,A,A,B

Inputs: A, B
Output: A

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

Japt, 3 bytes

í-V

Try it

í-V     :Implicit input of arrays U & V
í V     :Interleave both
 -      :Reduce each pair by subtraction
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.