2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,84 @@
-- 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 lambda(lstInt)
map(ordinalString, lstInt)
end lambda
end script
map(showOrdinals, ¬
map(tupleRange, ¬
[[0, 25], [250, 265], [1000, 1025]]))
end run
-- GENERIC FUNCTIONS
-- 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 lambda(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- tupleRange :: (Int, Int) -> [Int]
on tupleRange(lstPair)
set {m, n} to lstPair
range(m, n)
end tupleRange
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m 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 range
-- 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 lambda : f
end script
end if
end mReturn

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

@ -1,21 +0,0 @@
(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)))
(defun add-suffix (n)
"A more concise, albeit less readable version"
(format nil "~d'~:[~[th~;st~;nd~;rd~:;th~]~;th~]" n (< (mod (- n 10) 100) 10) (mod n 10)))
(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,28 @@
USING: math kernel combinators math.ranges math.parser
sequences io ;
IN: nth
: tens-digit ( n -- n ) 10 [ /i ] [ mod ] bi ;
: teens? ( n -- ? ) tens-digit 1 = ;
: non-teen-suffix ( n -- str )
10 mod {
{ 1 [ "st" ] }
{ 2 [ "nd" ] }
{ 3 [ "rd" ] }
[ drop "th" ]
} case ;
: ordinal-suffix ( n -- str )
dup teens? [ drop 0 ] when non-teen-suffix ;
: join-suffix ( n -- str ) dup ordinal-suffix
[ number>string ] dip append ;
: main ( -- )
0 25 250 265 1000 1025 [ [a,b] ] 2tri@
[ [ join-suffix ] map ] tri@
[ [ bl ] [ write ] interleave nl ] tri@ ;
MAIN: main

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

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

@ -1,16 +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"
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
function Nth (n) return n .. "'" .. getSuffix(n) end
for n = 0, 25 do
print(Nth(n), Nth(n + 250), Nth(n + 1000))
end
for i = 0, 25 do print(Nth(i), Nth(i + 250), Nth(i + 1000)) 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,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.

View file

@ -0,0 +1,17 @@
function Get-Nth ([int]$Number)
{
$suffix = "th"
switch ($Number % 10)
{
1 {$suffix = "st"}
2 {$suffix = "nd"}
3 {$suffix = "rd"}
}
"$Number$suffix"
}
1..25 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
251..265 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force
1001..1025 | ForEach-Object {Get-Nth $_} | Format-Wide {$_} -Column 5 -Force

7
Task/Nth/SQL/nth.sql Normal file
View file

@ -0,0 +1,7 @@
select level card,
to_char(to_date(level,'j'),'fmjth') ord
from dual
connect by level <= 15;
select to_char(to_date(5373485,'j'),'fmjth')
from dual;