problem2c.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:STA141C 作者: clarkfitzg 项目源码 文件源码
def overlap_score(q1, q2):
    """
    >>> overlap_score("a b c", "a b")
    0.8

    >>> overlap_score("   ", " ")
    0
    """

    c1 = Counter(q1.split())
    c2 = Counter(q2.split())

    numerator = 0
    for word in c1:
        if word in c2:
            numerator += c1[word]
    for word in c2:
        if word in c1:
            numerator += c2[word]

    m = sum(c1.values())
    n = sum(c2.values())

    try:
        score = numerator / (m + n)
    except ZeroDivisionError:
        score = 0
    return score
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号