Skip to content

Instantly share code, notes, and snippets.

@lavafroth
Created November 14, 2025 13:15
Show Gist options
  • Select an option

  • Save lavafroth/1c8471988e3f185c6757e3674fcf2a1b to your computer and use it in GitHub Desktop.

Select an option

Save lavafroth/1c8471988e3f185c6757e3674fcf2a1b to your computer and use it in GitHub Desktop.
def test(fn):
print(fn(tuple(map(str, range(0)))))
# assert fn(tuple(map(str, range(0)))) == ''
print(fn(tuple(map(str, range(2)))))
# assert fn(tuple(map(str, range(2)))) == '0 and 1'
print(fn(tuple(map(str, range(4)))))
# assert fn(tuple(map(str, range(4)))) == '0, 1, 2, and 3'
print(fn(tuple(map(str, range(10)))))
# assert fn(tuple(map(str, range(10)))) == '0, 1, 2, 3, 4, 5, 6, 7, 8, and 9'
comma = lambda x: ", ".join(x[:-1]) + (x[-1:] and (x[:-2] and ", and " or " and ") + x[-1] or "")
test(comma)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment