Wild Dogs [直线 count 有效数字]

  1. 思路: 方程 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!")

  1. 统计列表次数
# 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值