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 = s + ', and ' + a[-1]
class List:
def __init__(self):
self.m = []
def set(self, /, *i, a='', b=''):
self.m.append((i, a, b))
def to_string(self, x):
f = lambda: a + x[i] + b
for i, a, b in self.m:
for i in i: x[i] = f()
return ''.join(x)
x = List()
match n := len(a):
case 1: ...
case 2: x.set(0, b=' and ')
case _:
x.set(*range(n - 1), b=', ')
x.set(-1, a='and ')
s = x.to_string(a)
remainder, last = a[..-2], a[-1]
s = remainder.join(", ") + "#{' and ' unless remainder.empty?}" + last.to_s