Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
24
Task/Closest-pair-problem/Elixir/closest-pair-problem.elixir
Normal file
24
Task/Closest-pair-problem/Elixir/closest-pair-problem.elixir
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
defmodule Closest_pair do
|
||||
def bruteForce([p0,p1|_] = points) do
|
||||
pnts = List.to_tuple(points)
|
||||
minDist = distance(p0, p1)
|
||||
n = tuple_size(pnts)
|
||||
{minDistance, minPoints} = Enum.reduce(0..n-2, {minDist, [0,1]}, fn i,{mD,mP} ->
|
||||
Enum.reduce(i+1..n-1, {mD,mP}, fn j,{md,mp} ->
|
||||
dist = distance(elem(pnts,i), elem(pnts,j))
|
||||
if dist < md, do: {dist, [i,j]}, else: {md,mp}
|
||||
end)
|
||||
end)
|
||||
{:math.sqrt(minDistance), minPoints}
|
||||
end
|
||||
|
||||
defp distance({p0x,p0y}, {p1x,p1y}) do
|
||||
(p1x - p0x) * (p1x - p0x) + (p1y - p0y) * (p1y - p0y)
|
||||
end
|
||||
end
|
||||
|
||||
data = [{0.654682, 0.925557}, {0.409382, 0.619391}, {0.891663, 0.888594}, {0.716629, 0.996200},
|
||||
{0.477721, 0.946355}, {0.925092, 0.818220}, {0.624291, 0.142924}, {0.211332, 0.221507},
|
||||
{0.293786, 0.691701}, {0.839186, 0.728260}]
|
||||
|
||||
IO.inspect Closest_pair.bruteForce(data)
|
||||
Loading…
Add table
Add a link
Reference in a new issue