Created
July 24, 2018 21:52
-
-
Save tmehlinger/a093efe0602330bfbd68895292694171 to your computer and use it in GitHub Desktop.
This file contains 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
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