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

后端 未结 27 2285
日久生厌
日久生厌 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:12

    I'm a fan of the serial comma: I eat, shoot, and leave.

    I continually need a solution to this problem and have solved it in 3 languages (though not C#). I would adapt the following solution (in Lua, does not wrap answer in curly braces) by writing a concat method that works on any IEnumerable:

    function commafy(t, andword)
      andword = andword or 'and'
      local n = #t -- number of elements in the numeration
      if n == 1 then
        return t[1]
      elseif n == 2 then
        return concat { t[1], ' ', andword, ' ', t[2] }
      else
        local last = t[n]
        t[n] = andword .. ' ' .. t[n]
        local answer = concat(t, ', ')
        t[n] = last
        return answer
      end
    end
    

提交回复
热议问题