[Tutor] correcting an Active State Recipe for conversion to ordinal

spir denis.spir at free.fr
Thu Feb 4 18:42:26 CET 2010


On Thu, 4 Feb 2010 12:11:24 -0500
Serdar Tumgoren <zstumgoren at gmail.com> wrote:

> Hi folks,
> 
> A few months back I posted my first (and only) "recipe" to
> ActiveState. It was just a little function to convert an integer or
> its string representation to an ordinal value: 1 to 1st, 2 to 2nd,
> etc.
> 
> Not sure if this really qualifies as a recipe, per se, but it was a
> handy little function that I needed but couldn't find in Pythonic
> version elsewhere on the web (perhaps because it's so simple?).  The
> inspiration for the function was a similar one in Django and some Java
> code I found online. So I figured I'd share the code once I got it
> working.
> 
> I just noticed, however, that in the comments section of the
> ActiveState recipe that someone is getting incorrect results for
> certain numbers (11 and 12, specifically).
> 
> But when I use the code on my own machine it still works fine. So I
> was hoping that you all could help me "crowdsource" the issue. If you
> have the time and inclination, could you look at the code and tell me
> if and where I've gone wrong? And of course, if there's a simpler way
> to perform the conversion I'd be glad to update the recipe.  I
> certainly don't want something out in the wilds of the Web that's
> incorrect, inelegant or just plain confusing.
> 
> Here's the link to the recipe:
> 
> http://code.activestate.com/recipes/576888/
> 
> Your advice, as always, is appreciated.
> 
> Regards,
> Serdar

No time to search for the issue, but here are some trials (hole from 10 --> 19):
for i in range(21):
	print "%s\t: %s" %(i,ordinal(i))
for i in (-1,22,33,99,100,101,199,200,999,1000):
	print "%s\t: %s" %(i,ordinal(i))
==>
0	: 0th
1	: 1st
2	: 2nd
3	: 3rd
4	: 4th
5	: 5th
6	: 6th
7	: 7th
8	: 8th
9	: 9th
10	: None
11	: None
12	: None
13	: None
14	: None
15	: None
16	: None
17	: None
18	: None
19	: None
20	: 20th
-1	: -1th
22	: 22nd
33	: 33rd
99	: 99th
100	: 100th
101	: 101st
102	: 102nd
103	: 103rd
199	: 199th
200	: 200th
999	: 999th
1000	: 1000th


Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


More information about the Tutor mailing list