Skip to content

Instantly share code, notes, and snippets.

@tkrs
Last active July 29, 2017 16:01
Show Gist options
  • Save tkrs/88d452daec03d4a85f6f to your computer and use it in GitHub Desktop.
Save tkrs/88d452daec03d4a85f6f to your computer and use it in GitHub Desktop.
DistanceAlgorithms
#!/usr/bin/env python3
import math
def distance(xs, ys):
return math.sqrt(sum([math.pow(q - p, 2) for (q,p) in zip(xs, ys)]))
a = [1,2,4,5,2]
b = [3,4,7,9,1]
print(distance(a, b))
a = [1,2,4,5,2]
b = [1,2,4,5,2]
print(distance(a, b))
def distance(a, b):
m = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
for i in range(len(a) + 1): m[i][0] = i
for i in range(len(b) + 1): m[0][i] = i
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
m[i][j] = min(
m[i - 1][j ] + 1,
m[i ][j - 1] + 1,
m[i - 1][j - 1] + (0 if a[i - 1] == b[j - 1] else 1)
)
return m[-1][-1]
#!/usr/bin/env python
import math
class P(object):
def __init__(self, x, y):
self.x = x
self.y = y
def distance(a, b):
return math.sqrt(math.pow(b.x-a.x, 2) + math.pow(b.y-a.y, 2))
if __name__ == '__main__':
a = P(1, 1)
b = P(4, 5)
print a.distance(b)
main = do
let x = (1, 1)
let y = (4, 5)
print $ distance x y
distance (a, b) (c, d) = sqrt $ x + y
where x = (c - a) ^ 2
y = (d - b) ^ 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment