Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Wild Dogs by Vasily__Chibilyaev
from itertools import combinations
def wild_dogs(coords):
killed = 0
max_killed = 0
shots = set()
for a, b in combinations(coords, r = 2):
A = a[1] - b[1]
B = b[0] - a[0]
C = a[0] * b[1] - a[1] * b[0]
killed = sum(1 for c in coords if A * c[0] + B * c[1] + C == 0)
max_killed = max(killed, max_killed)
shots.add(((A, B, C), killed))
best_shots = {s for s, k in shots if k == max_killed}
dist = lambda a, b, c : abs(c) / (a**2 + b**2)**.5
min_dist = round(min(dist(*s) for s in best_shots), 2)
return int(min_dist) if int(min_dist) == min_dist else min_dist
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!")
Sept. 15, 2018
Comments: