0

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.

Asocia
  • 5,935
  • 2
  • 21
  • 46
  • 2
    Hi William. Please format your code properly for better readability. – navneethc Apr 05 '21 at 17:26
  • If this is running as an automated test are you supposed to support custom input? Either implementing a specific function or reading data from stdin? – blueteeth Jun 13 '21 at 18:31

3 Answers3

0

There's nothing wrong with your program apart that for some reason you substract 0 but that doesn't change the results.

  • I added the zeroes as a variant since the starting point was (0, 0) thinking maybe that had something to do with it but with or without, it still came out the same. I still don't know why it says I'm wrong. Thanks for the input. – William Anderson Apr 05 '21 at 18:19
0

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))
blueteeth
  • 3,330
  • 1
  • 13
  • 23
Sachin KS
  • 1
  • 1
0
#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))
Liria
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 03 '22 at 06:05