Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

5
Task/Nth/00-META.yaml Normal file
View file

@ -0,0 +1,5 @@
---
category:
- String manipulation
from: http://rosettacode.org/wiki/N'th
note: Ordinal numbers

15
Task/Nth/00-TASK.txt Normal file
View file

@ -0,0 +1,15 @@
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 [[wp:Ordinal number (linguistics)|ordinal suffix]].
;Example:
Returns would include <code>1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th</code>
;Task:
Use your routine to show here the output for ''at least'' the following (inclusive) ranges of integer inputs:
<code>0..25, 250..265, 1000..1025</code>
'''Note:''' apostrophes are now ''optional'' to allow correct apostrophe-less English.
<br><br>

7
Task/Nth/11l/nth.11l Normal file
View file

@ -0,0 +1,7 @@
V _suffix = [th, st, nd, rd, th, th, th, th, th, th]
F nth(n)
R #.'#..format(n, I n % 100 <= 10 | n % 100 > 20 {:_suffix[n % 10]} E th)
L(j) (0..1000).step(250)
print_elements(Array(j.+25).map(i -> nth(i)))

View file

@ -0,0 +1,59 @@
Nth:
MOVEQ #1,D1
.loop:
MOVE.L D1,-(SP)
MOVE.L D0,-(SP)
MOVE.B (2,SP),D0
JSR printhex
MOVE.B (3,SP),D0
JSR printhex
MOVE.L (SP)+,D0
MOVE.L D0,-(SP)
AND.W #$00FF,D0
;HANDLE SPECIAL CASES
CMP.B #$11,D0
BEQ .fourth
CMP.B #$12,D0
BEQ .fourth
CMP.B #$13,D0
BEQ .fourth
;HANDLE THE REST
AND.W #$000F,D0
CMP.B #1,D0
BEQ .first
CMP.B #2,D0
BEQ .second
CMP.B #3,D0
BEQ .third
.fourth:
LEA th,A3
bra .print
.third:
LEA rd,a3
bra .print
.second:
LEA nd,a3
bra .print
.first:
LEA est,a3
.print:
JSR PrintString
JSR newline
MOVE.L (SP)+,d0
MOVE.L (SP)+,d1
ABCD D1,D0
DBRA D7,.loop
rts
th:
dc.b "th",255
even
est:
dc.b "st",255
even
nd:
dc.b "nd",255
even
rd:
dc.b "rd",255
even

View file

@ -0,0 +1,13 @@
MOVE.w #0,D0
MOVE.W #25,D7
JSR Nth
MOVE.w #$250,D0 ;since we're working with binary-coded decimal, this number is stored as hex.
MOVE.W #15,D7
JSR Nth
MOVE.w #$1000,D0 ;since we're working with binary-coded decimal, this number is stored as hex.
MOVE.W #25,D7
JSR Nth
jmp * ;stop the cpu - we're done.

View file

@ -0,0 +1,77 @@
org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Given a 16-bit unsigned integer in HL, return a string
;; consisting of its ASCII representation plus an ordinal
;; suffix in HL.
nth: lxi b,-10 ; Divisor for digit loop
push h ; Push integer to stack
lxi h,nthsfx ; Pointer to end of digit area
xthl ; Swap digit pointer with integer
nthdigit: lxi d,-1 ; Get current digit - DE holds quotient
nthdiv10: inx d ; Increment quotient,
dad b ; subtract 10 from integer,
jc nthdiv10 ; Keep going until HL<0
mvi a,'0'+10 ; Calculate ASCII value for digit
add l ; Modulus is in (H)L
xthl ; Swap integer with pointer
dcx h ; Point to one digit further left
mov m,a ; Store the digit
xthl ; Swap pointer with integer
xchg ; Put quotient in HL
mov a,h ; Is it zero?
ora l
jnz nthdigit ; If not, go calculate next digit
mvi e,0 ; Default suffix is 'th'.
lxi h,nthnum+3 ; Look at tens digit
mov a,m
cpi '1' ; Is it '1'?
jz nthsetsfx ; Then it is always 'th'.
inx h ; Look at zeroes digit
mov a,m
sui '0' ; Subtract ASCII '0'
cpi 4 ; 4 or higher?
jnc nthsetsfx ; Then it is always 'th'.
rlc ; Otherwise, suffix is at N*2+nthord
mov e,a
nthsetsfx: mvi d,0 ; Look up suffix in list
lxi h,nthord
dad d ; Pointer to suffix in HL
lxi d,nthsfx ; Pointer to space for suffix in DE
mov a,m ; Get first letter of suffix
stax d ; Store it in output
inx h ; Increment both pointers
inx d
mov a,m ; Get second letter of suffix
stax d ; Store it in output
pop h ; Return pointer to leftmost digit
ret
nthord: db 'thstndrd' ; Ordinal suffixes
nthnum: db '*****' ; Room for digits
nthsfx: db '**$' ; Room for suffix
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
demo: ;; Demo code
lxi h,0 ; Starting at 0...
mvi b,26 ; ...print 26 numbers [0..25]
call printnums
lxi h,250 ; Starting at 250...
mvi b,16 ; ...print 16 numbers [250..265]
call printnums
lxi h,1000 ; Starting at 1000...
mvi b,26 ; ...print 26 numbers [1000..1025]
;; Print values from HL up to HL+B
printnums: push h ; Keep number
push b ; Keep counter
call nth ; Get string for current HL
xchg ; Put it in DE
mvi c,9 ; CP/M print string
call 5
mvi e,' ' ; Separate numbers with spaces
mvi c,2 ; CP/M print character
call 5
pop b ; Restore counter
pop h ; Restore number
inx h ; Increment number
dcr b ; Decrement counter
jnz printnums ; If not zero, print next number
ret

View file

@ -0,0 +1,58 @@
bits 16
cpu 8086
segment .text
org 100h
jmp demo
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Given a number in AX, return string with ordinal suffix
;; in DX.
nth: mov cx,10 ; Divisor
mov bx,.sfx ; Pointer to end of number
.digit: xor dx,dx ; Zero DX
div cx ; AX = DX:AX/10; DX=remainder
add dl,'0' ; Make digit
dec bx ; Back up the pointer
mov [bx],dl ; Store digit
and ax,ax ; Done yet? (AX=0?)
jnz .digit ; If not, get next digit
mov dx,bx ; Keep string pointer in DX
xor bx,bx ; Default suffix is 'th'.
cmp byte [.num+3],'1' ; Is the tens digit '1'?
je .setsfx ; Then the suffix is 'th'.
mov cl,[.num+4] ; Get the ones digit
cmp cl,'4' ; Is it '4' or higher?
jae .setsfx ; Then the suffix is 'th'.
mov bl,cl
sub bl,'0' ; Calculate offset.
shl bl,1 ; [0..3]*2 + .ord
.setsfx: mov ax,[bx+.ordsfx] ; Set suffix
mov [.sfx],ax
ret
.ordsfx: db 'thstndrd'
.num: db '*****'
.sfx: db '**$'
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Print numbers.
demo: mov ax,0 ; Starting at 0...
mov bl,26 ; ...print 26 numbers [0..25]
call printn
mov ax,250 ; Starting at 250...
mov bl,16 ; ...print 16 numbers [250..265]
call printn
mov ax,1000 ; Starting at 1000...
mov bl,26 ; ...print 26 numbers [1000..1025]
;; Print BL numbers starting at AX
printn: push ax ; Keep number
push bx ; Keep counter
call nth ; Get string for current AX
mov ah,9 ; MS-DOS print string
int 21h
mov dl,' ' ; Separate numbers by spaces
mov ah,2 ; MS-DOS print character
int 21h
pop bx ; Restore counter
pop ax ; Restore number
inc ax ; Next number
dec bl ; Are we done yet?
jnz printn
ret

49
Task/Nth/ALGOL-68/nth.alg Normal file
View file

@ -0,0 +1,49 @@
# PROC to suffix a number with st, nd, rd or th as appropriate #
PROC nth = ( INT number )STRING:
BEGIN
INT number mod 100 = number MOD 100;
# RESULT #
whole( number, 0 )
+ IF number mod 100 >= 10 AND number mod 100 <= 20
THEN
# numbers in the range 10 .. 20 always have "th" #
"th"
ELSE
# not in the range 10 .. 20, suffix is st, nd, rd or th #
# depending on the final digit #
CASE number MOD 10
IN # 1 # "st"
, # 2 # "nd"
, # 3 # "rd"
OUT "th"
ESAC
FI
END; # nth #
# PROC to test nth, displays nth for all numbers in the range from .. to #
PROC test nth = ( INT from, INT to )VOID:
BEGIN
INT test count := 0;
FOR test value FROM from TO to
DO
STRING test result = nth( test value );
print( ( " "[ 1 : 8 - UPB test result ], nth( test value ) ) );
test count +:= 1;
IF test count MOD 8 = 0
THEN
print( ( newline ) )
FI
OD;
print( ( newline ) )
END; # test nth #
main: (
test nth( 0, 25 );
test nth( 250, 265 );
test nth( 1000, 1025 )
)

42
Task/Nth/ALGOL-W/nth.alg Normal file
View file

@ -0,0 +1,42 @@
begin % suffix number with st, nd, rd or th as appropriate %
string(2) procedure ordinalSuffix ( integer value number ) ;
begin
integer numberRem100;
numberRem100 := number rem 100;
if numberRem100 >= 10 and numberRem100 <= 20 then begin
% numbers in the range 10 .. 20 always have "th" %
"th"
end
else begin
% not in the range 10 .. 20, suffix is st, nd, rd or th %
% depending on the final digit %
integer numberRem10;
numberRem10 := number rem 10;
if numberRem10 = 1 then "st"
else if numberRem10 = 2 then "nd"
else if numberRem10 = 3 then "rd"
else "th"
end if_numberRem100_in_10_to_20__
end ordinalSuffix ;
% tests ordinalSuffix, displays the suffix for all numbers in from .. to %
procedure testSuffix ( integer value from, to ) ;
begin
integer count;
count := 0;
for testValue := from until to do begin
writeon( i_w := 4, s_w := 0, " ", testValue, ordinalSuffix( testValue ) );
count := count + 1;
if count rem 8 = 0 then write()
end for_testValue ;
if count rem 8 not = 0 then write();
write()
end testSuffix ;
begin % task %
testSuffix( 0, 25 );
testSuffix( 250, 265 );
testSuffix( 1000, 1025 )
end
end.

5
Task/Nth/APL/nth.apl Normal file
View file

@ -0,0 +1,5 @@
nth{
sfx4 2'stndrdth'
tens(10<100|)20>100|
(),sfx[(4×tens)(3)10|;]
}

32
Task/Nth/AWK/nth.awk Normal file
View file

@ -0,0 +1,32 @@
# syntax: GAWK -f NTH.AWK
BEGIN {
prn(0,25)
prn(250,265)
prn(1000,1025)
exit(0)
}
function prn(start,stop, i) {
printf("%d-%d: ",start,stop)
for (i=start; i<=stop; i++) {
printf("%d%s ",i,nth(i))
}
printf("\n")
}
function nth(yearday, nthday) {
if (yearday ~ /1[1-3]$/) { # 11th,12th,13th
nthday = "th"
}
else if (yearday ~ /1$/) { # 1st,21st,31st,etc.
nthday = "st"
}
else if (yearday ~ /2$/) { # 2nd,22nd,32nd,etc.
nthday = "nd"
}
else if (yearday ~ /3$/) { # 3rd,23rd,33rd,etc.
nthday = "rd"
}
else if (yearday ~ /[0456789]$/) { # 4th-10th,20th,24th-30th,etc.
nthday = "th"
}
return(nthday)
}

View file

@ -0,0 +1,39 @@
PROC Nth(CARD val,CHAR ARRAY s)
CHAR ARRAY sfx
BYTE d
StrC(val,s)
s(0)=s(0)+1
s(s(0))=''
d=val MOD 100
IF d>10 AND d<14 THEN
sfx="th"
ELSE
d=val MOD 10
IF d=1 THEN sfx="st"
ELSEIF d=2 THEN sfx="nd"
ELSEIF d=3 THEN sfx="rd"
ELSE sfx="th"
FI
FI
s(0)=s(0)+2
SAssign(s,sfx,s(0)-1,s(0)+1)
RETURN
PROC Main()
CARD ARRAY n=[0 250 1000]
CHAR ARRAY s(10)
CARD i,j
FOR i=0 TO 2
DO
FOR j=n(i) TO n(i)+25
DO
Nth(j,s)
PrintF("%S ",s)
OD
PutE() PutE()
OD
RETURN

26
Task/Nth/Ada/nth.ada Normal file
View file

@ -0,0 +1,26 @@
with Ada.Text_IO;
procedure Nth is
function Suffix(N: Natural) return String is
begin
if N mod 10 = 1 and then N mod 100 /= 11 then return "st";
elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
else return "th";
end if;
end Suffix;
procedure Print_Images(From, To: Natural) is
begin
for I in From .. To loop
Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
end loop;
Ada.Text_IO.New_Line;
end Print_Images;
begin
Print_Images( 0, 25);
Print_Images( 250, 265);
Print_Images(1000, 1025);
end Nth;

View file

@ -0,0 +1,82 @@
-- ORDINAL STRINGS -----------------------------------------------------------
-- ordinalString :: Int -> String
on ordinalString(n)
(n as string) & ordinalSuffix(n)
end ordinalString
-- ordinalSuffix :: Int -> String
on ordinalSuffix(n)
set modHundred to n mod 100
if (11 modHundred) and (13 modHundred) then
"th"
else
item ((n mod 10) + 1) of ¬
{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}
end if
end ordinalSuffix
-- TEST ----------------------------------------------------------------------
on run
-- showOrdinals :: [Int] -> [String]
script showOrdinals
on |λ|(lstInt)
map(ordinalString, lstInt)
end |λ|
end script
map(showOrdinals, ¬
map(uncurry(enumFromTo), ¬
[[0, 25], [250, 265], [1000, 1025]]))
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- uncurry :: Handler (a -> b -> c) -> Script |λ| ((a, b) -> c)
on uncurry(f)
script
on |λ|(xy)
set {x, y} to xy
mReturn(f)'s |λ|(x, y)
end |λ|
end script
end uncurry

View file

@ -0,0 +1,19 @@
0 OP = 1
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT
40 END
100 GOSUB 200"NTH
110 PRINT NTH$ " ";
120 RETURN
200 M1 = N - INT(N / 10) * 10
210 M2 = N - INT(N / 100) * 100
220 NTH$ = "TH"
230 IF M1 = 1 AND M2 <> 11 THEN NTH$ = "ST"
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND"
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD"
260 IF NOT OP THEN NTH$ = "'" + NTH$
270 NTH$ = STR$(N) + NTH$
280 RETURN

View file

@ -0,0 +1,14 @@
suffixes: ["th" "st" "nd" "rd" "th" "th" "th" "th" "th" "th"]
nth: function [n][
if? or? 100 >= n%100
20 < n%100
-> (to :string n) ++ "'" ++ suffixes\[n%10]
else -> (to :string n) ++ "'th"
]
loop range.step:250 0 1000 'j [
loop j..j+24 'i ->
prints (nth i)++" "
print ""
]

View file

@ -0,0 +1,16 @@
for k, v in [[0, 25], [250, 265], [1000, 1025]] {
while v[1] <= v[2] {
Out .= Ordinal(v[1]) " "
v[1]++
}
Out .= "`n"
}
MsgBox, % Out
Ordinal(n) {
s2 := Mod(n, 100)
if (s2 > 10 && s2 < 14)
return n "th"
s1 := Mod(n, 10)
return n (s1 = 1 ? "st" : s1 = 2 ? "nd" : s1 = 3 ? "rd" : "th")
}

View file

@ -0,0 +1,28 @@
function ordinal(n)
ns$ = string(n)
begin case
case right(ns$, 1) = "1"
if right(ns$, 2) = "11" then return ns$ + "th"
return ns$ + "st"
case right(ns$, 1) = "2"
if right(ns$, 2) = "12" then return ns$ + "th"
return ns$ + "nd"
case right(ns$, 1) = "3"
if right(ns$, 2) = "13" then return ns$ + "th"
return ns$ + "rd"
else
return ns$ + "th"
end case
end function
subroutine imprimeOrdinal(a, b)
for i = a to b
print ordinal(i); " ";
next i
print
end subroutine
call imprimeOrdinal (0, 25)
call imprimeOrdinal (250, 265)
call imprimeOrdinal (1000, 1025)
end

View file

@ -0,0 +1,16 @@
PROCNth( 0, 25)
PROCNth( 250, 265)
PROCNth(1000,1025)
END
DEF PROCNth(s%,e%)
LOCAL i%,suff$
FOR i%=s% TO e%
suff$="th"
IF i% MOD 10 = 1 AND i% MOD 100 <> 11 suff$="st"
IF i% MOD 10 = 2 AND i% MOD 100 <> 12 suff$="nd"
IF i% MOD 10 = 3 AND i% MOD 100 <> 13 suff$="rd"
PRINT STR$i%+suff$+" ";
NEXT
PRINT
ENDPROC

40
Task/Nth/BCPL/nth.bcpl Normal file
View file

@ -0,0 +1,40 @@
get "libhdr"
// Generate ASCII string of number n with ordinal suffix
// The string is stored at v.
let nth(n, v) = valof
$( let sfx = "thstndrd"
let c, s = 0, ?
// Generate digits
$( c := c + 1
v%c := n rem 10 + '0'
n := n / 10
$) repeatuntil n = 0
// Put digits in correct order
for i = 1 to c/2 do
$( let d = v%i
v%i := v%(c+1-i)
v%(c+1-i) := d
$)
// The length of the string is the amount of digits + 2
v%0 := c+2;
// Figure out the proper suffix from the last two digits
test v%(c-1)='1' | v%c>'3'
then s := 0
else s := 2*(v%c - '0')
v%(c+1) := sfx%(s+1)
v%(c+2) := sfx%(s+2)
resultis v
$)
let start() be
$( let buf = vec 10
for i = 0 to 25 do writef("%S*N", nth(i, buf))
for i = 250 to 265 do writef("%S*N", nth(i, buf))
for i = 1000 to 1025 do writef("%S*N", nth(i, buf))
$)

6
Task/Nth/BQN/nth.bqn Normal file
View file

@ -0,0 +1,6 @@
Nth {
sfx "th","st","nd","rd"
(•Fmt 𝕩)sfx˜ (110|𝕩÷10)×(310|𝕩)×10|𝕩
}
4 Nth¨ (26) (250+16) (1000+26)

28
Task/Nth/BaCon/nth.bacon Normal file
View file

@ -0,0 +1,28 @@
' Nth (sans apostrophes)
FUNCTION nth$(NUMBER n) TYPE STRING
LOCAL suffix
IF n < 0 THEN RETURN STR$(n)
IF MOD(n, 100) >= 11 AND MOD(n, 100) <= 13 THEN
suffix = "th"
ELSE
suffix = MID$("thstndrdthththththth", MOD(n, 10) * 2 + 1, 2)
ENDIF
RETURN CONCAT$(STR$(n), suffix)
END FUNCTION
' Test a few ranges
FOR i = 1 TO 4
READ first, last
per = 1
FOR n = first TO last
PRINT nth$(n) FORMAT "%s "
' limit to 10 entries per line
IF per = 10 OR n = last THEN
per = 1
PRINT
ELSE
INCR per
ENDIF
NEXT
NEXT
DATA 0, 25, 250, 265, 1000, 1025, -20, -11

28
Task/Nth/Babel/nth.pb Normal file
View file

@ -0,0 +1,28 @@
((irregular ("st" "nd" "rd"))
(main
{(0 250 1000)
{ test ! "\n" << }
each})
(test {
<-
{iter 1 - -> dup <- + ordinalify ! <<
{iter 10 %} {" "} {"\n"} ifte << }
26 times})
(ordinalify {
<-
{{ -> dup <- 100 % 10 cugt } !
{ -> dup <- 100 % 14 cult } !
and not
{ -> dup <- 10 % 0 cugt } !
{ -> dup <- 10 % 4 cult } !
and
and}
{ -> dup
<- %d "'"
irregular -> 10 % 1 - ith
. . }
{ -> %d "'th" . }
ifte }))

View file

@ -0,0 +1,24 @@
@echo off
::Main thing...
call :Nth 0 25
call :Nth 250 265
call :Nth 1000 1025
pause
exit /b
::The subroutine
:Nth <lbound> <ubound>
setlocal enabledelayedexpansion
for /l %%n in (%~1,1,%~2) do (
set curr_num=%%n
if !curr_num:~-2!==11 (set "out=%%nth"
) else if !curr_num:~-2!==12 (set "out=%%nth"
) else if !curr_num:~-2!==13 (set "out=%%nth"
) else if !curr_num:~-1!==1 (set "out=%%nst"
) else if !curr_num:~-1!==2 (set "out=%%nnd"
) else if !curr_num:~-1!==3 (set "out=%%nrd"
) else (set "out=%%nth")
set "range_output=!range_output! !out!"
)
echo."!range_output:~1!"
goto :EOF

12
Task/Nth/Befunge/nth.bf Normal file
View file

@ -0,0 +1,12 @@
0>55*:>1-\:0\`!v
#v$#$<^:\+*8"}"_
>35*:>1-\:0\`!v
#v$#$<^:\+*2"}"_
5< v$_v#!::-<0*5
@v <,*>#81#4^# _
>>:0\>:55+%68*v:
tsnr |:/+ 55\+<,
htdd >$>:#,_$:vg
v"d"\*!`3:%+55<9
>%55+/1-!!*:8g,^

41
Task/Nth/C++/nth.cpp Normal file
View file

@ -0,0 +1,41 @@
#include <string>
#include <iostream>
using namespace std;
string Suffix(int num)
{
switch (num % 10)
{
case 1 : if(num % 100 != 11) return "st";
break;
case 2 : if(num % 100 != 12) return "nd";
break;
case 3 : if(num % 100 != 13) return "rd";
}
return "th";
}
int main()
{
cout << "Set [0,25]:" << endl;
for (int i = 0; i < 26; i++)
cout << i << Suffix(i) << " ";
cout << endl;
cout << "Set [250,265]:" << endl;
for (int i = 250; i < 266; i++)
cout << i << Suffix(i) << " ";
cout << endl;
cout << "Set [1000,1025]:" << endl;
for (int i = 1000; i < 1026; i++)
cout << i << Suffix(i) << " ";
cout << endl;
return 0;
}

32
Task/Nth/C-sharp/nth-1.cs Normal file
View file

@ -0,0 +1,32 @@
using System;
using System.Linq;
class Program
{
private static string Ordinalize(int i)
{
i = Math.Abs(i);
if (new[] {11, 12, 13}.Contains(i%100))
return i + "th";
switch (i%10)
{
case 1:
return i + "st";
case 2:
return i + "nd";
case 3:
return i + "rd";
default:
return i + "th";
}
}
static void Main()
{
Console.WriteLine(string.Join(" ", Enumerable.Range(0, 26).Select(Ordinalize)));
Console.WriteLine(string.Join(" ", Enumerable.Range(250, 16).Select(Ordinalize)));
Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize)));
}
}

22
Task/Nth/C-sharp/nth-2.cs Normal file
View file

@ -0,0 +1,22 @@
using System;
static string Ordinalize(int i)
{
return i + (
i % 100 is >= 11 and <= 13 ? "th" :
i % 10 == 1 ? "st" :
i % 10 == 2 ? "nd" :
i % 10 == 3 ? "rd" :
"th");
}
static void PrintRange(int begin, int end)
{
for(var i = begin; i <= end; i++)
Console.Write(Ordinalize(i) + (i == end ? "" : " "));
Console.WriteLine();
}
PrintRange(0, 25);
PrintRange(250, 265);
PrintRange(1000, 1025);

52
Task/Nth/C/nth-1.c Normal file
View file

@ -0,0 +1,52 @@
#include <stdio.h>
char* addSuffix(int num, char* buf, size_t len)
{
char *suffixes[4] = { "th", "st", "nd", "rd" };
int i;
switch (num % 10)
{
case 1 : i = (num % 100 == 11) ? 0 : 1;
break;
case 2 : i = (num % 100 == 12) ? 0 : 2;
break;
case 3 : i = (num % 100 == 13) ? 0 : 3;
break;
default: i = 0;
};
snprintf(buf, len, "%d%s", num, suffixes[i]);
return buf;
}
int main(void)
{
int i;
printf("Set [0,25]:\n");
for (i = 0; i < 26; i++)
{
char s[5];
printf("%s ", addSuffix(i, s, 5));
}
putchar('\n');
printf("Set [250,265]:\n");
for (i = 250; i < 266; i++)
{
char s[6];
printf("%s ", addSuffix(i, s, 6));
}
putchar('\n');
printf("Set [1000,1025]:\n");
for (i = 1000; i < 1026; i++)
{
char s[7];
printf("%s ", addSuffix(i, s, 7));
}
putchar('\n');
return 0;
}

33
Task/Nth/C/nth-2.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
static int digits(const int x) {
if (x / 10 == 0) return 1;
return 1 + digits(x / 10);
}
static char * get_ordinal(const int i) {
const int string_size = digits(i) + 3;
char * o_number = malloc(string_size);
char * ordinal;
if(i % 100 >= 11 && i % 100 <= 13) ordinal = "th";
else ordinal = i/10==1?"th":i%10==1?"st":i%10==2?"nd":i%10==3?"rd":"th";
sprintf_s(o_number, string_size, "%d%s", i, ordinal);
return o_number;
}
static void print_range(const int begin, const int end) {
printf("Set [%d,%d]:\n", begin, end);
for (int i = begin; i <= end; i++) {
char * o_number = get_ordinal(i);
printf("%s ", o_number);
free(o_number);
}
printf("\n");
}
int main(void) {
print_range(0, 25);
print_range(250, 265);
print_range(1000, 1025);
}

31
Task/Nth/CLU/nth.clu Normal file
View file

@ -0,0 +1,31 @@
nth = proc (n: int) returns (string)
num: string := int$unparse(n)
sfx: array[string] := array[string]$[0: "th", "st", "nd", "rd"]
if n / 10 // 10 = 1 cor n // 10 > 3 then
return(num || sfx[0])
else
return(num || sfx[n // 10])
end
end nth
do_range = proc (from, to: int)
po: stream := stream$primary_output()
col: int := 0
for i: int in int$from_to(from,to) do
stream$putleft(po, nth(i), 7)
col := col + 1
if col = 10 then
stream$putc(po, '\n')
col := 0
end
end
stream$putl(po, "\n")
end do_range
start_up = proc ()
do_range(0,25)
do_range(250,265)
do_range(1000,1025)
end start_up

31
Task/Nth/COBOL/nth.cobol Normal file
View file

@ -0,0 +1,31 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. NTH-PROGRAM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUMBER.
05 N PIC 9(8).
05 LAST-TWO-DIGITS PIC 99.
05 LAST-DIGIT PIC 9.
05 N-TO-OUTPUT PIC Z(7)9.
05 SUFFIX PIC AA.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
PERFORM NTH-PARAGRAPH VARYING N FROM 0 BY 1 UNTIL N IS GREATER THAN 25.
PERFORM NTH-PARAGRAPH VARYING N FROM 250 BY 1 UNTIL N IS GREATER THAN 265.
PERFORM NTH-PARAGRAPH VARYING N FROM 1000 BY 1 UNTIL N IS GREATER THAN 1025.
STOP RUN.
NTH-PARAGRAPH.
MOVE 'TH' TO SUFFIX.
MOVE N (7:2) TO LAST-TWO-DIGITS.
IF LAST-TWO-DIGITS IS LESS THAN 4,
OR LAST-TWO-DIGITS IS GREATER THAN 20,
THEN PERFORM DECISION-PARAGRAPH.
MOVE N TO N-TO-OUTPUT.
DISPLAY N-TO-OUTPUT WITH NO ADVANCING.
DISPLAY SUFFIX WITH NO ADVANCING.
DISPLAY SPACE WITH NO ADVANCING.
DECISION-PARAGRAPH.
MOVE N (8:1) TO LAST-DIGIT.
IF LAST-DIGIT IS EQUAL TO 1 THEN MOVE 'ST' TO SUFFIX.
IF LAST-DIGIT IS EQUAL TO 2 THEN MOVE 'ND' TO SUFFIX.
IF LAST-DIGIT IS EQUAL TO 3 THEN MOVE 'RD' TO SUFFIX.

View file

@ -0,0 +1,30 @@
100 cls
110 imprimeordinal(0,25)
120 print : print
130 imprimeordinal(250,265)
140 print : print
150 imprimeordinal(1000,1025)
160 print
170 end
180 sub ordinal$(n)
190 ns$ = str$(n)
200 ordinal$ = ""
210 select case right$(ns$,1)
220 case "1"
230 if right$(ns$,2) = "11" then ordinal$ = ns$+"th" : goto 340
240 ordinal$ = ns$+"st" : goto 340
250 case "2"
260 if right$(ns$,2) = "12" then ordinal$ = ns$+"th" : goto 340
270 ordinal$ = ns$+"nd" : goto 340
280 case "3"
290 if right$(ns$,2) = "13" then ordinal$ = ns$+"th" : goto 340
300 ordinal$ = ns$+"rd" : goto 340
310 case else
320 ordinal$ = ns$+"th" : goto 340
330 end case
340 return
350 sub imprimeordinal(a,b)
360 for i = a to b
370 print ordinal$(i);" ";
380 next i
390 return

View file

@ -0,0 +1,14 @@
(defn n-th [n]
(str n
(let [rem (mod n 100)]
(if (and (>= rem 11) (<= rem 13))
"th"
(condp = (mod n 10)
1 "st"
2 "nd"
3 "rd"
"th")))))
(apply str (interpose " " (map n-th (range 0 26))))
(apply str (interpose " " (map n-th (range 250 266))))
(apply str (interpose " " (map n-th (range 1000 1026))))

View file

@ -0,0 +1 @@
(apply str (interpose " " (map #(clojure.pprint/cl-format nil "~:R" %) (range 0 26))))

View file

@ -0,0 +1,28 @@
0 REM ROSETTACODE.ORG
1 REM N'TH
2 REM WRITE A FUNCTION/METHOD/SUBROUTINE/... THAT WHEN GIVEN AN INTEGER GREATER
3 REM THAN OR EQUAL TO ZERO RETURNS A STRING OF THE NUMBER FOLLOWED BY
4 REM AN APOSTROPHE THEN THE ORDINAL SUFFIX.
5 REM BASED ON APPLESOFT BASIC VERSION @ ROSETTACODE.ORG
6 REM
7 REM *************************
8 PRINT CHR$(14): REM CHANGE TO LOWER/UPPER CASE CHAR SET
9 OP = 1
10 FOR N = 0 TO 25 : GOSUB 100 : NEXT : PRINT
20 FOR N = 250 TO 265 : GOSUB 100 : NEXT : PRINT
30 FOR N = 1000 TO 1025 : GOSUB 100 : NEXT : PRINT
40 END
50 REM *************************
100 GOSUB 200
110 PRINT NTH$ " ";
120 RETURN
130 REM ************************
200 M1 = N - INT(N / 10) * 10
210 M2 = N - INT(N / 100) * 100
220 NTH$ = "TH"
230 IF M1 = 1 AND M2 <> 11 THEN NTH$ = "ST"
240 IF M1 = 2 AND M2 <> 12 THEN NTH$ = "ND"
250 IF M1 = 3 AND M2 <> 13 THEN NTH$ = "RD"
260 IF NOT OP THEN NTH$ = "'" + NTH$
270 NTH$ = STR$(N) + NTH$
280 RETURN

View file

@ -0,0 +1,8 @@
(defun add-suffix (number)
(let* ((suffixes #10("th" "st" "nd" "rd" "th"))
(last2 (mod number 100))
(last-digit (mod number 10))
(suffix (if (< 10 last2 20)
"th"
(svref suffixes last-digit))))
(format nil "~a~a" number suffix)))

View file

@ -0,0 +1,2 @@
(defun add-suffix (n)
(format nil "~d'~:[~[th~;st~;nd~;rd~:;th~]~;th~]" n (< (mod (- n 10) 100) 10) (mod n 10)))

View file

@ -0,0 +1,6 @@
(loop for (low high) in '((0 25) (250 265) (1000 1025))
do (progn
(format t "~a to ~a: " low high)
(loop for n from low to high
do (format t "~a " (add-suffix n))
finally (terpri))))

View file

@ -0,0 +1,40 @@
include "cowgol.coh";
include "strings.coh";
# Given an int32, return decimal string and add an ordinal suffix
sub ordsfx(n: int32, buf: [uint8]): (out: [uint8]) is
var sfx: [uint8][] := {"th", "st", "nd", "rd"};
out := buf;
buf := IToA(n, 10, buf); # IToA is included in standard library
# Since we now already have the digits, we can make the decision
# without doing any more work.
if (n > 10 and [@prev @prev buf] == '1')
or ([@prev buf] > '3') then
CopyString(sfx[0], buf);
else
CopyString(sfx[[@prev buf]-'0'], buf);
end if;
end sub;
# Print suffixed numerals from start to end inclusive
sub test(start: int32, end_: int32) is
var buf: uint8[16]; # buffer
var n: uint8 := 10;
while start <= end_ loop
print(ordsfx(start, &buf[0]));
print_char(' ');
start := start + 1;
n := n - 1;
if n == 0 then
print_nl();
n := 10;
end if;
end loop;
print_nl();
end sub;
test(0, 25);
test(250,265);
test(1000,1025);

View file

@ -0,0 +1,18 @@
struct Int
def ordinalize
num = self.abs
ordinal = if (11..13).includes?(num % 100)
"th"
else
case num % 10
when 1; "st"
when 2; "nd"
when 3; "rd"
else "th"
end
end
"#{self}#{ordinal}"
end
end
[(0..25),(250..265),(1000..1025)].each{|r| puts r.map{ |n| n.ordinalize }.join(", "); puts}

12
Task/Nth/D/nth.d Normal file
View file

@ -0,0 +1,12 @@
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);
}

32
Task/Nth/Draco/nth.draco Normal file
View file

@ -0,0 +1,32 @@
proc nonrec nth(word n; *char buf) *char:
channel output text ch;
open(ch, buf);
write(ch; n,
if (n/10)%10=1 then "th"
elif n%10=1 then "st"
elif n%10=2 then "nd"
elif n%10=3 then "rd"
else "th"
fi
);
close(ch);
buf
corp;
proc nonrec print_range(word start, stop) void:
[8] char buf;
word col, n;
col := 0;
for n from start upto stop do
write(nth(n, &buf[0]));
col := col + 1;
if col%10=0 then writeln() else write('\t') fi
od;
writeln()
corp
proc nonrec main() void:
print_range(0, 25);
print_range(250, 265);
print_range(1000, 1025)
corp

23
Task/Nth/ERRE/nth.erre Normal file
View file

@ -0,0 +1,23 @@
PROGRAM NTH_SOLVE
!
! for rosettacode.org
!
PROCEDURE NTH(S%,E%)
LOCAL I%,SUFF$
FOR I%=S% TO E% DO
SUFF$="th"
IF I% MOD 10=1 AND I% MOD 100<>11 THEN SUFF$="st" END IF
IF I% MOD 10=2 AND I% MOD 100<>12 THEN SUFF$="nd" END IF
IF I% MOD 10=3 AND I% MOD 100<>13 THEN SUFF$="rd" END IF
PRINT(STR$(I%)+SUFF$+" ";)
END FOR
PRINT
END PROCEDURE
BEGIN
NTH(0,25)
NTH(250,265)
NTH(1000,1025)
END PROGRAM

View file

@ -0,0 +1,36 @@
proc nth num . ordinal$ .
num$ = num
lastTwoDigits$ = substr num$ len num$ - 1 2
lastDigit$ = substr num$ len num$ 1
if lastTwoDigits$ = "11" or lastTwoDigits$ = "12" or lastTwoDigits$ = "13"
ordinal$ = num$ & "th"
elif lastDigit$ = "1"
ordinal$ = num$ & "st"
elif lastDigit$ = "2"
ordinal$ = num$ & "nd"
elif lastDigit$ = "3"
ordinal$ = num$ & "rd"
else
ordinal$ = num$ & "th"
.
.
print "0 to 25:"
for i = 0 to 25
call nth i ordinal$
write ordinal$
write " "
.
print ""
print "250 to 265:"
for i = 250 to 265
call nth i ordinal$
write ordinal$
write " "
.
print ""
print "1000 to 1025:"
for i = 1000 to 1025
call nth i ordinal$
write ordinal$
write " "
.

29
Task/Nth/Elena/nth.elena Normal file
View file

@ -0,0 +1,29 @@
import extensions;
import system'math;
import system'routines;
extension op
{
ordinalize()
{
int i := self.Absolute;
if (new int[]{11,12,13}.ifExists(i.mod:100))
{
^ i.toPrintable() + "th"
};
(i.mod(10)) =>
1 { ^ i.toPrintable() + "st" }
2 { ^ i.toPrintable() + "nd" }
3 { ^ i.toPrintable() + "rd" };
^ i.toPrintable() + "th"
}
}
public program()
{
console.printLine(new Range(0,26).selectBy(mssgconst ordinalize<op>[1]));
console.printLine(new Range(250,26).selectBy(mssgconst ordinalize<op>[1]));
console.printLine(new Range(1000,26).selectBy(mssgconst ordinalize<op>[1]))
}

View file

@ -0,0 +1,20 @@
defmodule RC do
def ordinalize(n) do
num = abs(n)
ordinal = if rem(num, 100) in 4..20 do
"th"
else
case rem(num, 10) do
1 -> "st"
2 -> "nd"
3 -> "rd"
_ -> "th"
end
end
"#{n}#{ordinal}"
end
end
Enum.each([0..25, 250..265, 1000..1025], fn range ->
Enum.map(range, fn n -> RC.ordinalize(n) end) |> Enum.join(" ") |> IO.puts
end)

15
Task/Nth/F-Sharp/nth.fs Normal file
View file

@ -0,0 +1,15 @@
open System
let ordinalsuffix n =
let suffixstrings = [|"th"; "st"; "nd"; "rd"|]
let (d, r) = Math.DivRem(n, 10)
n.ToString() + suffixstrings.[ if r < 4 && (d &&& 1) = 0 then r else 0 ]
[<EntryPoint>]
let main argv =
let show = (Seq.iter (ordinalsuffix >> (printf " %s"))) >> (Console.WriteLine)
[0..25] |> show
[250..265] |> show
[1000..1025] |> show
0

View file

@ -0,0 +1,14 @@
USING: io kernel math math.order math.parser math.ranges qw
sequences ;
IN: rosetta-code.nth
: n'th ( n -- str )
dup 10 /mod swap 1 = [ drop 0 ] when
[ number>string ]
[ 4 min qw{ th st nd rd th } nth ] bi* append ;
: n'th-demo ( -- )
0 25 250 265 1000 1025 [ [a,b] ] 2tri@
[ [ n'th write bl ] each nl ] tri@ ;
MAIN: n'th-demo

10
Task/Nth/Forth/nth.fth Normal file
View file

@ -0,0 +1,10 @@
: 'nth ( -- c-addr ) s" th st nd rd th th th th th th " drop ;
: .nth ( n -- )
dup 100 mod 10 20 within if 0 .r ." th " exit then
dup 0 .r 10 mod 3 * 'nth + 3 type ;
: test ( n n -- ) cr do i 5 mod 0= if cr then i .nth loop ;
: tests ( -- )
26 0 test 266 250 test 1026 1000 test ;
tests

91
Task/Nth/Fortran/nth.f Normal file
View file

@ -0,0 +1,91 @@
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Fri Jun 6 15:40:18
!
!a=./f && make -k $a && echo 0 25 | $a && echo 250 265 | $a && echo 1000 1025 | $a
!gfortran -std=f2008 -Wall -fopenmp -ffree-form -fall-intrinsics -fimplicit-none -g f.f08 -o f
! 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
! 1000th 1001st
! 1002nd 1003rd 1004th
! 1005th 1006th 1007th
! 1008th 1009th 1010th
! 1011th 1012th 1013th
! 1014th 1015th 1016th
! 1017th 1018th 1019th
! 1020th 1021st 1022nd
! 1023rd 1024th 1025th
!
!Compilation finished at Fri Jun 6 15:40:18
program nth
implicit none
logical :: need
integer :: here, there, n, i, iostat
read(5,*,iostat=iostat) here, there
if (iostat .ne. 0) then
write(6,*)'such bad input never before seen.'
write(6,*)'I AYE EYE QUIT!'
call exit(1)
end if
need = .false.
n = abs(there - here) + 1
i = 0
do while (0 /= mod(3+mod(here-i, 3), 3))
write(6,'(a22)',advance='no') ''
i = i+1
end do
do i = here, there, sign(1, there-here)
write(6,'(a22)',advance='no') ordinate(i)
if (2 /= mod(i,3)) then
need = .true.
else
write(6,'(a)')''
need = .false.
end if
end do
if (need) write(6,'(a)')''
contains
character(len=22) function ordinate(n)
character(len=19) :: a
character(len=20), parameter :: &
&a09 = "thstndrdthththththth",&
&ateen = "thththththththththth"
integer :: ones, tens, ones_index
integer, intent(in) :: n
write(a,'(i19)') n
ones = mod(n,10)
tens = mod(n,100)
ones_index = ones*2+1
if (n < 1000) then
if ((10 .le. tens) .and. (tens .lt. 20)) then
ordinate = a // "'" // ateen(ones_index:ones_index+1)
! ^^^^^^ remove these characters to remove the important '
else
ordinate = a // "'" // a09(ones_index:ones_index+1)
! ^^^^^^ remove these characters to remove the important '
end if
else
if ((10 .le. tens) .and. (tens .lt. 20)) then
ordinate = a // ateen(ones_index:ones_index+1)
else
ordinate = a // a09(ones_index:ones_index+1)
end if
end if
end function ordinate
end program nth

View file

@ -0,0 +1,39 @@
' FB 1.05.0 Win64
' Apostrophes NOT used as incorrect English
Function ordinal(n As UInteger) As String
Dim ns As String = Str(n)
Select Case Right(ns, 1)
Case "0", "4" To "9"
Return ns + "th"
Case "1"
If Right(ns, 2) = "11" Then Return ns + "th"
Return ns + "st"
Case "2"
If Right(ns, 2) = "12" Then Return ns + "th"
Return ns + "nd"
Case "3"
If Right(ns, 2) = "13" Then Return ns + "th"
Return ns + "rd"
End Select
End Function
Dim i As Integer
For i = 0 To 25
Print ordinal(i); " ";
Next
Print : Print
For i = 250 To 265
Print ordinal(i); " ";
Next
Print : Print
For i = 1000 To 1025
Print ordinal(i); " ";
Next
Print : Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,28 @@
10 ' N'th
20 LET LOLIM% = 0
30 LET HILIM% = 25
40 GOSUB 1000
50 LET LOLIM% = 250
60 LET HILIM% = 265
70 GOSUB 1000
80 LET LOLIM% = 1000
90 LET HILIM% = 1025
100 GOSUB 1000
110 END
995 ' Print images
1000 FOR I% = LOLIM% TO HILIM%
1010 LET NR% = I%
1020 GOSUB 1500
1030 LET SI$ = STR$(I%)
1040 PRINT RIGHT$(SI$, LEN(SI$) - 1); SUF$; " ";
1050 NEXT I%
1060 PRINT
1070 RETURN
1495 ' Get suffix
1500 IF (NR% MOD 10 = 1) AND (NR% MOD 100 <> 11) THEN LET SUF$ = "st": GOTO 2000
1600 IF (NR% MOD 10 = 2) AND (NR% MOD 100 <> 12) THEN LET SUF$ = "nd": GOTO 2000
1700 IF (NR% MOD 10 = 3) AND (NR% MOD 100 <> 13) THEN LET SUF$ = "rd": GOTO 2000
1800 LET SUF$ = "th"
2000 RETURN

View file

@ -0,0 +1,17 @@
Public Sub Main()
Dim siNums As Short[] = [0, 25, 250, 265, 1000, 1025]
Dim siCount, siNumbers As Short
Dim sOrdinal As String
For siNumbers = 0 To 4 Step 2
For siCount = siNums[siNumbers] To siNums[siNumbers + 1]
sOrdinal = "th"
If Right(Str(siCount), 1) = "1" And Right(Str(siCount), 2) <> "11" Then sOrdinal = "st"
If Right(Str(siCount), 1) = "2" And Right(Str(siCount), 2) <> "12" Then sOrdinal = "nd"
If Right(Str(siCount), 1) = "3" And Right(Str(siCount), 2) <> "13" Then sOrdinal = "rd"
Print siCount & sOrdinal;;
Next
Print gb.NewLine
Next
End

37
Task/Nth/Go/nth.go Normal file
View file

@ -0,0 +1,37 @@
package main
import "fmt"
func ord(n int) string {
s := "th"
switch c := n % 10; c {
case 1, 2, 3:
if n%100/10 == 1 {
break
}
switch c {
case 1:
s = "st"
case 2:
s = "nd"
case 3:
s = "rd"
}
}
return fmt.Sprintf("%d%s", n, s)
}
func main() {
for n := 0; n <= 25; n++ {
fmt.Printf("%s ", ord(n))
}
fmt.Println()
for n := 250; n <= 265; n++ {
fmt.Printf("%s ", ord(n))
}
fmt.Println()
for n := 1000; n <= 1025; n++ {
fmt.Printf("%s ", ord(n))
}
fmt.Println()
}

19
Task/Nth/Haskell/nth.hs Normal file
View file

@ -0,0 +1,19 @@
import Data.Array
ordSuffs :: Array Integer String
ordSuffs = listArray (0,9) ["th", "st", "nd", "rd", "th",
"th", "th", "th", "th", "th"]
ordSuff :: Integer -> String
ordSuff n = show n ++ suff n
where suff m | (m `rem` 100) >= 11 && (m `rem` 100) <= 13 = "th"
| otherwise = ordSuffs ! (m `rem` 10)
printOrdSuffs :: [Integer] -> IO ()
printOrdSuffs = putStrLn . unwords . map ordSuff
main :: IO ()
main = do
printOrdSuffs [ 0.. 25]
printOrdSuffs [ 250.. 265]
printOrdSuffs [1000..1025]

11
Task/Nth/Icon/nth.icon Normal file
View file

@ -0,0 +1,11 @@
procedure main(A)
every writes(" ",nth(0 to 25) | "\n")
every writes(" ",nth(250 to 265) | "\n")
every writes(" ",nth(1000 to 1025) | "\n")
end
procedure nth(n)
return n || ((n%10 = 1, n%100 ~= 11, "st") |
(n%10 = 2, n%100 ~= 12, "nd") |
(n%10 = 3, n%100 ~= 13, "rd") | "th")
end

2
Task/Nth/J/nth-1.j Normal file
View file

@ -0,0 +1,2 @@
suf=: (;:'th st nd rd th') {::~ 4 <. 10 10 (* 1&~:)~/@#: ]
nth=: [: ;:inv (": , suf)each

7
Task/Nth/J/nth-2.j Normal file
View file

@ -0,0 +1,7 @@
thru=: <./ + i.@(+ *)@-~
nth 0 thru 25
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
nth 250 thru 265
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
nth 1000 thru 1025
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th

26
Task/Nth/Java/nth-1.java Normal file
View file

@ -0,0 +1,26 @@
public class Nth {
public static String ordinalAbbrev(int n){
String ans = "th"; //most of the time it should be "th"
if(n % 100 / 10 == 1) return ans; //teens are all "th"
switch(n % 10){
case 1: ans = "st"; break;
case 2: ans = "nd"; break;
case 3: ans = "rd"; break;
}
return ans;
}
public static void main(String[] args){
for(int i = 0; i <= 25;i++){
System.out.print(i + ordinalAbbrev(i) + " ");
}
System.out.println();
for(int i = 250; i <= 265;i++){
System.out.print(i + ordinalAbbrev(i) + " ");
}
System.out.println();
for(int i = 1000; i <= 1025;i++){
System.out.print(i + ordinalAbbrev(i) + " ");
}
}
}

48
Task/Nth/Java/nth-2.java Normal file
View file

@ -0,0 +1,48 @@
package nth;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public interface Nth {
public static String suffix(int n){
if(n % 100 / 10 == 1){
return "th"; //teens are all "th"
}
switch(n % 10){
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th"; //most of the time it should be "th"
}
}
public static void print(int start, int end) {
IntStream.rangeClosed(start, end)
.parallel()
.mapToObj(i -> i + suffix(i) + " ")
.reduce(String::concat)
.ifPresent(System.out::println)
;
}
public static void print(int[] startAndEnd) {
print(startAndEnd[0], startAndEnd[1]);
}
public static int[] startAndEnd(int start, int end) {
return new int[] {
start,
end
};
}
public static void main(String... arguments){
Stream.of(
startAndEnd(0, 25),
startAndEnd(250, 265),
startAndEnd(1000, 1025)
)
.forEach(Nth::print)
;
}
}

View file

@ -0,0 +1,24 @@
console.log(function () {
var lstSuffix = 'th st nd rd th th th th th th'.split(' '),
fnOrdinalForm = function (n) {
return n.toString() + (
11 <= n % 100 && 13 >= n % 100 ?
"th" : lstSuffix[n % 10]
);
},
range = function (m, n) {
return Array.apply(
null, Array(n - m + 1)
).map(function (x, i) {
return m + i;
});
};
return [[0, 25], [250, 265], [1000, 1025]].map(function (tpl) {
return range.apply(null, tpl).map(fnOrdinalForm).join(' ');
}).join('\n\n');
}());

View file

@ -0,0 +1,5 @@
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th 261st 262nd 263rd 264th 265th
1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th 1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th 1021st 1022nd 1023rd 1024th 1025th

View file

@ -0,0 +1,26 @@
(function (lstTestRanges) {
'use strict'
let lstSuffix = 'th st nd rd th th th th th th'.split(' '),
// ordinalString :: Int -> String
ordinalString = n =>
n.toString() + (
11 <= n % 100 && 13 >= n % 100 ?
"th" : lstSuffix[n % 10]
),
// range :: Int -> Int -> [Int]
range = (m, n) =>
Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
return lstTestRanges
.map(tpl => range
.apply(null, tpl)
.map(ordinalString)
);
})([[0, 25], [250, 265], [1000, 1025]]);

17
Task/Nth/Jq/nth.jq Normal file
View file

@ -0,0 +1,17 @@
# ordinalize an integer input, positive or negative
def ordinalize:
(if . < 0 then -(.) else . end) as $num
| ($num % 100) as $small
| (if 11 <= $small and $small <= 13 then "th"
else
( $num % 10)
| (if . == 1 then "st"
elif . == 2 then "nd"
elif . == 3 then "rd"
else "th"
end)
end) as $ordinal
| "\(.)\($ordinal)" ;
([range(-5; -1)], [range(0;26)], [range(250;266)], [range(1000;1026)])
| map(ordinalize)

View file

@ -0,0 +1 @@
using Printf

View file

@ -0,0 +1,12 @@
function ordinal(n::Integer)
n < 0 && throw(DomainError())
suffixes = ("st", "nd", "rd")
u = n % 10
t = n ÷ 10 % 10
if u > 3 || u == 0 || t == 1
suf = "th"
else
suf = suffixes[u]
end
return string(n, suf)
end

View file

@ -0,0 +1,17 @@
println("Tests of ordinal formatting of integers.")
for (i, n) in enumerate(0:25)
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
println()
for (i, n) in enumerate(250:265)
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
println()
for (i, n) in enumerate(1000:1025)
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end

View file

@ -0,0 +1,3 @@
Nth(x::Integer) = if x % 100 ∈ [11, 12, 13] "th" else ["th", "st", "nd", "rd", "th"][min(x % 10 + 1, 5)] end
NthA(x::Integer) = "$(x)'$(Nth(x)) "
[0:25..., 250:265..., 1000:1025...] .|> NthA .|> print;

View file

@ -0,0 +1,9 @@
fun Int.ordinalAbbrev() =
if (this % 100 / 10 == 1) "th"
else when (this % 10) { 1 -> "st" 2 -> "nd" 3 -> "rd" else -> "th" }
fun IntRange.ordinalAbbrev() = map { "$it" + it.ordinalAbbrev() }.joinToString(" ")
fun main(args: Array<String>) {
listOf((0..25), (250..265), (1000..1025)).forEach { println(it.ordinalAbbrev()) }
}

View file

@ -0,0 +1,19 @@
{def fnOrdinalForm
{lambda {:n}
{if {and {<= 11 {% :n 100}} {>= 13 {% :n 100}}}
then :nth
else :n{A.get {% :n 10} th st nd rd th th th th th th}}}}
-> fnOrdinalForm
{S.map fnOrdinalForm {S.serie 0 25}}
-> 0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th
16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th
{S.map fnOrdinalForm {S.serie 250 265}}
-> 250th 251st 252nd 253rd 254th 255th 256th 257th 258th 259th 260th
261st 262nd 263rd 264th 265th
{S.map fnOrdinalForm {S.serie 1000 1025}}
-> 1000th 1001st 1002nd 1003rd 1004th 1005th 1006th 1007th 1008th 1009th 1010th
1011th 1012th 1013th 1014th 1015th 1016th 1017th 1018th 1019th 1020th
1021st 1022nd 1023rd 1024th 1025th

View file

@ -0,0 +1,32 @@
call printImages 0, 25
call printImages 250, 265
call printImages 1000, 1025
end
sub printImages loLim, hiLim
loLim = int(loLim)
hiLIm = int(hiLim)
for i = loLim to hiLim
print str$(i) + suffix$(i) + " ";
next i
print
end sub
function suffix$(n)
n = int(n)
nMod10 = n mod 10
nMod100 = n mod 100
if (nMod10 = 1) and (nMod100 <> 11) then
suffix$ = "st"
else
if (nMod10 = 2) and (nMod100 <> 12) then
suffix$ = "nd"
else
if (NMod10 = 3) and (NMod100 <> 13) then
suffix$ = "rd"
else
suffix$ = "th"
end if
end if
end if
end function

12
Task/Nth/Lua/nth.lua Normal file
View file

@ -0,0 +1,12 @@
function getSuffix (n)
local lastTwo, lastOne = n % 100, n % 10
if lastTwo > 3 and lastTwo < 21 then return "th" end
if lastOne == 1 then return "st" end
if lastOne == 2 then return "nd" end
if lastOne == 3 then return "rd" end
return "th"
end
function Nth (n) return n .. "'" .. getSuffix(n) end
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) end

18
Task/Nth/MATLAB/nth.m Normal file
View file

@ -0,0 +1,18 @@
function s = nth(n)
tens = mod(n, 100);
if tens > 9 && tens < 20
suf = 'th';
else
switch mod(n, 10)
case 1
suf = 'st';
case 2
suf = 'nd';
case 3
suf = 'rd';
otherwise
suf = 'th';
end
end
s = sprintf('%d%s', n, suf);
end

25
Task/Nth/Maple/nth.maple Normal file
View file

@ -0,0 +1,25 @@
toOrdinal := proc(n:: nonnegint)
if 1 <= n and n <= 10 then
if n >= 4 then
printf("%ath", n);
elif n = 3 then
printf("%ard", n);
elif n = 2 then
printf("%and", n);
else
printf("%ast", n);
end if:
else
printf(convert(n, 'ordinal'));
end if:
return NULL;
end proc:
a := [[0, 25], [250, 265], [1000, 1025]]:
for i in a do
for j from i[1] to i[2] do
toOrdinal(j);
printf(" ");
end do;
printf("\n\n");
end do;

View file

@ -0,0 +1,12 @@
suffixlist = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th","th"};
addsuffix[n_] := Module[{suffix},
suffix = Which[
Mod[n, 100] <= 10, suffixlist[[Mod[n, 10] + 1]],
Mod[n, 100] > 20, suffixlist[[Mod[n, 10] + 1]],
True, "th"
];
ToString[n] <> suffix
]
addsuffix[#] & /@ Range[0, 25] (* test 1 *)
addsuffix[#] & /@ Range[250, 265] (* test 2 *)
addsuffix[#] & /@ Range[1000, 1025] (* test 3 *)

View file

@ -0,0 +1,32 @@
loLim = 0
hiLim = 25
PrintImages()
loLim = 250
hiLim = 265
PrintImages()
loLim = 1000
hiLim = 1025
PrintImages()
Sub PrintImages
For i = loLim To hiLim
nr = i
GetSuffix()
TextWindow.Write(i + suffix + " ")
EndFor
TextWindow.WriteLine("")
EndSub
Sub GetSuffix
rem10 = Math.Remainder(nr, 10)
rem100 = Math.Remainder(nr, 100)
If rem10 = 1 And rem100 <> 11 Then
suffix = "st"
ElseIf rem10 = 2 And rem100 <> 12 Then
suffix = "nd"
ElseIf rem10 = 3 And rem100 <> 13 Then
suffix = "rd"
Else
suffix = "th"
EndIf
EndSub

View file

@ -0,0 +1,19 @@
ordinal = function(n)
if n > 3 and n < 20 then return n + "th"
if n % 10 == 1 then return n + "st"
if n % 10 == 2 then return n + "nd"
if n % 10 == 3 then return n + "rd"
return n + "th"
end function
out = []
test = function(from, to)
for i in range(from, to)
out.push ordinal(i)
end for
end function
test 0, 25
test 250, 265
test 1000, 1025
print out.join

View file

@ -0,0 +1,45 @@
MODULE Nth;
FROM STextIO IMPORT
WriteString, WriteLn;
FROM WholeStr IMPORT
IntToStr;
PROCEDURE Suffix(N: CARDINAL; VAR OUT Destination: ARRAY OF CHAR);
VAR
NMod10, NMod100: CARDINAL;
BEGIN
NMod10 := N MOD 10;
NMod100 := N MOD 100;
IF (NMod10 = 1) AND (NMod100 <> 11) THEN
Destination := "st";
ELSIF (NMod10 = 2) AND (NMod100 <> 12) THEN
Destination := "nd";
ELSIF (NMod10 = 3) AND (NMod100 <> 13) THEN
Destination := "rd";
ELSE
Destination := "th";
END;
END Suffix;
PROCEDURE PrintImages(LoLim, HiLim: CARDINAL);
VAR
I: CARDINAL;
IString: ARRAY [0 .. 15] OF CHAR;
ISuff: ARRAY [0 .. 1] OF CHAR;
BEGIN
FOR I := LoLim TO HiLim DO
IntToStr(I, IString);
Suffix(I, ISuff);
WriteString(IString);
WriteString(ISuff);
WriteString(" ");
END;
WriteLn;
END PrintImages;
BEGIN
PrintImages( 0, 25);
PrintImages( 250, 265);
PrintImages(1000, 1025);
END Nth.

View file

@ -0,0 +1,30 @@
def ordinalAbbrev(n)
if int(n % 100 / 10) = 1
return "th"
end
if n % 10 = 1
return "st"
else if n % 10 = 2
return "nd"
else if n % 10 = 3
return "rd"
else
return "th"
end
end
for i in range(0, 25)
print (i + "'" + ordinalAbbrev(i) + " ")
end
println
for i in range(250, 265)
print (i + "'" + ordinalAbbrev(i) + " ")
end
println
for i in range(1000, 1025)
print (i + "'" + ordinalAbbrev(i) + " ")
end
println

View file

@ -0,0 +1,27 @@
10 REM N'th
20 LOLIM=0
30 HILIM=25
40 GOSUB 1000
50 LOLIM=250
60 HILIM=265
70 GOSUB 1000
80 LOLIM=1000
90 HILIM=1025
100 GOSUB 1000
110 END
995 REM ** Print images
1000 FOR I=LOLIM TO HILIM
1010 NR=I
1020 GOSUB 1500
1030 SI$=STR$(I)
1040 PRINT RIGHT$(SI$,LEN(SI$)-1);SUF$;" ";
1050 NEXT I
1060 PRINT
1070 RETURN
1495 REM ** Get suffix
1500 LA=NR-INT(NR/10)*10:NLA=NR-INT(NR/100)*100
1510 IF LA=1 AND NLA<>11 THEN SUF$="st":RETURN
1520 IF LA=2 AND NLA<>12 THEN SUF$="nd":RETURN
1530 IF LA=3 AND NLA<>13 THEN SUF$="rd":RETURN
1540 SUF$="th"
1550 RETURN

9
Task/Nth/Nim/nth.nim Normal file
View file

@ -0,0 +1,9 @@
const Suffix = ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"]
proc nth(n: Natural): string =
$n & "'" & (if n mod 100 in 11..20: "th" else: Suffix[n mod 10])
for j in countup(0, 1000, 250):
for i in j..j+24:
stdout.write nth(i), " "
echo ""

15
Task/Nth/OCaml/nth.ocaml Normal file
View file

@ -0,0 +1,15 @@
let show_nth n =
if (n mod 10 = 1) && (n mod 100 <> 11) then "st"
else if (n mod 10 = 2) && (n mod 100 <> 12) then "nd"
else if (n mod 10 = 3) && (n mod 100 <> 13) then "rd"
else "th"
let () =
let show_ordinals (min, max) =
for i=min to max do
Printf.printf "%d%s " i (show_nth i)
done;
print_newline() in
List.iter show_ordinals [ (0,25); (250,265); (1000,1025) ]

View file

@ -0,0 +1,35 @@
class Nth {
function : OrdinalAbbrev(n : Int ) ~ String {
ans := "th"; # most of the time it should be "th"
if(n % 100 / 10 = 1) {
return ans; # teens are all "th"
};
select(n % 10){
label 1: { ans := "st"; }
label 2: { ans := "nd"; }
label 3: { ans := "rd"; }
};
return ans;
}
function : Main(args : String[]) ~ Nil {
for(i := 0; i <= 25; i+=1;) {
abbr := OrdinalAbbrev(i);
"{$i}{$abbr} "->Print();
};
""->PrintLine();
for(i := 250; i <= 265; i+=1;) {
abbr := OrdinalAbbrev(i);
"{$i}{$abbr} "->Print();
};
""->PrintLine();
for(i := 1000; i <= 1025; i+=1;) {
abbr := OrdinalAbbrev(i);
"{$i}{$abbr} "->Print();
};
}
}

7
Task/Nth/Oforth/nth.fth Normal file
View file

@ -0,0 +1,7 @@
: nth(n)
| r |
n "th" over 10 mod ->r
r 1 == ifTrue: [ n 100 mod 11 == ifFalse: [ drop "st" ] ]
r 2 == ifTrue: [ n 100 mod 12 == ifFalse: [ drop "nd" ] ]
r 3 == ifTrue: [ n 100 mod 13 == ifFalse: [ drop "rd" ] ]
+ ;

View file

@ -0,0 +1,5 @@
ordinal(n)=my(k=n%10,m=n%100); Str(n,if(m<21&&m>3,"th",k==1,"st",k==2,"nd",k==3,"rd","th"));
apply(ordinal, [0..25])
apply(ordinal, [250..265])
apply(ordinal, [1000..1025])
apply(ordinal, [111, 1012])

25
Task/Nth/PHP/nth.php Normal file
View file

@ -0,0 +1,25 @@
function nth($num) {
$os = "th";
if ($num % 100 <= 10 or $num % 100 > 20) {
switch ($num % 10) {
case 1:
$os = "st";
break;
case 2:
$os = "nd";
break;
case 3:
$os = "rd";
break;
}
}
return $num . $os;
}
foreach ([[0,25], [250,265], [1000,1025]] as $i) {
while ($i[0] <= $i[1]) {
echo nth($i[0]) . " ";
$i[0]++;
}
echo "\n";
}

26
Task/Nth/PL-I/nth.pli Normal file
View file

@ -0,0 +1,26 @@
Nth: procedure options (main); /* 1 June 2014 */
declare i fixed (10);
do i = 0 to 25, 250 to 265, 1000 to 1025;
if i = 250 | i = 1000 then put skip (2);
put edit (enth(i)) (x(1), a);
end;
enth: procedure (i) returns (character (25) varying);
declare i fixed (10);
declare suffix character (2);
select (mod(i, 10));
when (1) suffix = 'st';
when (2) suffix = 'nd';
when (3) suffix = 'rd';
otherwise suffix = 'th';
end;
select (mod(i, 100));
when (11, 12, 13) suffix = 'th';
otherwise ;
end;
return ( trim(i) || suffix );
end enth;
end Nth;

84
Task/Nth/PL-M/nth.plm Normal file
View file

@ -0,0 +1,84 @@
100H: /* SUFFIX NUMBERS WITH ST, ND, RD OR TH AS APPROPRIATE */
/* CP/M BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER: PROCEDURE( N ); /* PRINTS A NUMBER IN THE MINIMUN FIELD WIDTH */
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR ( 6 )BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* TASK */
DECLARE LD LITERALLY '100' /* LOWER CASE LETTERS NEEDED FOR THE SUFFICES */
, LH LITERALLY '104'
, LN LITERALLY '110'
, LR LITERALLY '114'
, LS LITERALLY '115'
, LT LITERALLY '116'
;
/* RETURNS THE TWO CHARACTER ORDINAL SUFFIX FOR N */
SUFFIX: PROCEDURE( N )ADDRESS;
DECLARE N ADDRESS;
DECLARE ( S1, S2 ) BYTE;
S1 = LT;
S2 = LH;
IF N MOD 100 < 4 OR N MOD 100 > 20 THEN DO;
DECLARE N10 ADDRESS;
N10 = N MOD 10;
IF N10 = 1 THEN DO;
S1 = LS;
S2 = LT;
END;
ELSE IF N10 = 2 THEN DO;
S1 = LN;
S2 = LD;
END;
ELSE IF N10 = 3 THEN DO;
S1 = LR;
S2 = LD;
END;
END;
RETURN ( S1 * 256 ) + S2;
END SUFFIX;
/* PRINTS THE TWO CHARACTER SUFFIX IN S */
PR$SUFFIX: PROCEDURE( S );
DECLARE S ADDRESS;
CALL PR$CHAR( HIGH( S ) );
CALL PR$CHAR( LOW( S ) );
END PR$SUFFIX ;
DECLARE I ADDRESS;
DO I = 0 TO 25;
IF I < 10 THEN CALL PR$CHAR( ' ' );
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) );
IF ( I + 1 ) MOD 10 = 0 THEN CALL PR$NL;
END;
CALL PR$NL;
CALL PR$NL;
DO I = 250 TO 265;
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) );
IF ( I - 249 ) MOD 10 = 0 THEN CALL PR$NL;
END;
CALL PR$NL;
CALL PR$NL;
DO I = 1000 TO 1025;
CALL PR$CHAR( ' ' );CALL PR$NUMBER( I );CALL PR$SUFFIX( SUFFIX( I ) );
IF ( I - 999 ) MOD 10 = 0 THEN CALL PR$NL;
END;
CALL PR$NL;
EOF

33
Task/Nth/Pascal/nth.pas Normal file
View file

@ -0,0 +1,33 @@
Program n_th;
function Suffix(N: NativeInt):AnsiString;
var
res: AnsiString;
begin
res:= 'th';
case N mod 10 of
1:IF N mod 100 <> 11 then
res:= 'st';
2:IF N mod 100 <> 12 then
res:= 'nd';
3:IF N mod 100 <> 13 then
res:= 'rd';
else
end;
Suffix := res;
end;
procedure Print_Images(loLim, HiLim: NativeInt);
var
i : NativeUint;
begin
for I := LoLim to HiLim do
write(i,Suffix(i),' ');
writeln;
end;
begin
Print_Images( 0, 25);
Print_Images( 250, 265);
Print_Images(1000, 1025);
end.

16
Task/Nth/Perl/nth-1.pl Normal file
View file

@ -0,0 +1,16 @@
use 5.10.0;
my %irregulars = ( 1 => 'st',
2 => 'nd',
3 => 'rd',
11 => 'th',
12 => 'th',
13 => 'th');
sub nth
{
my $n = shift;
$n . # q(') . # Uncomment this to add apostrophes to output
($irregulars{$n % 100} // $irregulars{$n % 10} // 'th');
}
sub range { join ' ', map { nth($_) } @{$_[0]} }
print range($_), "\n" for ([0..25], [250..265], [1000..1025]);

4
Task/Nth/Perl/nth-2.pl Normal file
View file

@ -0,0 +1,4 @@
use Lingua::EN::Numbers::Ordinate 'ordinate';
foreach my $i (0..25, 250..265, 1000..1025) {
print ordinate($i),"\n";
}

18
Task/Nth/Phix/nth-1.phix Normal file
View file

@ -0,0 +1,18 @@
(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">ordinals</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"th"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"st"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nd"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rd"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Nth</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">mod10</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">mod10</span><span style="color: #0000FF;">></span><span style="color: #000000;">4</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">mod10</span><span style="color: #0000FF;">+</span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span> <span style="color: #000000;">mod10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;"><nowiki>'\''</nowiki></span><span style="color: #0000FF;">,</span><span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">ordinals</span><span style="color: #0000FF;">[</span><span style="color: #000000;">mod10</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">ranges</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">265</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1025</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ranges</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">ranges</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ranges</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %6s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Nth</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--

10
Task/Nth/Phix/nth-2.phix Normal file
View file

@ -0,0 +1,10 @@
(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">do_one</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">pad_head</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d%s%s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">apostrophe</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">ord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)}),</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">do_set</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bApostrophe</span><span style="color: #0000FF;">=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">do_one</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'`'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bApostrophe</span><span style="color: #0000FF;">)}}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">re</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">265</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1025</span><span style="color: #0000FF;">}})</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #000000;">do_set</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">re</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">}),{</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">}})</span>
<!--

View file

@ -0,0 +1,7 @@
nth2(N) = N.to_string() ++ Th =>
( tween(N) -> Th = "th"
; 1 = N mod 10 -> Th = "st"
; 2 = N mod 10 -> Th = "nd"
; 3 = N mod 10 -> Th = "rd"
; Th = "th" ).
tween(N) => Tween = N mod 100, between(11, 13, Tween).

View file

@ -0,0 +1,7 @@
nth3(N) = cc(N,"th"), tween(N) => true.
nth3(N) = cc(N,"st"), N mod 10 = 1 => true.
nth3(N) = cc(N,"nd"), N mod 10 = 2 => true.
nth3(N) = cc(N,"rd"), N mod 10 = 3 => true.
nth3(N) = cc(N,"th") => true.
% helper function
cc(N,Th) = N.to_string() ++ Th.

View file

@ -0,0 +1,3 @@
nth4(N) = Nth =>
Suffix = ["th","st","nd","rd","th","th","th","th","th","th"],
Nth = N.to_string() ++ cond((N mod 100 <= 10; N mod 100 > 20), Suffix[1 + N mod 10], "th").

View file

@ -0,0 +1,5 @@
go =>
Ranges = [ 0..25, 250..265, 1000..1025],
foreach(Range in Ranges) println([nth2(I) : I in Range])
end,
nl.

18
Task/Nth/PicoLisp/nth.l Normal file
View file

@ -0,0 +1,18 @@
(de rangeth (A B)
(mapcar
'((I)
(pack I
(if (member (% I 100) (11 12 13))
'th
(case (% I 10)
(1 'st)
(2 'nd)
(3 'rd)
(T 'th) ) ) ) )
(range A B) ) )
(prinl (glue " " (rangeth 0 25)))
(prinl (glue " " (rangeth 250 265)))
(prinl (glue " " (rangeth 1000 1025)))
(bye)

View file

@ -0,0 +1,21 @@
function nth($inp){
$suffix = "th"
switch($inp % 100){
11{$suffix="th"}
12{$suffix="th"}
13{$suffix="th"}
default{
switch($inp % 10){
1{$suffix="st"}
2{$suffix="nd"}
3{$suffix="rd"}
}
}
}
return "$inp$suffix "
}
0..25 | %{Write-host -nonewline (nth "$_")};""
250..265 | %{Write-host -nonewline (nth "$_")};""
1000..1025 | %{Write-host -nonewline (nth "$_")};""

Some files were not shown because too many files have changed in this diff Show more