Problem 5 Closest Point Write a program that takes three flo

Problem 5. (Closest Point) Write a program that takes three floats x, y, and z from the command line, reads from standard input a sequence of coordinates (xi, yi, zi), and writes the coordinates of the point closest to (x, y, z). Recall that the square of the distance between (x, y, z) and (xi, yi, zi) is (x xi)2 + (y yi)2 + (z zi)2. For efficiency, do not use either math.sqrt() or the ** operator.

closest.py =

# closest.py: takes three floats x, y, and z from the command line, reads from
# standard input a sequence of coordinates (x_i, y_i, z_i), and writes the
# coordinates of the point closest to (x, y, z).

import stdio
import sys

# Read x, y, and z from command line, as floats.
x = ...
y = ...
z = ...

# Closest squared-distance so far, initialized to infinity.
bestDist2 = float(\'inf\')

# Read coordinates (xi, yi, zi) from standard input and calculate its
# squared-distance to the point (x, y, z). Check if that value is smaller
# than bestDist2, and if so, update bestDist2 to the new value, and
# let (bestx, besty, bestz) be (xi, yi, zi).
while ...:
...

# Write the closest point (bestx, besty, bestz).
...

Solution

import sys
#import stdio
#print(\'Number of arguments:\', len(sys.argv), \'arguments.\')
#print(sys.argv)
if len(sys.argv) != 4:
   print(\"Problem with number of inputs\")
   sys.exit()
x1 = float(sys.argv[1])
y1 = float(sys.argv[2])
z1 = float(sys.argv[3])
bx=1.0
by=1.0
bz=1.0
n = int(raw_input(\"How many points are you going to enter?\"))
dist = float(\'inf\')
for i in range(0,n):
   print(\"New point:\")
   x = float(raw_input(\"Enter the x coordinate:\"))
   y = float(raw_input(\"Enter the y coordinate:\"))
   z = float(raw_input(\"Enter the z coordinate:\"))
   tmp = (x-x1)*(x-x1) + (y-y1)*(y-y1)+(z-z1)*(z-z1)
   if(tmp<dist):
       dist = tmp
       bx=x
       by=y
       bz=z
print(\"The closest point is (\"+str(bx)+\',\'+str(by)+\',\'+str(bz)+\')\')

Problem 5. (Closest Point) Write a program that takes three floats x, y, and z from the command line, reads from standard input a sequence of coordinates (xi, y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site