'Mapping Software Problem in Python Data Structures Course in SoloLearn
I'm currently working on the Mapping Software problem in the Tuple Unpacking lesson in the Python Data Structures course on SoloLearn. I ran my code in my own IDE and it came out to what I think is correct but the test case in SoloLearn says I'm wrong and their answer is hidden so I have no idea if I'm even close. I've had trouble finding the answer to it searching the interwebs. Here's the problem and my code.
You are working on a mapping software. The map is stored as a list of points, where each item is represented as a tuple, containing the X and Y coordinates of the point. You need to calculate and output the distance to the closest point from the point (0, 0). To calculate the distance of the point (x, y) from (0, 0), use the following formula: √x²+y²
Code:
import math
points = [
(12, 55),
(880, 123),
(64, 64),
(190, 1024),
(77, 33),
(42, 11),
(0, 90)
]
distances = []
for (x, y) in points:
z = math.sqrt(((x ** 2) - 0 + ((y ** 2) - 0)))
distances.append(z)
print(min(distances))
Output:
43.41658669218482
I feel like it has something to do with rounding or something small like that but I'm not sure. Can anybody help me. Thanks in advance.
Solution 1:[1]
There's nothing wrong with your program apart that for some reason you substract 0 but that doesn't change the results.
Solution 2:[2]
Don't import math in this problem. This code will work.
points = [
(12, 55),
(880, 123),
(64, 64),
(190, 1024),
(77, 33),
(42, 11),
(0, 90)
]
# your code goes here
dist_list = []
for (a,b) in points:
dist = (a ** 2 + b ** 2)
dist = dist ** (1/2)
dist_list.append(dist)
print(min(dist_list))
Solution 3:[3]
#this code works perfectly:
import math
points = [(12, 55),
(880, 123),
(64, 64),
(190, 1024),
(77, 33),
(42, 11),(0, 90)]
# your code goes here
dist=[]
for x,y in points:
d= math.sqrt(x**2 + y**2)
dist.append(d)
print(min(dist))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Valeri Terziyski |
| Solution 2 | blueteeth |
| Solution 3 |
