Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Wild Dogs by frbrgeorge
from itertools import combinations
from collections import Counter
from math import *
def wild_dogs(coords):
def coll(a,b,c):
return (b[0]-a[0])*(c[1]-a[1]) == (b[1]-a[1])*(c[0]-a[0])
def dist0(a,b):
return abs((b[0]-a[0])*a[1] - a[0]*(b[1]-a[1])) / sqrt((b[0]-a[0])**2 + (b[1]-a[1])**2)
D = Counter((tuple(a),tuple(b)) for a,b,c in combinations(coords,3) if coll(a,b,c))
if D:
M = max(D.values())
return round(min(dist0(a,b) for a,b in D if D[a,b]==M),2)
else:
return round(min(dist0(a,b) for a,b in combinations(coords,2)),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!")
Dec. 25, 2018