Eric Lippert's challenge “comma-quibbling”, best answer?

后端 未结 27 2213
日久生厌
日久生厌 2020-12-01 06:59

I wanted to bring this challenge to the attention of the stackoverflow community. The original problem and answers are here. BTW, if you did not follow it before, you should

27条回答
  •  一向
    一向 (楼主)
    2020-12-01 07:03

    Here as a Python one liner

    
    >>> f=lambda s:"{%s}"%", ".join(s)[::-1].replace(',','dna ',1)[::-1]
    >>> f([])
    '{}'
    >>> f(["ABC"])
    '{ABC}'
    >>> f(["ABC","DEF"])
    '{ABC and DEF}'
    >>> f(["ABC","DEF","G","H"])
    '{ABC, DEF, G and H}'
    

    This version might be easier to understand

    
    >>> f=lambda s:"{%s}"%" and ".join(s).replace(' and',',',len(s)-2)
    >>> f([])
    '{}'
    >>> f(["ABC"])
    '{ABC}'
    >>> f(["ABC","DEF"])
    '{ABC and DEF}'
    >>> f(["ABC","DEF","G","H"])
    '{ABC, DEF, G and H}'
    

提交回复
热议问题