Created
November 30, 2025 12:20
-
-
Save moreati/91fbc621f05499491a4155a797c7a456 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.10" | |
| # /// | |
| def oxford_comma(x: list[str]) -> str: | |
| ''' | |
| >>> oxford_comma([]) | |
| '' | |
| >>> oxford_comma(['foo']) | |
| 'foo' | |
| >>> oxford_comma(['foo', 'bar']) | |
| 'foo and bar' | |
| >>> oxford_comma(['foo', 'bar', 'baz']) | |
| 'foo, bar, and baz' | |
| >>> oxford_comma(['foo', 'bar', 'baz', 'quux']) | |
| 'foo, bar, baz, and quux' | |
| ''' | |
| match x: | |
| case []: return '' | |
| case [a]: return a | |
| case [a, b]: return f'{a} and {b}' | |
| case [*rest, a]: return f'{", ".join(rest)}, and {a}' | |
| if __name__ == '__main__': | |
| import sys | |
| print(oxford_comma(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment