September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

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}

View file

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

View file

@ -1,28 +1,14 @@
USING: math kernel combinators math.ranges math.parser
sequences io ;
IN: nth
USING: io kernel math math.order math.parser math.ranges qw
sequences ;
IN: rosetta-code.nth
: tens-digit ( n -- n ) 10 [ /i ] [ mod ] bi ;
: 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 ;
: 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 ( -- )
: n'th-demo ( -- )
0 25 250 265 1000 1025 [ [a,b] ] 2tri@
[ [ join-suffix ] map ] tri@
[ [ bl ] [ write ] interleave nl ] tri@ ;
[ [ n'th write bl ] each nl ] tri@ ;
MAIN: main
MAIN: n'th-demo

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

20
Task/Nth/VBA/nth.vba Normal file
View file

@ -0,0 +1,20 @@
Private Function ordinals() As Variant
ordinals = [{"th","st","nd","rd"}]
End Function
Private Function Nth(n As Variant, Optional apostrophe As Boolean = False) As String
Dim mod10 As Integer: mod10 = n Mod 10 + 1
If mod10 > 4 Or n Mod 100 = mod10 + 9 Then mod10 = 1
Nth = CStr(n) & String$(Val(-apostrophe), "'") & ordinals()(mod10)
End Function
Public Sub main()
Ranges = [{0,25;250,265;1000,1025}]
For i = 1 To UBound(Ranges)
For j = Ranges(i, 1) To Ranges(i, 2)
If j Mod 10 = 0 Then Debug.Print
Debug.Print Format(Nth(j, i = 2), "@@@@@@@");
Next j
Debug.Print
Next i
End Sub