Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Outputting ordinal numbers (1st, 2nd, 3rd)

I would like to generate (as a return result of a function, or simply as the output of a program) the ordinal suffix of a positive integer concatenated to the number.

Samples:

1st 
2nd 
3rd 
4th 
... 
11th 
12th 
13th 
... 
20th 
21st
22nd
23rd
24th

And so on, with the suffix repeating the initial 1-10 subpattern every 10 until 100, where the pattern ultimately starts over.

The input would be the number and the output the ordinal string as shown above.

What is the smallest algorithm for this?

Answer*

Draft saved
Draft discarded
Cancel
4
  • \$\begingroup\$ "31th"?­­­­­­­­ \$\endgroup\$ Commented May 3, 2018 at 17:49
  • \$\begingroup\$ ⍕,{2↑'thstndrd'↓⍨2×⊃⍵⌽∊1 0 8\⊂10↑⍳4} \$\endgroup\$ Commented May 3, 2018 at 18:23
  • \$\begingroup\$ sorry, I forgot to mention ⎕io←0. I can see you guessed that, but there are a few 1,2,3,4,0,0... that should be 0,1,2,3,0,0... \$\endgroup\$ Commented May 3, 2018 at 19:35
  • \$\begingroup\$ @ngn Fixed. And happens to work for 0 too! \$\endgroup\$ Commented May 3, 2018 at 20:26

AltStyle によって変換されたページ (->オリジナル) /