0
Tuple unpacking
https://code.sololearn.com/cuEUJ988z5Mi/?ref=app Need to find the smallest distance form the given list of tuples
8 Respuestas
+ 3
David Ashton 's code can be reduced to;
b = []
b.extend((x*x+y*y)**0.5 for x, y in points)
print(min(b))
+ 7
I would do it this way
b = []
for i, j in points:
x, y = i, j
a =[(x*x + y*y)**0.5]
b.extend(a)
print(min(b))
+ 4
ChaoticDawg Nicely done! And pythonic. 👏
Might as well reduce it all the way
print(min((x*x+y*y)**0.5 for x, y in points))
😁
+ 2
David Ashton 😆🤣😂 lol, nice
+ 1
print(min(b))
Also pit b on outside of for loop, otherwise it resets
+ 1
b initialization should happens outside for loops. What happens in your code is,
i=0->
b=[]
a= distance calc
b=[a0]
i=1->
b=[]
a= calc
b=[a1]
i=2->
b=[]
a=calc
b=[a3]
and so on. B never store every results of iteration, only its last iteration got stored. Hence my suggestion to initialize b outside of for loops
0
Satrio Bayu Pradhipta I tried it it's printing 90 but it should print 43
When I print inside the for loop I'm able to print all the values of a
But couldn't print the minimum value in a
0
David Ashton I thought i can take the both values of the tuple inside the for loop
thanks for rectifying the error sir!!