Re: Ordinal Numbers (1st, 2nd, etc.)
Re: Ordinal Numbers (1st, 2nd, etc.)
- Subject: Re: Ordinal Numbers (1st, 2nd, etc.)
- From: Paul Skinner <email@hidden>
- Date: Fri, 17 Aug 2001 11:49:41 -0400
on 8/17/01 10:36 AM, Arthur J Knapp wrote:
>
> Date: Thu, 16 Aug 2001 20:17:21 -0400
>
> Subject: Re: Ordinal Numbers (1st, 2nd, etc.)
>
> From: Jason Bourque <email@hidden>
>
>
> on addNumericSuffix(thisNum)
>
> set the listIndex to (thisNum mod 10) + 1
>
> if thisNum is less than 11 then
>
> set the numSuffix to item listIndex of ,
>
> {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}
>
> else if thisNum is greater than 10 and thisNum is less than 20 then
>
> set the numSuffix to item listIndex of ,
>
> {"th", "th", "th", "th", "th", "th", "th", "th", "th", "th"}
>
> else
>
> set the numSuffix to item listIndex of ,
>
> {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}
>
> end if
I'm reminded of Sylvester the cat for some reason.
>
>
Good Lord, surely it isn't as complicated as all this. Am I missing
>
something?
>
>
Arthur J. Knapp
Well It's not as quite as simple as your handler 'AsOrdinal(n)' which
misses something after 20. It returns 21th.
I think what's responsible for all of these various methods is not
working out the logic required before beginning coding. Then adding code to
handle variations that are detected in testing. In this case it looks like a
very simple rule until you test it over a larger range.
I notice that after that poting Jason thought it out pretty well and
then Nigal Garvey optimized Jason's code " Just to speed it up a bit:"
in...
>
> on addNumericSuffix(thisNum)
>
> set {tens, units} to {thisNum mod 100 div 10, thisNum mod 10}
>
> if units is in {1, 2, 3} and tens is not 1 then
>
> item units of {"st", "nd", "rd"}
>
> else
>
> "th"
>
> end if
>
> return "" & thisNum & result
>
> end addNumericSuffix
That did speed things up a lot. I timed him at .26 seconds/1000 calls. I
tried my hand at this popular topic and got something very similar to his.
on ordinalSuffix(anInteger)
try
set anInteger to anInteger as integer
on error errorText
return errorText
end try
set lastCharacter to (anInteger mod 100) mod 10
if lastCharacter < 4 and (anInteger < 11 or anInteger > 20) then
set suffix to item ((lastCharacter) + 1) of {"th", "st", "nd", "rd"}
else
set suffix to "th"
end if
return suffix
end ordinalSuffix
Mine's a bit faster at .093 seconds/1000 calls despite the similarity. I
assume that the line 'units is in {1, 2, 3}' is the slow code since I don't
think it's the additional 'div'.
--
Paul Skinner