September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,14 @@
|
|||
Dim x(9)
|
||||
x = {0.654682, 0.409382, 0.891663, 0.716629, 0.477721, 0.925092, 0.624291, 0.211332, 0.293786, 0.839186}
|
||||
Dim y(9)
|
||||
y = {0.925557, 0.619391, 0.888594, 0.996200, 0.946355, 0.818220, 0.142924, 0.221507, 0.691701, 0.728260}
|
||||
|
||||
minDist = 1^30
|
||||
For i = 0 To 8
|
||||
For j = i+1 To 9
|
||||
dist = (x[i] - x[j])^2 + (y[i] - y[j])^2
|
||||
If dist < minDist Then minDist = dist : minDisti = i : minDistj = j
|
||||
Next j
|
||||
Next i
|
||||
Print "El par más cercano es "; minDisti; " y "; minDistj; " a una distancia de "; Sqr(minDist)
|
||||
End
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
Dim As Integer i, j
|
||||
Dim As Double minDist = 1^30
|
||||
Dim As Double x(9), y(9), dist, mini, minj
|
||||
|
||||
Data 0.654682, 0.925557
|
||||
Data 0.409382, 0.619391
|
||||
Data 0.891663, 0.888594
|
||||
Data 0.716629, 0.996200
|
||||
Data 0.477721, 0.946355
|
||||
Data 0.925092, 0.818220
|
||||
Data 0.624291, 0.142924
|
||||
Data 0.211332, 0.221507
|
||||
Data 0.293786, 0.691701
|
||||
Data 0.839186, 0.728260
|
||||
|
||||
For i = 0 To 9
|
||||
Read x(i), y(i)
|
||||
Next i
|
||||
|
||||
For i = 0 To 8
|
||||
For j = i+1 To 9
|
||||
dist = (x(i) - x(j))^2 + (y(i) - y(j))^2
|
||||
If dist < minDist Then
|
||||
minDist = dist
|
||||
mini = i
|
||||
minj = j
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
Print "El par más cercano es "; mini; " y "; minj; " a una distancia de "; Sqr(minDist)
|
||||
End
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
/*REXX program solves the closest pair of points problem (in two dimensions). */
|
||||
/*REXX program solves the closest pair of points problem (in two dimensions). */
|
||||
parse arg N low high seed . /*obtain optional arguments from the CL*/
|
||||
if N=='' | N=="," then N= 100 /*Not specified? Then use the default.*/
|
||||
if low=='' | low=="," then low= 0 /* " " " " " " */
|
||||
|
|
@ -6,16 +6,16 @@ if high=='' | high=="," then high= 20000 /* " " " " "
|
|||
if datatype(seed, 'W') then call random ,,seed /*seed for RANDOM (BIF) repeatability.*/
|
||||
w=length(high); w=w + (w//2==0) /*W: for aligning the output columns.*/
|
||||
/*╔══════════════════════╗*/ do j=1 for N /*generate N random points*/
|
||||
/*║ generate N points. ║*/ @x.j=random(low, high) /* " a random X */
|
||||
/*╚══════════════════════╝*/ @y.j=random(low, high) /* " " " Y */
|
||||
/*║ generate N points. ║*/ @x.j= random(low, high) /* " a random X */
|
||||
/*╚══════════════════════╝*/ @y.j= random(low, high) /* " " " Y */
|
||||
end /*j*/ /*X & Y make the point.*/
|
||||
A=1; B=2 /* [↓] MINDD is actually the squared*/
|
||||
minDD= (@x.A - @x.B)**2 + (@y.A - @y.B)**2 /*distance between the first two points*/
|
||||
/* [↓] use of XJ & YJ speed things up.*/
|
||||
do j=1 for N-1; xj=@x.j; yj=@y.j /*find minimum distance between a ··· */
|
||||
do j=1 for N-1; xj= @x.j; yj= @y.j /*find minimum distance between a ··· */
|
||||
do k=j+1 to N /* ··· point and all the other points.*/
|
||||
dd=(xj - @x.k)**2 + (yj - @y.k)**2 /*compute squared distance from points.*/
|
||||
if dd<minDD then parse value dd j k with minDD A B
|
||||
dd= (xj - @x.k)**2 + (yj - @y.k)**2 /*compute squared distance from points.*/
|
||||
if dd<minDD then parse value dd j k with minDD A B
|
||||
end /*k*/ /* [↑] needn't take SQRT of DD (yet).*/
|
||||
end /*j*/ /* [↑] when done, A & B are the points*/
|
||||
$= 'For ' N " points, the minimum distance between the two points: "
|
||||
|
|
@ -25,6 +25,6 @@ say left('', length($) - 1) "["right(@x.B, w)',' right(@y.B, w)"
|
|||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
|
||||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ % 2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/; return g
|
||||
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g= g *.5'e'_ % 2
|
||||
do j=0 while h>9; m.j= h; h= h % 2 + 1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g= (g+x/g)*.5; end /*k*/; return g
|
||||
|
|
|
|||
100
Task/Closest-pair-problem/Swift/closest-pair-problem.swift
Normal file
100
Task/Closest-pair-problem/Swift/closest-pair-problem.swift
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
import Foundation
|
||||
|
||||
struct Point {
|
||||
var x: Double
|
||||
var y: Double
|
||||
|
||||
func distance(to p: Point) -> Double {
|
||||
let x = pow(p.x - self.x, 2)
|
||||
let y = pow(p.y - self.y, 2)
|
||||
|
||||
return (x + y).squareRoot()
|
||||
}
|
||||
}
|
||||
|
||||
extension Collection where Element == Point {
|
||||
func closestPair() -> (Point, Point)? {
|
||||
let (xP, xY) = (sorted(by: { $0.x < $1.x }), sorted(by: { $0.y < $1.y }))
|
||||
|
||||
return Self.closestPair(xP, xY)?.1
|
||||
}
|
||||
|
||||
static func closestPair(_ xP: [Element], _ yP: [Element]) -> (Double, (Point, Point))? {
|
||||
guard xP.count > 3 else { return xP.closestPairBruteForce() }
|
||||
|
||||
let half = xP.count / 2
|
||||
let xl = Array(xP[..<half])
|
||||
let xr = Array(xP[half...])
|
||||
let xm = xl.last!.x
|
||||
let (yl, yr) = yP.reduce(into: ([Element](), [Element]()), {cur, el in
|
||||
if el.x > xm {
|
||||
cur.1.append(el)
|
||||
} else {
|
||||
cur.0.append(el)
|
||||
}
|
||||
})
|
||||
|
||||
guard let (distanceL, pairL) = closestPair(xl, yl) else { return nil }
|
||||
guard let (distanceR, pairR) = closestPair(xr, yr) else { return nil }
|
||||
|
||||
let (dMin, pairMin) = distanceL > distanceR ? (distanceR, pairR) : (distanceL, pairL)
|
||||
|
||||
let ys = yP.filter({ abs(xm - $0.x) < dMin })
|
||||
|
||||
var (closest, pairClosest) = (dMin, pairMin)
|
||||
|
||||
for i in 0..<ys.count {
|
||||
let p1 = ys[i]
|
||||
|
||||
for k in i+1..<ys.count {
|
||||
let p2 = ys[k]
|
||||
|
||||
guard abs(p2.y - p1.y) < dMin else { break }
|
||||
|
||||
let distance = abs(p1.distance(to: p2))
|
||||
|
||||
if distance < closest {
|
||||
(closest, pairClosest) = (distance, (p1, p2))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (closest, pairClosest)
|
||||
}
|
||||
|
||||
func closestPairBruteForce() -> (Double, (Point, Point))? {
|
||||
guard count >= 2 else { return nil }
|
||||
|
||||
var closestPoints = (self.first!, self[index(after: startIndex)])
|
||||
var minDistance = abs(closestPoints.0.distance(to: closestPoints.1))
|
||||
|
||||
guard count != 2 else { return (minDistance, closestPoints) }
|
||||
|
||||
for i in 0..<count {
|
||||
for j in i+1..<count {
|
||||
let (iIndex, jIndex) = (index(startIndex, offsetBy: i), index(startIndex, offsetBy: j))
|
||||
let (p1, p2) = (self[iIndex], self[jIndex])
|
||||
|
||||
let distance = abs(p1.distance(to: p2))
|
||||
|
||||
if distance < minDistance {
|
||||
minDistance = distance
|
||||
closestPoints = (p1, p2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (minDistance, closestPoints)
|
||||
}
|
||||
}
|
||||
|
||||
var points = [Point]()
|
||||
|
||||
for _ in 0..<10_000 {
|
||||
points.append(Point(
|
||||
x: .random(in: -10.0...10.0),
|
||||
y: .random(in: -10.0...10.0)
|
||||
))
|
||||
}
|
||||
|
||||
print(points.closestPair()!)
|
||||
Loading…
Add table
Add a link
Reference in a new issue