Skip to content

Instantly share code, notes, and snippets.

@tmehlinger
Created July 24, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmehlinger/a093efe0602330bfbd68895292694171 to your computer and use it in GitHub Desktop.
Save tmehlinger/a093efe0602330bfbd68895292694171 to your computer and use it in GitHub Desktop.
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