Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import pytest
suffixes = {
'1': 'st',
'2': 'nd',
'3': 'rd',
}
def ordinal(n):
s = str(n)
if len(s) > 1 and s[-2] == '1':
return f'{n}th'
return f'{n}{suffixes.get(s[-1], "th")}'
@pytest.mark.parametrize('n, o', [
(0, '0th'),
(1, '1st'),
(2, '2nd'),
(3, '3rd'),
(4, '4th'),
(10, '10th'),
(11, '11th'),
(12, '12th'),
(13, '13th'),
(100, '100th'),
(101, '101st'),
(102, '102nd'),
(103, '103rd'),
(110, '110th'),
(111, '111th'),
(112, '112th'),
(113, '113th'),
])
def test_ordinal(n, o):
assert ordinal(n) == o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment