Home > Software design >  find the minimum distance fromapoint on x axis from 3 other points on x axis
find the minimum distance fromapoint on x axis from 3 other points on x axis

Time:04-25

I want to calculate the minimum distance from 3 points the user entered, all the points are on the x-axis, could anyone help me to fix my code, please?

d=[]
i=0
a=list(input("please enter x1, x2, x3: "))
a=a.sort()
for item in range(0,(max(a))):
    d=sum(a[i]-item)
print(f"here is the minimum distance{min(d)}")

CodePudding user response:

first list(input(...)) probably isnt doing what you want if i enter '123 33 12' this would become ['1','2','3',' ','3'...]

instead you want split

a_list = input("Enter some numbers with spaces").split()
['123','33','12']

then you need to make them integers

num_list = [int(x) for x in a_list]

then you just get the min

closest = min(num_list,key=lambda x:abs(x-56))
print(f"{closest} is the closest point to 56")

if you only care about the distance and not the actual value just change where you cast it to int ot

dist_list = [abs(int(x)-56) for x in a_list]
print(f"minimum distance = {min(dist_list)}")

with your extra comment i guess its just

num_list = [11,55,27]
print("POINT:",sum(num_list)/len(num_list),f"requires the least movement for all {num_list}")

CodePudding user response:

Please check if this is doing what you expect. The question isn't quiet clear to me, or maybe the loop you use just confuses me.

if __name__ == '__main__':
    a = input("Please enter x-coordinates separated by spaces: ").split()
    a = [int(c) for c in a]
    a.sort()
    best = float('inf')
    for i, number in enumerate(a[1:]):
        best = min(best, number-a[i])
        
    print(f"The minimum distance of the given points is {best}")

CodePudding user response:

Try like this

print('how many numbers?')
n = int(input('n: '))

a = []
for i in range(1, n 1):
    x = input(f'x_{i}: ')
    a.append(x)

a_int = sorted([int(x) for x in a])
print(a_int)

rest = []
for j in range(n-1):
    r = abs(a_int[j] - a_int[j 1])
    rest.append((r, j))

p = min(rest)

print(f'closest points are ({a_int[p[1]]}, {a_int[p[1] 1]}) and distance is {p[0]}')

you will have the chance to input how many numbers you want to check, later on, it is going to sort them from the smallest to the biggest. To check the distance, it will rest each one of them and save the position to later search for the minimum.

let me know if does it help. it is a bit of brute force though ...

CodePudding user response:

Assuming you use @Joran Beasley input function

a_list = input("Enter some numbers with spaces").split()

You can use:

import numpy as np

# convert strings to floats of integers
a = np.array(a).astype(float)

# find distances between each point
distances = np.abs(np.subtract(a, a[:,np.newaxis]))

# remove same point distances (distance = 0)
distances_non_zero = distances[distances != 0]

# get minimum distance
minimum_distace = np.min(distances_non_zero)
  • Related