-3
\$\begingroup\$

Challenge:

To take two points on the plane and figure out the position of the point that's a) on the line that connects them, and b) equally distant from each of the original points.

Example:

(0,0), (3,3) -> (1.5,1.5)

(-1,-1), (1,1) -> (0,0)

(-7,3), (2,-2) -> (-2.5,0.5)

(3, 1), (-5,9) -> (-1,5)

Rules:

  • The input and output can be a tuple, array, list, or any other type of sequence.
  • There must be two inputs, each representing a point.
  • The output has to support floating point numbers.

Shortest solution in bytes wins.

\$\endgroup\$
  • 6
    \$\begingroup\$ Some more test cases would probably be a good idea. Maybe ((-2,3), (5,4)), ((-7,-5),(-7,-5)) would be a good start. \$\endgroup\$ – Jonathan Allan Apr 20 '17 at 21:52
  • 2
    \$\begingroup\$ May input be two complex numbers? May output be a complex number? \$\endgroup\$ – Jonathan Allan Apr 20 '17 at 22:25
  • \$\begingroup\$ Is there any relevance of the "on a plane" part? Any two arbitrary points share infinitely many planes, and the desired point is shared by all of them. \$\endgroup\$ – Julian Wolf Apr 20 '17 at 22:29
  • 2
    \$\begingroup\$ @JulianWolf I take "point on a plane" to signify a 2d coordinate system. \$\endgroup\$ – Sparr Apr 20 '17 at 23:32
  • \$\begingroup\$ @Sparr: that makes sense, thanks. I guess my mind treats "on a plane" as being distinct from "on the plane". (This is probably due to me not talking to enough actual mathematicians.) \$\endgroup\$ – Julian Wolf Apr 20 '17 at 23:34

13 Answers 13

4
\$\begingroup\$

Jelly, 7 2 bytes

-5 bytes (ha ha, yes I am an idiot) thanks to FryAmTheEggman!

+H

Takes two lists each of length d and returns one list of length d where d is the dimensions of the Cartesian space (question is posed for 2 dimensions, but should work for any d).

Try it online!

How?

+H - Main link: a, b (lists of coordinates)
+  - add (vectorises across the dimensions)
 H - halve (vectorises across the result)
\$\endgroup\$
4
\$\begingroup\$

Mathematica, 4 bytes

Mean

Works on lists of vectors: for example, Mean[{{-7, 3}, {2, -2}}] returns {-5/2, 1/2}. (If the input consists of floating-point numbers, so will the output.)

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

Python 3, 39 35 bytes

lambda*x:[sum(a)/2for a in zip(*x)]

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ If you place an * before the first x (for 0 bytes) it will actually take multiple (and variable) arguments rather than the single (wrapped) list. \$\endgroup\$ – Jonathan Allan Apr 20 '17 at 22:53
  • \$\begingroup\$ @JonathanAllan Thanks! I didn't know that was possible, but it does look neater and more clean. \$\endgroup\$ – Adnan Apr 20 '17 at 23:11
1
\$\begingroup\$

Haskell, 22 19 bytes

zipWith$((/2).).(+)

Can be called with (zipWith$((/2).).(+)) (p1::[a]) (p2::[a]) where a is any type deriving Fractional. Works in arbitrary dimensions.

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

TI-BASIC, 8 bytes

.5(L₁+L₂

Takes input on List 1 and List 2. Works for all dimensions.

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

Julia 0.6, 13 bytes

(x->x/2)∘(+)
\$\endgroup\$
0
\$\begingroup\$

JavaScript , 30 bytes

valid for any number of dimensions

a=>b=>a.map((c,i)=>(c+b[i])/2)

Try it online!

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

R, 22 bytes

As an unnamed function that takes 2 vectors as parameters. Simply returns the mean of input vectors the same as a lot of answers.

function(a,b)((a+b)/2)

Try it online!

And 16 bytes using the pryr library

pryr::f((a+b)/2)
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 70 54 49 bytes

Thanks to @Julian Wolf for helping to save a bunch of bytes!

(defun f(a b)(mapcar(lambda(c d)(/(+ c d)2))a b))

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ car and cadr can shave a bunch of bytes off of those calls to nth. mapcar is also your friend. \$\endgroup\$ – Julian Wolf Apr 20 '17 at 22:42
  • \$\begingroup\$ @JulianWolf Thanks! I don't really know Lisp, so that was of a lot of help. \$\endgroup\$ – Steadybox Apr 20 '17 at 23:23
  • \$\begingroup\$ No problem! LISP is super duper awesome once you get the hang of it. The next step would be putting both operations into a single lambda—no need to call mapcar separately for addition and division. \$\endgroup\$ – Julian Wolf Apr 20 '17 at 23:32
0
\$\begingroup\$

Math.JS, 14 bytes

f(a,b)=(a+b)/2

Creates function f which takes 2 One Dimensional Matricies as input.

As expected, this just adds the two, then halfs the result.

Try it!

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

Python + NumPy, 18 bytes

lambda a,b:(a+b)/2

Works in two dimensions for points inputted as imaginary numbers, or (since OP specifies that inputs must be taken as sequences) in arbitrary dimensions for points inputted as NumPy arrays.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Thanks for the TIO link, @Riker! I really ought to get into the habit of adding those. \$\endgroup\$ – Julian Wolf Apr 21 '17 at 2:56
0
\$\begingroup\$

Python + NumPy, 17 bytes

lambda a:sum(a)/2

A slightly less boring version of the other python answer. Takes args as 1 numpy array, containing both pairs.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Does this not work in normal Python? \$\endgroup\$ – Cyoce May 8 '17 at 22:55
  • \$\begingroup\$ @Cyoce Python doesn't support division across a vector. \$\endgroup\$ – Rɪᴋᴇʀ May 9 '17 at 1:23
  • \$\begingroup\$ nvm. I see it now. \$\endgroup\$ – Cyoce May 9 '17 at 1:24
0
\$\begingroup\$

Julia, 13 bytes

a->b->(a+b)/2

Call with currying syntax, i.e. (a->b->(a+b)/2)([1,2])([3,4]).

\$\endgroup\$
  • \$\begingroup\$ Shoot, didn't see that you'd beat me to this one \$\endgroup\$ – Julian Wolf Apr 21 '17 at 2:58
  • \$\begingroup\$ @JulianWolf it's not a problem! Yours is actually a byte shorter, so you can definitely leave it up. I don't even have v0.6, so I think tha'ts pretty cool. \$\endgroup\$ – Rɪᴋᴇʀ Apr 21 '17 at 2:59
  • \$\begingroup\$ Yeah, I'm definitely leaving the composition solution up (they seem sufficiently different); there's no need for the two equivalent v0.5 solutions to stay though \$\endgroup\$ – Julian Wolf Apr 21 '17 at 3:02
  • \$\begingroup\$ Not familiar with Julia specifically, but could you save a byte with currying to a->b->(a+b)/2? \$\endgroup\$ – Cyoce May 8 '17 at 22:53
  • \$\begingroup\$ @Cyoce yes, thanks! \$\endgroup\$ – Rɪᴋᴇʀ May 9 '17 at 1:20

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.