Program the function distance(p1, p2) which returns the distance between the points p1 and p2 in n-dimensional space. p1 and p2 will be given as arrays.

Your program should work for all lengths of arrays, and should return -1 if the arrays aren't of the same length or if both arrays are empty sets.

If you don't know how to measure the distance between two points, go here:
http://mathworld.wolfram.com/Distance.html

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def distance(p1, p2):
	

Solutions:
def distance(p1, p2):
	return sum(((a - b) ** 2 for (a, b) in zip(p1, p2))) ** 0.5 if len(p1) == len(p2) > 0 else -1


def distance(p1, p2):
	from functools import reduce
	from math import hypot
	if not p1 or len(p1) != len(p2):
		return -1
	return reduce(hypot, (x - y for (x, y) in zip(p1, p2)))


def distance(p1, p2):
	if not (p1 or p2) or len(p1) != len(p2):
		return -1
	return sum(((y - x) ** 2 for (x, y) in zip(p1, p2))) ** 0.5


distance = lambda p, q: -(not 0 < len(p) == len(q) > 0) or sum(((x - y) ** 2 for (x, y) in zip(p, q))) ** 0.5


import numpy as np

def distance(p1, p2):
	return np.linalg.norm(np.array(p1) - np.array(p2)) if 0 < len(p1) == len(p2) else -1


import math

def distance(p1, p2):
	if len(p1) != len(p2) or len(p1) == 0:
		return -1
	if len(p1) == 1:
		return math.fabs(p1[0] - p2[0])
	dist = 0
	for i in range(len(p1)):
		dist += (p1[i] - p2[i]) ** 2
	return dist ** 0.5


def distance(p1, p2):
	if len(p1) != len(p2) or len(p1) < 1:
		return -1
	return sum(((p1[i] - p2[i]) ** 2 for i in range(len(p1)))) ** 0.5


import math

def distance(p1, p2):
	if p1 == [] or p2 == [] or len(p1) != len(p2):
		return -1
	diff = []
	for i in range(len(p1)):
		diff.append((p1[i] - p2[i]) ** 2)
	return math.sqrt(sum(diff))