N'th: Difference between revisions
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
m (→{{header|REXX}}: added some comments to the REXX statements.) |
||
Line 75: | Line 75: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX pgm displays some ranges of numbers with (th/nd/rd/th) suffixes. */ |
<lang rexx>/*REXX pgm displays some ranges of numbers with (th/nd/rd/th) suffixes. */ |
||
bot= 0; top= 25; call tell bot,top |
bot= 0; top= 25; call tell bot,top /*show the 1st range of numbers. */ |
||
bot= 250; top=265; call tell bot,top |
bot= 250; top=265; call tell bot,top /* " " 2nd " " " */ |
||
bot=1000; top=1025; call tell bot,top |
bot=1000; top=1025; call tell bot,top /* " " 3rd " " " */ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*───────────────────────────────────TELL subroutine────────────────────*/ |
/*───────────────────────────────────TELL subroutine────────────────────*/ |
||
tell: procedure; parse arg L,H; $= |
tell: procedure; parse arg L,H; $= /*get Low & High #s, nullify list*/ |
||
do j=L to H |
do j=L to H /*process the range, low───►high.*/ |
||
$=$ j"'" || th(j) |
$=$ j"'" || th(j) /*append the Nth number to list*/ |
||
end /*j*/ |
end /*j*/ /* [↑] $ has a leading blank. */ |
||
say 'numbers from ' L " to " H ' (inclusive):' |
say 'numbers from ' L " to " H ' (inclusive):' /*display the title.*/ |
||
say strip($) /*display the list. */ |
|||
say strip($) |
|||
say /*display the sep. */ |
|||
say |
|||
return /*return to invoker.*/ |
|||
return |
|||
/*───────────────────────────────────TH subroutine──────────────────────*/ |
/*───────────────────────────────────TH subroutine──────────────────────*/ |
||
th: procedure; parse arg x; x=abs(x) |
th: procedure; parse arg x; x=abs(x) |
Revision as of 21:14, 16 March 2014
Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.
Example returns would include 1'st 2'nd 3'rd 11'th 1001'st
- Task
Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs:
0..25, 250..265, 1000..1025
D
<lang d>import std.stdio, std.string, std.range, std.algorithm;
string nth(in uint n) pure {
static immutable suffix = "th st nd rd th th th th th th".split; return "%d'%s".format(n, (n % 100 <= 10 || n % 100 > 20) ? suffix[n % 10] : "th");
}
void main() {
foreach (r; [iota(26), iota(250, 266), iota(1000, 1026)]) writefln("%-(%s %)", r.map!nth);
}</lang>
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
J
Implementation:
<lang J>suf=: (;:'th st nd rd th'){::~4<.10 10(* 1&~:)~/@#:]
nth=: [:;:inv (":,',suf)each</lang>
Task:
<lang J> thru=: <./ + i.@(+ *)@-~
nth 0 thru 25
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th
nth 250 thru 265
250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th
nth 1000 thru 1025
1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th</lang>
Note the cute colorization caused by the interaction between this task's imposition of the quote character and this wiki's syntax support.
Perl 6
<lang perl6>my %irregulars = <1 st 2 nd 3 rd>, (11..13 X=> 'th');
sub N'th ($n) { "$n'" ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) }
say .list».&N'th.join: ' ' for [^26], [250..265], [1000..1025];</lang>
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Python
<lang python>_suffix = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
def nth(n):
return "%i'%s" % (n, _suffix[n%10] if n % 100 <= 10 or n % 100 > 20 else 'th')
if __name__ == '__main__':
for j in range(0,1001, 250): print(' '.join(nth(i) for i in list(range(j, j+25))))</lang>
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 500'th 501'st 502'nd 503'rd 504'th 505'th 506'th 507'th 508'th 509'th 510'th 511'th 512'th 513'th 514'th 515'th 516'th 517'th 518'th 519'th 520'th 521'st 522'nd 523'rd 524'th 750'th 751'st 752'nd 753'rd 754'th 755'th 756'th 757'th 758'th 759'th 760'th 761'st 762'nd 763'rd 764'th 765'th 766'th 767'th 768'th 769'th 770'th 771'st 772'nd 773'rd 774'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th
REXX
<lang rexx>/*REXX pgm displays some ranges of numbers with (th/nd/rd/th) suffixes. */ bot= 0; top= 25; call tell bot,top /*show the 1st range of numbers. */ bot= 250; top=265; call tell bot,top /* " " 2nd " " " */ bot=1000; top=1025; call tell bot,top /* " " 3rd " " " */ exit /*stick a fork in it, we're done.*/ /*───────────────────────────────────TELL subroutine────────────────────*/ tell: procedure; parse arg L,H; $= /*get Low & High #s, nullify list*/
do j=L to H /*process the range, low───►high.*/ $=$ j"'" || th(j) /*append the Nth number to list*/ end /*j*/ /* [↑] $ has a leading blank. */
say 'numbers from ' L " to " H ' (inclusive):' /*display the title.*/ say strip($) /*display the list. */ say /*display the sep. */ return /*return to invoker.*/ /*───────────────────────────────────TH subroutine──────────────────────*/ th: procedure; parse arg x; x=abs(x)
return word('th st nd rd', 1+x//10*(x//100%10\==1)*(x//10<4))</lang>
output using the default inputs:
numbers from 0 to 25 (inclusive): 0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th numbers from 250 to 265 (inclusive): 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th numbers from 1000 to 1025 (inclusive): 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th
Tcl
<lang tcl>proc ordinal {n} {
if {$n%100<10 || $n%100>20} {
set suff [lindex {th st nd rd th th th th th th} [expr {$n % 10}]]
} else {
set suff th
} return "$n'$suff"
}
foreach start {0 250 1000} {
for {set n $start; set l {}} {$n<=$start+25} {incr n} {
lappend l [ordinal $n]
} puts $l
}</lang>
- Output:
0'th 1'st 2'nd 3'rd 4'th 5'th 6'th 7'th 8'th 9'th 10'th 11'th 12'th 13'th 14'th 15'th 16'th 17'th 18'th 19'th 20'th 21'st 22'nd 23'rd 24'th 25'th 250'th 251'st 252'nd 253'rd 254'th 255'th 256'th 257'th 258'th 259'th 260'th 261'st 262'nd 263'rd 264'th 265'th 266'th 267'th 268'th 269'th 270'th 271'st 272'nd 273'rd 274'th 275'th 1000'th 1001'st 1002'nd 1003'rd 1004'th 1005'th 1006'th 1007'th 1008'th 1009'th 1010'th 1011'th 1012'th 1013'th 1014'th 1015'th 1016'th 1017'th 1018'th 1019'th 1020'th 1021'st 1022'nd 1023'rd 1024'th 1025'th