Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

8
Task/Nth/00DESCRIPTION Normal file
View file

@ -0,0 +1,8 @@
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]].
<br>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.

4
Task/Nth/00META.yaml Normal file
View file

@ -0,0 +1,4 @@
---
category:
- String manipulation
note: Ordinal numbers

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 )
)

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)
}

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,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,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,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

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 }))

52
Task/Nth/C/nth.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;
}

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,15 @@
(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)))
(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))))

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);
}

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 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

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

@ -0,0 +1,89 @@
!-*- 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)
else
ordinate = a // "'" // a09(ones_index:ones_index+1)
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

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

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

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

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

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,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;

View file

@ -0,0 +1,5 @@
my %irregulars = <1 st 2 nd 3 rd>, (11..13 X=> 'th');
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'th' ) }
say .list».&nth for [^26], [250..265], [1000..1025];

View file

@ -0,0 +1,5 @@
my %irregulars = <1 ˢᵗ 2 ⁿᵈ 3 ʳᵈ>, (11..13 X=> 'ʰ');
sub nth ($n) { $n ~ ( %irregulars{$n % 100} // %irregulars{$n % 10} // 'ʰ' ) }
say .list».&nth for [^26], [250..265], [1000..1025];

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

@ -0,0 +1,15 @@
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/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)

16
Task/Nth/Prolog/nth.pro Normal file
View file

@ -0,0 +1,16 @@
nth(N, N_Th) :-
( tween(N) -> Th = "th"
; 1 is N mod 10 -> Th = "st"
; 2 is N mod 10 -> Th = "nd"
; 3 is N mod 10 -> Th = "rd"
; Th = "th" ),
string_concat(N, Th, N_Th).
tween(N) :- Tween is N mod 100, between(11, 13, Tween).
test :-
forall( between(0,25, N), (nth(N, N_Th), format('~w, ', N_Th)) ),
nl, nl,
forall( between(250,265,N), (nth(N, N_Th), format('~w, ', N_Th)) ),
nl, nl,
forall( between(1000,1025,N), (nth(N, N_Th), format('~w, ', N_Th)) ).

8
Task/Nth/Python/nth.py Normal file
View file

@ -0,0 +1,8 @@
_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))))

18
Task/Nth/R/nth.r Normal file
View file

@ -0,0 +1,18 @@
nth <- function(n)
{
if (length(n) > 1) return(sapply(n, nth))
mod <- function(m, n) ifelse(!(m%%n), n, m%%n)
suffices <- c("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")
if (n %% 100 <= 10 || n %% 100 > 20)
suffix <- suffices[mod(n+1, 10)]
else
suffix <- 'th'
paste(n, "'", suffix, sep="")
}
range <- list(0:25, 250:275, 500:525, 750:775, 1000:1025)
sapply(range, nth)

1
Task/Nth/README Normal file
View file

@ -0,0 +1 @@
Data source: http://rosettacode.org/wiki/N'th

17
Task/Nth/REXX/nth.rexx Normal file
View file

@ -0,0 +1,17 @@
/*REXX pgm shows ranges of numbers with ordinal (st/nd/rd/th) suffixes.*/
call tell 0, 25 /*show the 1st range of numbers. */
call tell 250, 265 /* " " 2nd " " " */
call tell 1000, 1025 /* " " 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 blank line*/
return /*return to invoker.*/
/*──────────────────────────────────TH subroutine───────────────────────*/
th: procedure; parse arg x; x=abs(x) /*just use ABS value*/
return word('th st nd rd', 1 + x//10 * (x//100%10 \== 1) * (x//10 < 4) )

10
Task/Nth/Racket/nth.rkt Normal file
View file

@ -0,0 +1,10 @@
#lang racket
(define (teen? n) (<= 11 (modulo n 100) 19))
(define (Nth n)
(format
"~a'~a" n
(match* ((modulo n 10) n)
[((or 1 2 3) (? teen?)) 'th] [(1 _) 'st] [(2 _) 'nd] [(3 _) 'rd] [(_ _) 'th])))
(for ((range (list (in-range 26) (in-range 250 266) (in-range 1000 1026))))
(displayln (string-join (for/list ((nth (sequence-map Nth range))) nth) " ")))

18
Task/Nth/Ruby/nth.rb Normal file
View file

@ -0,0 +1,18 @@
class Integer
def ordinalize
num = self.abs
ordinal = if (11..13).include?(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(&:ordinalize).join(", "); puts}

26
Task/Nth/Rust/nth.rust Normal file
View file

@ -0,0 +1,26 @@
fn nth(num: int) -> String {
format!("{}{}", num, match (num % 10, num % 100) {
(1, 11) => "th",
(1, _) => "st",
(2, 12) => "th",
(2, _) => "nd",
(3, 13) => "th",
(3, _) => "rd",
_ => "th"
})
}
fn main() {
let ranges = vec![
(0i, 26i),
(250i, 266i),
(1000i, 1026i)
];
for &(s, e) in ranges.iter() {
println!("[{}, {}) :", s, e);
for i in range(s, e) {
print!("{}, ", nth(i));
}
println!("");
}
}

20
Task/Nth/Scala/nth.scala Normal file
View file

@ -0,0 +1,20 @@
object Nth extends App {
def abbrevNumber(i: Int) = print(s"$i${ordinalAbbrev(i)} ")
def ordinalAbbrev(n: Int) = {
val ans = "th" //most of the time it should be "th"
if (n % 100 / 10 == 1) ans //teens are all "th"
else (n % 10) match {
case 1 => "st"
case 2 => "nd"
case 3 => "rd"
case _ => ans
}
}
(0 to 25).foreach(abbrevNumber)
println()
(250 to 265).foreach(abbrevNumber)
println();
(1000 to 1025).foreach(abbrevNumber)
}

29
Task/Nth/Seed7/nth.seed7 Normal file
View file

@ -0,0 +1,29 @@
$ include "seed7_05.s7i";
const func string: suffix (in integer: num) is func
result
var string: suffix is "";
begin
if num rem 10 = 1 and num rem 100 <> 11 then suffix := "st";
elsif num rem 10 = 2 and num rem 100 <> 12 then suffix := "nd";
elsif num rem 10 = 3 and num rem 100 <> 13 then suffix := "rd";
else suffix := "th";
end if;
end func;
const proc: printImages (in integer: start, in integer: stop) is func
local
var integer: num is 0;
begin
for num range start to stop do
write(num <& suffix(num) <& " ");
end for;
writeln;
end func;
const proc: main is func
begin
printImages( 0, 25);
printImages( 250, 265);
printImages(1000, 1025);
end func;

15
Task/Nth/Tcl/nth.tcl Normal file
View file

@ -0,0 +1,15 @@
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
}