Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Wild Dogs solution in Clear category for Wild Dogs by yuwai
from math import sqrt
def wild_dogs(coords):
l = len(coords)
def is_line(point1, point2, point3): # 判断三点一线
if (point3[0] - point1[0]) * (point2[1] - point1[1]) == (point3[1] - point1[1]) * (point2[0] - point1[0]):
return True
else:
return False
d = {}
def get_distance(point1, point2):
if (point1[0] - point2[0]) * (point1[1] - point2[1]) == 0:
return 0
else:
a = point1[0] - point2[0]
b = point2[1] - point1[1]
c = point2[0] * point1[1] - point1[0] * point2[1]
d = c / sqrt(a ** 2 + b ** 2)
return abs(d)
if l == 2:
return round(get_distance(coords[0], coords[1]),2)
else:
n = 1
max_point = {}
for i in range(l - 2):
for j in range(i + 1, l - 1):
d[(i, j)] = 1
for k in range(j + 1,l):
if is_line(coords[i], coords[j], coords[k]):
d[(i, j)] += 1
if d[(i, j)] > n:
n = d[(i, j)]
max_point.clear()
max_point[(i, j)] = n
if d[(i, j)] == n:
max_point[(i, j)] = n
if n==1:
max_point[(0,l-1)]=1
max_point[(l-2,l-1)]=1
distance = []
for key in max_point.keys():
#print(key)
distance.append(get_distance(coords[key[0]], coords[key[1]]))
distance.sort()
#print(round(distance[0],2))
return round(distance[0],2)
if __name__ == '__main__':
print("Example:")
print(wild_dogs([(7, 122), (8, 139), (9, 156),
(10, 173), (11, 190), (-100, 1)]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert wild_dogs([(7, 122), (8, 139), (9, 156),
(10, 173), (11, 190), (-100, 1)]) == 0.18
assert wild_dogs([(6, -0.5), (3, -5), (1, -20)]) == 3.63
assert wild_dogs([(10, 10), (13, 13), (21, 18)]) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
April 16, 2019