Advertisement
Vermiculus

Untitled

Apr 9th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.38 KB | None | 0 0
  1. def ordinal(s):
  2.     """Gets a number from a standard ordinal.
  3.    
  4.     >>> ordinal("15th")
  5.     15
  6.    
  7.     >>> ordinal("34st")
  8.     None
  9.    
  10.     >>> ordinal("foobar")
  11.     None
  12.     """
  13.     if not s[:-2].isdigit():
  14.         return None
  15.     value = int(s[:-2])
  16.     v = int(s[-3])
  17.     th = s[-2:]
  18.     if v is 1 and th == 'st' or v is 2 and th == 'nd' or v is 3 and th == 'rd' or th == 'th':
  19.         return value
  20.    
  21.     return None
  22.  
  23. print ordinal("213rd")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement