Proche voisin
On souhaite programmer une fonction donnant le point d'une liste de points le plus proche d'un point
de départ. Les points sont tous à coordonnées entières.
Les points sont donnés sous la forme d'un tuple de deux entiers qui sont leurs coordonnées (dans un repère orthonormé).
La liste des points Ă traiter est donc un tableau de tuples.
On rappelle que la distance entre deux points du plan de coordonnées \((x; y)\) et \((x'; y')\)
est donnée par la formule :
\(d=\sqrt{(x-x')^2+(y-y')^2}\)
On importe pour cela la fonction racine carrée (sqrt
) du module math
de Python.
Exemples
Python Console Session>>> distance((1, 0), (5, 3))
5.0
>>> distance((1, -4), (6, 8))
13.0
>>> proche_voisin([(7, 9), (2, 5), (5, 2)], (0, 0))
(2, 5)
>>> proche_voisin([(7, 9), (2, 5), (5, 2)], (5, 2))
(5, 2)
Compléter le code des fonctions distance
et proche_voisin
fournies ci-dessous pour qu’elles répondent à leurs spécifications.
Attention
Il est interdit d'utiliser min
Compléter le code ci-dessous
.65039.9888.128013.127797Eg[ r;)/(lDo4,6Ib=ax+5uûtP7e-h0TmnCkê:Séq.èy1cd32à 8_wvLpf*9RA]is050Y0F0C0w0=0n0?0h0X0n0w0?0?0v010C0=0+010406050?0A0K0K0w0i0V040Q0p0n0A170p0L0h020w0K0+0j0h0/0F1h0i0S0A0F0?050l1e1g1i1k1c0+04051P1I1S0l1P1c0Y0=0)0 111315110L0f0A0w0f0F0G0+0V0C0H1r0h0H0=0f0H0n1{0H0C1a050`0u0n0F1#1214011`1|1~1|0C2426220C0i1Q1?0 1n0?0+0w0L150!01281%010,0|0F0L1v0F222q2s2x2a2A262D0K2F040c0h0D0i0p0+0p0?0=1q1s0^2o0i0i0F0X2!1I2H0L1Q0l1?2:2k2m2l230Y2J1(0=0L2C2X221Y1!10292}2 0L0p33220+2)1Q2.2:3g1d2r1s352y390i1h0n220w1_2)0,15030%0%0X3a0F1~380p0G0W3H1a0h0W1I0w3h3k1b3j2I3m2a3o3q3s3u0F3w013y3A3C3E303H0G2v040h0!3N3P2s3R2.2|013W0w3r1Q3t0H3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