B
B
BitNeBolt2020-10-21 12:20:35
Python
BitNeBolt, 2020-10-21 12:20:35

Why does it work in python but not in Java?

There is a method that should order the points in the new array by increasing distance from the first selected point.

Here's what's in python:

Python

points = []
  sorted_list = []
  INF = 999

  def findClosestPointIndex(self, point):
    best_distance = self.INF
    best_index = -1

    for i in range(len(self.points)):
      current_point = self.points[i]
      if (not current_point.visited):
        distance = current_point.distanceTo(point)

        if (distance < best_distance):
          best_distance = distance
          best_index = i

    print("Closest to " + point.buildStr() + " is: " + self.points[best_index].buildStr())

    if (best_index != -1):
      self.points[best_index].visited = True
      self.sorted_list.append(self.points[best_index])
      self.findClosestPointIndex(self.points[best_index])

    return best_index

  def main(self):
    points_count = int(input("Enter points count: "))

    for i in range(points_count):
      self.points.append(Point(random.randint(0, 9), random.randint(0, 9)))

    start_point = self.points[0]
    start_point.visited = True
    self.sorted_list.append(start_point)
    self.findClosestPointIndex(start_point)

    print("Source list: ")
    for point in self.points:
      print(point.buildStr())

    print("Sorted list:")
    for point in self.sorted_list:
      print(point.buildStr())

    print("Finished.")



Here's what's in Java:
Java

private void findBestAndAdd(Vector3 point)
    {
        double bestDistance = Double.MAX_VALUE;
        int bestIndex = -1;

        for (int i = 0; i < points.size(); i++)
        {
            Vector3 currentPoint = points.get(i);

            if (! currentPoint.visited)
            {
                double distance = Vector3.distanceBetween(point, currentPoint);

                if (distance < bestDistance)
                {
                    bestDistance = distance;
                    bestIndex = i;
                }
            }
        }

        if (bestIndex != -1)
        {
            Vector3 bestPoint = points.get(bestIndex);

            bestPoint.visited = true;
            sortedPoints.add(bestPoint);
            findBestAndAdd(bestPoint);
        }
    }

    public void sortPoints()
    {
        System.out.println("Array before sort:");
        for (Vector3 point : points)
            System.out.println(point.toString());

        Vector3 startPoint = points.get(0);
        startPoint.visited = true;
        sortedPoints.add(startPoint);

        findBestAndAdd(startPoint);

        this.points = sortedPoints;

        System.out.println("Array after sort:");
        for (Vector3 point : points)
            System.out.println(point.toString());
    }



Here are the results given the input:
Python

Input:
Source list:
8;9
2;0
6;0
9;8


Weekend:
Sorted list:
8;9
9;8
6;0
2;0



Java

Input:
Array before sort:
3.2060296130482615 ; 15.364776819179262 ; 0.0
2.9060809054810197 ; 14.744574242841 ; 0.0
2.9060809054810197 ; 14.744574242841 ; 30.0
3.2060296130482615 ; 15.364776819179262 ; 30.0


Weekend:
Array after sort:
3.2060296130482615 ; 15.364776819179262 ; 0.0
3.2060296130482615 ; 15.364776819179262 ; 30.0
2.9060809054810197 ; 14.744574242841 ; 30.0



Why does it work correctly (as I need) in python, but not in Java.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2020-10-21
@dmshar

Do you believe in miracles? If not, then the answer is trivial - because the Java script has an error. Did you write both codes yourself? And debugged while writing. Here, carefully, step by step, we go through both programs and look for where the difference occurs.
PS By the way, why are you testing scripts on different inputs? And absolutely different - even by the number of elements (coordinates???) in the object. First, see how they work on the same ones. And then make a decision.
And yet - what's wrong with the results produced by the Java script? In my opinion, quite adequate result of sorting in relation to the first element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question