+ 2

Tuple unpacking and the mapping problem.

I'm stuck on the problem that has to do with tuple unpacking with mapping software. I am asked to output the shortest distance from the point (0, 0) out of a list of points. The code I have feels right but the test case says it is wrong and the test case is hidden so I have no idea if I'm close or if it is problem with the final number not being rounded. Can anybody tell me where I am doing wrong? Here's my code. import math # Calculate and output the distance to the closest point from # the point (0, 0) 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) + ( y ** 2)) distances.append([z]) print(min(distances))

28th Apr 2021, 8:10 AM
William Anderson
William Anderson - avatar
24 Respostas
+ 8
This works fine, without importing math: points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] #your code goes here distances = [] for (x, y) in points: distances.append((x**2 + y**2)**0.5) print(min(distances))
15th Aug 2021, 11:28 AM
Jo_Flow
+ 2
#can be modify import math points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] list1=[] for (x,y) in points: distance=math.sqrt(x**2+y**2) list1.append(distance) print(min(list1))
9th Jul 2021, 1:24 PM
Kuan Tian
Kuan Tian - avatar
+ 1
This worked for me: from math import sqrt points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] #your code goes here distancias = [] for (x,y) in points: elemento = sqrt(x**2+y**2) distancias.append(elemento) print (min(distancias))
23rd Oct 2022, 3:16 PM
Jeanfreddy GutiƩrrez
Jeanfreddy GutiƩrrez - avatar
0
since its a tuple, you need another for loop to iterate over those values. OR call them directly dustances = [] for pair in points: for x,y in pair: ..... ..... AND the best way to test is to give input you know the answer too and Print your variables! Dkn't matter how the code "feels" or if test cases are hidden
28th Apr 2021, 9:18 AM
Slick
Slick - avatar
0
Slick I tried this but it did not work points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] distances = [] for pair in points: for x,y in pair: z=((x**2)+(y**2)) distances.appened([z]) print(min(distances))
29th Apr 2021, 12:24 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
yup, that code would only append the last sum of squares. distances.append(z) needs to be in the second for loop, not the first
29th Apr 2021, 12:57 PM
Slick
Slick - avatar
0
i don't get
29th Apr 2021, 1:11 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
put the code in the playground and link here
29th Apr 2021, 1:12 PM
Slick
Slick - avatar
29th Apr 2021, 1:19 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
https://code.sololearn.com/c92PWMzbcZT4/?ref=app ended up, dropping the second loop because Im unable to unpack integers. But using indexing, you can access each tuple pair and get the values
29th Apr 2021, 1:40 PM
Slick
Slick - avatar
0
Slick it still did not work
1st May 2021, 12:40 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
What do you mean? the code does exactly what its written. What kind of output do you want? the code adds the squares of each numer in the list and prints the smallest total.
1st May 2021, 1:29 PM
Slick
Slick - avatar
0
Slick here is the queestion 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Ā² You can iterate over the list and use tuple unpacking to get the x and y coordinates of each point: for (x, y) in points: and output the smallest value. I can't know what exactly the problem is because it is only one test case
9th May 2021, 12:17 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
You can though, just do the problem out on paper with serveral examples the right way. Then try each set of examples in the code. If the answers match, you did it right. Then try it out in the code coach. If it doesnt pass, then its the math thats wrong. You gotta figure out the math, we'll be here to help with the coding
9th May 2021, 12:21 PM
Slick
Slick - avatar
0
I decided to go straight to the smallest distance instead of creating another list with all distances. By using 'for x, y in points' no second loop is necessary. import math points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] #your code goes here distance = int() for x, y in points: distance = math.sqrt(x**2 + y**2) shortest = distance if distance < shortest or shortest == 0 else shortest print(shortest)
24th Jun 2021, 7:37 AM
Maikel
0
points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] distances = [(x**2 + y**2) ** 0.5 for x, y in points] print(min(distances))
17th Sep 2021, 3:15 AM
MORTEZA ABDOLLAHI
MORTEZA ABDOLLAHI - avatar
0
points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] #your code goes here new_list=[] for num1,num2 in points: result = (num1**2+num2**2)**0.5 new_list.append(result) print (min(new_list))
2nd Oct 2021, 10:15 AM
jiaqi chen
jiaqi chen - avatar
0
how about my short way ;) import math points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] daftar =[ math.sqrt(pow(x,2)+pow(y,2)) for(x,y) in points] print(min(daftar))
25th Feb 2022, 11:02 AM
achmad mustafa
achmad mustafa - avatar
0
try this, points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] distances = [] for (x, y) in points: distances.append((x**2 + y**2)**0.5) print(min(distances))
31st May 2022, 12:57 PM
sudesh wimalaweera
0
points = [ (12, 55), (880, 123), (64, 64), (190, 1024), (77, 33), (42, 11), (0, 90) ] #your code goes here distances = [] for (x, y) in points: distances.append((x**2 + y**2)**0.5) print(min(distances))
14th Nov 2022, 8:05 AM
Pooja Patel
Pooja Patel - avatar