June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,4 +1,4 @@
-- ORDINAL STRINGS
-- ORDINAL STRINGS -----------------------------------------------------------
-- ordinalString :: Int -> String
on ordinalString(n)
@ -16,50 +16,26 @@ on ordinalSuffix(n)
end if
end ordinalSuffix
-- TEST
-- TEST ----------------------------------------------------------------------
on run
-- showOrdinals :: [Int] -> [String]
script showOrdinals
on lambda(lstInt)
on |λ|(lstInt)
map(ordinalString, lstInt)
end lambda
end |λ|
end script
map(showOrdinals, ¬
map(tupleRange, ¬
map(uncurry(enumFromTo), ¬
[[0, 25], [250, 265], [1000, 1025]]))
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- 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
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
@ -69,7 +45,19 @@ on range(m, n)
set end of lst to i
end repeat
return lst
end range
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
@ -78,7 +66,17 @@ on mReturn(f)
f
else
script
property lambda : f
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

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 ((11,12,13) ifExists(i mod:100))
[
^ i literal + "th".
].
(i mod:10) =>
1 [ ^ i literal + "st" ];
2 [ ^ i literal + "nd" ];
3 [ ^ i literal + "rd" ].
^ i literal + "th".
]
}
program =
[
console printLine(Range new(0,26); selectBy:%"op.ordinalize").
console printLine(Range new(250,26); selectBy:%"op.ordinalize").
console printLine(Range new(1000,26); selectBy:%"op.ordinalize").
].

View file

@ -74,8 +74,10 @@ contains
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

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

@ -1,12 +1,12 @@
function sprintfordinal{T<:Integer}(n::T)
const sfixes = ["st", "nd", "rd"]
0 <= n || throw(ArgumentError("number to be formatted must be ≥ 0, got $n"))
u = n%10
t = div(n, 10)%10
if 3 < u || u == 0 || t == 1
sf = "th"
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
sf = sfixes[u]
suf = suffixes[u]
end
@sprintf "%d%s" n sf
return string(n, suf)
end

View file

@ -1,24 +1,17 @@
println("Tests of ordinal formatting of integers.")
for (i, n) in enumerate(0:25)
if (i-1)%10 == 0
print("\n ")
end
print(@sprintf("%7s", sprintfordinal(n)))
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
println()
println()
for (i, n) in enumerate(250:265)
if (i-1)%10 == 0
print("\n ")
end
print(@sprintf("%7s", sprintfordinal(n)))
(i - 1) % 10 == 0 && println()
@printf("%7s", ordinal(n))
end
println()
for (i, n) in enumerate(1000:1025)
if (i-1)%10 == 0
print("\n ")
end
print(@sprintf("%7s", sprintfordinal(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,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

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

18
Task/Nth/Stata/nth.stata Normal file
View file

@ -0,0 +1,18 @@
mata
function maps(f,a) {
nr = rows(a)
nc = cols(a)
b = J(nr,nc,"")
for (i=1;i<=nr;i++) {
for (j=1;j<=nc;j++) b[i,j] = (*f)(a[i,j])
}
return(b)
}
function nth(n) {
k = max((min((mod(n-1,10)+1,4)),4*(mod(n-10,100)<10)))
return(strofreal(n)+("st","nd","rd","th")[k])
}
maps(&nth(),((0::25),(250::275),(1000::1025)))
end