Formats an ordinal. Doesn't handle negative numbers. >>> nthstr(1) '1st' >>> nthstr(0) '0th' >>> [nthstr(x) for x in [2, 3, 4, 5, 10, 11, 12, 13, 14, 15]] ['2nd', '3rd', '4th', '5th', '10th', '11th', '12th', '13th', '14th', '15th'] >>
(n)
| 1078 | |
| 1079 | |
| 1080 | def nthstr(n): |
| 1081 | """ |
| 1082 | Formats an ordinal. |
| 1083 | Doesn't handle negative numbers. |
| 1084 | |
| 1085 | >>> nthstr(1) |
| 1086 | '1st' |
| 1087 | >>> nthstr(0) |
| 1088 | '0th' |
| 1089 | >>> [nthstr(x) for x in [2, 3, 4, 5, 10, 11, 12, 13, 14, 15]] |
| 1090 | ['2nd', '3rd', '4th', '5th', '10th', '11th', '12th', '13th', '14th', '15th'] |
| 1091 | >>> [nthstr(x) for x in [91, 92, 93, 94, 99, 100, 101, 102]] |
| 1092 | ['91st', '92nd', '93rd', '94th', '99th', '100th', '101st', '102nd'] |
| 1093 | >>> [nthstr(x) for x in [111, 112, 113, 114, 115]] |
| 1094 | ['111th', '112th', '113th', '114th', '115th'] |
| 1095 | |
| 1096 | """ |
| 1097 | |
| 1098 | assert n >= 0 |
| 1099 | if n % 100 in [11, 12, 13]: |
| 1100 | return "%sth" % n |
| 1101 | return {1: "%sst", 2: "%snd", 3: "%srd"}.get(n % 10, "%sth") % n |
| 1102 | |
| 1103 | |
| 1104 | def cond(predicate, consequence, alternative=None): |