Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Wild Dogs by neshta85
from collections import Counter
def combinations(coords):
i = 1
for c in coords:
for j in coords[i:]:
yield [c, j]
i += 1
def linear_equation(x, y):
if x[0] != y[0]:
k = (y[1] - x[1]) / (y[0] - x[0])
b = x[1] - k * x[0]
return k, b
def wild_dogs(coords):
linear_count = (Counter([linear_equation(c[0], c[1]) for c in combinations(coords)]).most_common())
result = []
for x in linear_count:
if x[1] == linear_count[0][1]:
k, b = x[0]
result.append(round(abs(b) / (k * k + 1) ** 0.5, 2))
return min(result)
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!")
Jan. 18, 2019