Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ordinal(s):
- """Gets a number from a standard ordinal.
- >>> ordinal("15th")
- 15
- >>> ordinal("34st")
- None
- >>> ordinal("foobar")
- None
- """
- if not s[:-2].isdigit():
- return None
- value = int(s[:-2])
- v = int(s[-3])
- th = s[-2:]
- if v is 1 and th == 'st' or v is 2 and th == 'nd' or v is 3 and th == 'rd' or th == 'th':
- return value
- return None
- print ordinal("213rd")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement