- 思路: 方程 y = k * x + b 距离
def wild_dogs(coords):
# 所有点 22 得到 (k, b)
list_1=[]
for x_1, y_1 in coords:
for x_2, y_2 in coords:
if x_1!= x_2:
k=(y_2- y_1)/(x_2- x_1)
b=y_1-k* x_1
list_1.append((k, b))
# 统计出现次数
from collections import Counter
k_b_count =(Counter(list_1).most_common())
# 如果最多出现次数相同, 比较距离
list_2=[]
for x in k_b_count:
if x[1]==k_b_count[0][1]:
k, b=x[0]
list_2.append(round(abs(b)/(k*k+1)**0.5, 2))
return min(list_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!")
- 统计列表次数
# 2.1
words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
from collections import Counter
word_counts = Counter(words)
# 出现频率最高的3个单词
top_three = word_counts.most_common(3)
print(top_three)
# Outputs [('eyes', 8), ('the', 5), ('look', 4)]
2.2
#保留2 位有效数字
a=13.949999999999999
round(a, 2)
13.95