Exercism: Darts (Python)

根据飞镖的坐标来算分数,很简单。看了几个其他人的答案,几个启发:


1. 算坐标可以直接使用math.hypot。
2. math.sqrt可以使用**0.5或者math.pow来做。
3. 可以不使用sqrt来直接使用1,25,100。
4. 大部分人都用if来判断,很少人会extract到一个list里。

import math
score_map = [
    (1.0, 10),
    (5.0, 5),
    (10.0, 1), 
]
def score(x, y):
    pos=math.sqrt((x**2 + y**2))
    for p, score in score_map: 
        if pos <= p:
            return score
    return 0


评论

此博客中的热门博文

拜厄练习曲44, 45

Exercism: Word Count (Python)

pygame Alevel 项目