0
点赞
收藏
分享

微信扫一扫

Python:实现sock merchant袜子商人问题算法(附完整源码)


Python:实现sock merchant袜子商人问题算法

from collections import Counter


def sock_merchant(colors: list[int]) -> int:
"""
>>> sock_merchant([10, 20, 20, 10, 10, 30, 50, 10, 20])
3
>>> sock_merchant([1, 1, 3, 3])
2
"""
return sum(socks_by_color // 2 for socks_by_color in Counter(colors).values())


if __name__ == "__main__":
import doctest

doctest.testmod()

colors = [int(x) for x in input("Enter socks by color :").rstrip().split()]
print(f"sock_merchant({colors}) = {sock_merchant(colors)}")


举报

相关推荐

0 条评论