[Community Puzzle] Hello, World!

The one you point out is, but not the other two :

Location 1
Joe_s_Place distance: 612
Jack_s_Place distance: 612
Laplace distance: 612
1 Like

No. My results:

Travel point #0: N000000 W1753000
 = in DEG: (0, -175.5) 
 = in RAD: (0, -3.06305283725)
    distance = 612 to Joe_s_Place: N000000 E1790000 = in DEG: (-0, 179) = in RAD: (-0, 3.1241393610699)
    distance = 612 to Jack_s_Place: N000000 W1700000 = in DEG: (-0, -170) = in RAD: (-0, -2.9670597283904)
    distance = 612 to Laplace: N000000 E1790000 = in DEG: (0, 179) = in RAD: (0, 3.1241393610699)
  Result: closest capitals indexes = 0, 1, 2; distance = 612

Distance before rounding is 611.57209654507 for all 3 points.
If you try to visualize it on a globe they must be the same:
Each point is on the equator.
Travel point is West 175 and half degrees.
Jack is West 170 degrees, that is 5.5 degrees off the travel point.
Joe and Laplace is at East 179 degrees that would be West 181 degrees - that is again 5.5 degrees from the travel point.

Do you treat W and E correctly as the sign of degree? Do you treat longitude and latitude correctly, i.e not interchanged?

My distance function is like this:

public function getDistance(GeoLocation $otherLoc): int {
    $lat1rad = deg2rad($this->lat);
    $lat2rad = deg2rad($otherLoc->lat);
    $long1rad = deg2rad($this->long);
    $long2rad = deg2rad($otherLoc->long);
    $delta = acos(sin($lat1rad) * sin($lat2rad) + cos($lat1rad) * cos($lat2rad) * cos(abs($long1rad - $long2rad)));
    return intval(round($delta * self::Radius));

finally I made it , 100%
the problem about why my codes return different values on #1 of testcase6 , is because I use 57.3 to turn degree into radian, after I change this value to 57.29578, the answer is correct. Ooops that’s tough but finally I made it.
Thanks for all to help me find out these details.

1 Like

57.29578 is not good enough. In some more extreme cases it can cause you to fail again.
The conversion ratio is obtained by 180 / pi.

Many programming languages have the value of pi as a constant. If not, define pi as a constant with as high precision as you could. Create another constant, using the highest precision float-point data type, by calculating 180/pi for your own use.

This ratio is approx. as 57.295779513082320876798154814105

1 Like

I’m coding in Perl and all of the IDE Test cases are working, and all of the Validator test cases are working EXCEPT for validator #4. IDE Test case #4 is Lonely Planet and literally only has one potential message to display, so I am mystified as to what the difference is with validator #4 that would cause it to not work when all others do.

Is there any way to get the specs for validator #4 to perhaps help me understand why it is not working? Is it that different from IDE test case #4?

It has very similar edge case coordinates as the test case. Sent it via DM.

Thanks @Tbali, I just found your DM.

I was able to finally pass all validators. For some reason, my final loop that checks for ties was going inexplicably infinite even though it should have stopped the second there was no more array to process, or in this case, should not have entered the loop in the first place since we had already printed the one and only correct answer in the test case.

Looks like a perl idiosyncracy that I have to yet figure out.

And now I know what was happening with the loop as well. I went back and changed my loop condition from $array_dist[$x] == $short_dist and changed the operator to β€œeq” for string comparisons. Now instead of the undefined array saying that the number zero is the same as the undefined array, it actually compares the string representation which in this case ended up being ("" eq β€œ0”). Now my loop correctly identifies the end of the elements to print.

Thanks for the help!

1 Like