Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Wild Dogs by BrianMcleod
from math import sqrt
def find_line(dog1, dog2):
A = dog2[1] - dog1[1]
B = dog1[0] - dog2[0]
C = -(A*dog1[0] + B*dog1[1])
if B:
return (A/B, 1, C/B)
else:
return (1, B/A, C/A)
def origin_line_distance(line):
return abs(line[2])/sqrt((line[0]**2) + (line[1]**2))
def wild_dogs(coords):
lines = {}
for i in range(len(coords)):
for j in range(i+1, len(coords)):
line =find_line(coords[i], coords[j])
if line in lines.keys():
lines[line] |= set([tuple(coords[i]), tuple(coords[j])])
else:
lines[line] = set([tuple(coords[i]), tuple(coords[j])])
max_line = max(lines.items(), key=lambda kv: (len(kv[1]), -origin_line_distance(kv[0]) ))
return round(origin_line_distance(max_line[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)]) == 1.13
assert wild_dogs([(6, -0.5), (3, -5), (1, -20)]) == 5.27
assert wild_dogs([(10, 10), (13, 13), (21, 18)]) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 15, 2018