Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
match len(a):
case 1:
s = a[0]
case 2:
s = ' and '.join(a)
case _:
s = ', '.join(a[:-1])
s += ', and ' + a[-1]
class ListFormatter(dict):
def set(self, *i, left='', right=''):
self[i] = left, right
def format_list(self, array):
a = list(map(str, array))
for key, values in self.items():
for i in key:
a[i] = a[i].join(values)
return ''.join(a)
x = ListFormatter()
match n := len(a):
case 1: ...
case 2: x.set(0, right=' and ')
case _:
x.set(*range(n - 1), right=', ')
x.set(-1, left='and ')
s = x.format_list(a)
remainder, last = a[..-2], a[-1]
s = remainder.join(", ") + "#{' and ' unless remainder.empty?}" + last.to_s