Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
74
Task/Number-names/Nim/number-names.nim
Normal file
74
Task/Number-names/Nim/number-names.nim
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import strutils, algorithm
|
||||
|
||||
const
|
||||
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
|
||||
"eighty", "ninety"]
|
||||
small = ["zero", "one", "two", "three", "four", "five", "six", "seven",
|
||||
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
|
||||
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
|
||||
huge = ["", "", "million", "billion", "trillion", "quadrillion",
|
||||
"quintillion", "sextillion", "septillion", "octillion", "nonillion",
|
||||
"decillion"]
|
||||
|
||||
proc spellInteger(n: int64): string
|
||||
|
||||
proc nonzero(c: string, n: int, connect=""): string =
|
||||
if n == 0: "" else: connect & c & spellInteger(n)
|
||||
|
||||
proc lastAnd(num): string =
|
||||
var num = num
|
||||
if "," in num:
|
||||
let pos = num.rfind(",")
|
||||
var (pre, last) =
|
||||
if pos >= 0: (num[0 .. pos-1], num[pos+1 .. num.high])
|
||||
else: ("", num)
|
||||
if " and " notin last:
|
||||
last = " and" & last
|
||||
num = [pre, ",", last].join()
|
||||
return num
|
||||
|
||||
proc big(e, n): string =
|
||||
if e == 0:
|
||||
spellInteger(n)
|
||||
elif e == 1:
|
||||
spellInteger(n) & " thousand"
|
||||
else:
|
||||
spellInteger(n) & " " & huge[e]
|
||||
|
||||
iterator base1000Rev(n): int =
|
||||
var n = n
|
||||
while n != 0:
|
||||
let r = n mod 1000
|
||||
n = n div 1000
|
||||
yield r
|
||||
|
||||
proc spellInteger(n: int64): string =
|
||||
if n < 0:
|
||||
"minus " & spellInteger(-n)
|
||||
elif n < 20:
|
||||
small[int(n)]
|
||||
elif n < 100:
|
||||
let a = n div 10
|
||||
let b = n mod 10
|
||||
tens[int(a)] & nonzero("-", b)
|
||||
elif n < 1000:
|
||||
let a = n div 100
|
||||
let b = n mod 100
|
||||
small[int(a)] & " hundred" & nonzero(" ", b, " and")
|
||||
else:
|
||||
var sq = newSeq[string]()
|
||||
var e = 0
|
||||
for x in base1000Rev(n):
|
||||
if x > 0:
|
||||
sq.add big(e, x)
|
||||
inc e
|
||||
reverse sq
|
||||
lastAnd(sq.join(", "))
|
||||
|
||||
for n in [0, -3, 5, -7, 11, -13, 17, -19, 23, -29]:
|
||||
echo align($n, 4)," -> ",spellInteger(n)
|
||||
|
||||
var n = 201021002001
|
||||
while n != 0:
|
||||
echo align($n, 14)," -> ",spellInteger(n)
|
||||
n = n div -10
|
||||
104
Task/Number-names/Phix/number-names.phix
Normal file
104
Task/Number-names/Phix/number-names.phix
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
constant twenties = {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
|
||||
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}
|
||||
|
||||
function Twenty(integer N)
|
||||
return twenties[mod(N,20)+1]
|
||||
end function
|
||||
|
||||
constant decades = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
|
||||
|
||||
function Decade(integer N)
|
||||
return decades[mod(N,10)-1]
|
||||
end function
|
||||
|
||||
function Hundred(integer N)
|
||||
if N<20 then
|
||||
return Twenty(N)
|
||||
elsif mod(N,10)=0 then
|
||||
return Decade(mod(floor(N/10),10))
|
||||
end if
|
||||
return Decade(floor(N/10)) & '-' & Twenty(mod(N,10))
|
||||
end function
|
||||
|
||||
function Thousand(integer N, string withand)
|
||||
if N<100 then
|
||||
return withand & Hundred (N);
|
||||
elsif mod(N,100)=0 then
|
||||
return withand & Twenty(floor(N/100))&" hundred"
|
||||
end if
|
||||
return Twenty(floor(N/100)) & " hundred and " & Hundred(mod(N,100))
|
||||
end function
|
||||
|
||||
constant orders = {{power(10,12),"trillion"},
|
||||
{power(10,9),"billion"},
|
||||
{power(10,6),"million"},
|
||||
{power(10,3),"thousand"}}
|
||||
|
||||
function Triplet(atom N)
|
||||
atom Order, High, Low
|
||||
string Name, res = ""
|
||||
integer n
|
||||
for i=1 to length(orders) do
|
||||
{Order,Name} = orders[i]
|
||||
High = floor(N/Order)
|
||||
Low = mod(N,Order)
|
||||
if High!=0 then
|
||||
res &= Thousand(High,"")&' '&Name
|
||||
end if
|
||||
N = Low
|
||||
if Low=0 then exit end if
|
||||
if length(res) and High!=0 then
|
||||
res &= ", "
|
||||
end if
|
||||
end for
|
||||
if N!=0 or res="" then
|
||||
res &= Thousand(floor(N),iff(res=""?"":"and "))
|
||||
N = abs(mod(N,1))
|
||||
if N>1e-6 then
|
||||
res &= " point"
|
||||
for i=1 to 10 do
|
||||
n = floor(N*10.0000001)
|
||||
res &= ' '&twenties[n+1]
|
||||
N = N*10-n
|
||||
if abs(N)<1e-6 then exit end if
|
||||
end for
|
||||
end if
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
function spell(atom N)
|
||||
string res = ""
|
||||
if N<0 then
|
||||
res = "minus "
|
||||
N = -N
|
||||
end if
|
||||
res &= Triplet(N)
|
||||
return res
|
||||
end function
|
||||
|
||||
constant Samples = {99, 300, 310, 417,1_501, 12_609, 200000000000100, 999999999999999,
|
||||
-123456787654321,102003000400005,1020030004,102003,102,1,0,-1,-99,
|
||||
-1501,1234,12.34,10000001.2,1E-3,-2.7182818}
|
||||
|
||||
function smartp(atom N)
|
||||
string res
|
||||
if N=floor(N) then return sprintf("%d",N) end if
|
||||
res = sprintf("%18.8f",N)
|
||||
if find('.',res) then
|
||||
res = trim_tail(res,"0")
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
atom si
|
||||
for i=1 to length(Samples) do
|
||||
si = Samples[i]
|
||||
printf(1,"%18s %s\n",{smartp(si),spell(si)})
|
||||
end for
|
||||
|
||||
si = 201021002001
|
||||
while si!=0 do
|
||||
printf(1,"%18s %s\n",{smartp(si),spell(si)})
|
||||
si = trunc(si/-10)
|
||||
end while
|
||||
75
Task/Number-names/Ring/number-names.ring
Normal file
75
Task/Number-names/Ring/number-names.ring
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
OneList=["zero", "one", "two", "three", "four",
|
||||
"five", "six", "seven", "eight", "nine",
|
||||
"ten", "eleven", "twelve", "thirteen", "fourteen",
|
||||
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
|
||||
tenList=["" , "" , "twenty", "thirty", "fourty",
|
||||
"fifty", "sixty", "seventy", "eighty", "ninety"]
|
||||
|
||||
millionStr="Million"
|
||||
thousandStr="Thousand"
|
||||
hundredStr="Hundred"
|
||||
andStr="And"
|
||||
pointStr=" Point "
|
||||
|
||||
while true
|
||||
see "enter number to convert:"
|
||||
give theNumber
|
||||
|
||||
pointSplited=splitString(theNumber,".")
|
||||
fraction=0
|
||||
|
||||
useFr=false
|
||||
if len(pointSplited) >=1 theNumber=pointSplited[1] ok
|
||||
if len(pointSplited) >=2 useFr=true fraction=pointSplited[2] ok
|
||||
pointSplited=null
|
||||
|
||||
see getName(number(theNumber))
|
||||
if useFr=true see pointStr + getName(number(fraction)) ok
|
||||
see nl
|
||||
end
|
||||
|
||||
func getName num
|
||||
rtn=null
|
||||
if num=0
|
||||
rtn += OneList[floor(num+1)]
|
||||
return rtn
|
||||
ok
|
||||
if num<0
|
||||
return "minus " + getName(fabs(num))
|
||||
ok
|
||||
if num>= 1000000
|
||||
rtn += getName(num / 1000000) +" "+ millionStr
|
||||
num%=1000000
|
||||
ok
|
||||
if num>=1000
|
||||
if len(rtn)>0 rtn += ", " ok
|
||||
|
||||
rtn += getName(num / 1000)+ " " + thousandStr
|
||||
num%=1000
|
||||
ok
|
||||
|
||||
if num >=100
|
||||
if len(rtn)>0 rtn += ", " ok
|
||||
rtn += OneList[floor((num / 100)+1)] + " " + hundredStr
|
||||
num%=100
|
||||
ok
|
||||
|
||||
if num=0
|
||||
return rtn +
|
||||
ok
|
||||
if len(rtn)>0 rtn += " " + andStr + " " ok
|
||||
if(num>=20)
|
||||
|
||||
rtn += tenList[floor((num / 10)+1)]
|
||||
num%=10
|
||||
ok
|
||||
if num=0
|
||||
return rtn
|
||||
ok
|
||||
if len(rtn)>0 rtn += " " ok
|
||||
rtn += OneList[num+1]
|
||||
return rtn
|
||||
|
||||
func splitString str,chr
|
||||
for i in str if strcmp(i,chr)=0 i=nl ok next
|
||||
return str2list(str)
|
||||
61
Task/Number-names/SequenceL/number-names.sequencel
Normal file
61
Task/Number-names/SequenceL/number-names.sequencel
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import <Utilities/Math.sl>;
|
||||
import <Utilities/Sequence.sl>;
|
||||
import <Utilities/Conversion.sl>;
|
||||
import <Utilities/String.sl>;
|
||||
|
||||
main(argv(2)) := delimit(numberToEnglish(stringToInt(argv)), '\n');
|
||||
|
||||
ones := ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
|
||||
teens := ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
|
||||
tens := ["ten", "twenty", "thrity", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
|
||||
|
||||
magnitudes := ["", "thousand", "million", "billion", "trillion"];
|
||||
|
||||
numberToEnglish(num(0)) :=
|
||||
let
|
||||
triplets[i] :=
|
||||
(num / integerPower(1000, i - 1)) mod 1000
|
||||
foreach i within 1 ... ceiling(log(1000, num + 1));
|
||||
|
||||
englishtTriplets[j] := numberToEnglishHelper(triplets[j]);
|
||||
|
||||
partials[j] :=
|
||||
englishtTriplets[j] ++ magnitudes[j] ++ ", " when size(englishtTriplets[j]) > 0
|
||||
foreach j within reverse(1 ... size(triplets));
|
||||
in
|
||||
"zero" when num = 0
|
||||
else
|
||||
"negative " ++ numberToEnglish(-num) when num < 0
|
||||
else
|
||||
trim(allButLast(trim(join(partials))));
|
||||
|
||||
|
||||
numberToEnglishHelper(num(0)) :=
|
||||
let
|
||||
onesPlace := num mod 10;
|
||||
tensPlace := (num mod 100) / 10;
|
||||
hundredsPlace := (num mod 1000) / 100;
|
||||
|
||||
onesWord := "ten " when tensPlace = 1 and onesPlace = 0
|
||||
else
|
||||
"" when onesPlace = 0
|
||||
else
|
||||
teens[onesPlace] ++ " " when tensPlace = 1
|
||||
else
|
||||
ones[onesPlace] ++ " ";
|
||||
|
||||
tensWord := "" when tensPlace = 0 or tensPlace = 1
|
||||
else
|
||||
tens[tensPlace] ++ " " when onesPlace = 0
|
||||
else
|
||||
tens[tensPlace] ++ "-";
|
||||
|
||||
hundredsWord := "" when hundredsPlace = 0
|
||||
else
|
||||
ones[hundredsPlace] ++ " hundred ";
|
||||
|
||||
andWord := "" when hundredsPlace = 0 or (tensPlace = 0 and onesPlace = 0) else "and ";
|
||||
|
||||
|
||||
in
|
||||
hundredsWord ++ andWord ++ tensWord ++ onesWord;
|
||||
2
Task/Number-names/Sidef/number-names.sidef
Normal file
2
Task/Number-names/Sidef/number-names.sidef
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var l = frequire('Lingua::EN::Numbers');
|
||||
say l.num2en(123456789);
|
||||
61
Task/Number-names/jq/number-names-1.jq
Normal file
61
Task/Number-names/jq/number-names-1.jq
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# Adapted from the go version.
|
||||
# Tested with jq 1.4
|
||||
#
|
||||
# say/0 as defined here supports positive and negative integers within
|
||||
# the range of accuracy of jq, or up to the quintillions, whichever is
|
||||
# less. As of jq version 1.4, jq's integer accuracy is about 10^16.
|
||||
|
||||
def say:
|
||||
|
||||
# subfunction zillions recursively handles the thousands,
|
||||
# millions, billions, etc.
|
||||
# input: the number
|
||||
# i: which "illion" to use
|
||||
# sx: the string so far
|
||||
# output: the updated string
|
||||
def zillions(i; sx):
|
||||
["thousand", "million", "billion",
|
||||
"trillion", "quadrillion", "quintillion"] as $illions
|
||||
| if . == 0 then sx
|
||||
else (. / 1000 | floor)
|
||||
| (. % 1000) as $p
|
||||
| zillions(i + 1;
|
||||
if $p > 0 then
|
||||
(($p | say) + " " + $illions[i]) as $ix
|
||||
| if sx != "" then $ix + ", " + sx
|
||||
else $ix
|
||||
end
|
||||
else sx
|
||||
end)
|
||||
end
|
||||
;
|
||||
|
||||
[ "", "one", "two", "three", "four", "five", "six", "seven",
|
||||
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
|
||||
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] as $small
|
||||
| ["ones", "ten", "twenty", "thirty", "forty",
|
||||
"fifty", "sixty", "seventy", "eighty", "ninety"] as $tens
|
||||
|
||||
| if . == 0 then "zero"
|
||||
elif . < 0 then "minus " + (-(.) | say)
|
||||
elif . < 20 then $small[.]
|
||||
elif . < 100 then
|
||||
$tens[./10|floor] as $t
|
||||
| (. % 10)
|
||||
| if . > 0 then ($t + " " + $small[.]) else $t end
|
||||
elif . < 1000 then
|
||||
($small[./100|floor] + " hundred") as $h
|
||||
| (. % 100)
|
||||
| if . > 0 then $h + " and " + (say) else $h end
|
||||
else
|
||||
# Handle values larger than 1000 by considering
|
||||
# the rightmost three digits separately from the rest:
|
||||
((. % 1000)
|
||||
| if . == 0 then ""
|
||||
elif . < 100 then "and " + say
|
||||
else say
|
||||
end ) as $sx
|
||||
| zillions(0; $sx)
|
||||
end ;
|
||||
|
||||
say
|
||||
16
Task/Number-names/jq/number-names-2.jq
Normal file
16
Task/Number-names/jq/number-names-2.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
0
|
||||
"zero"
|
||||
-0
|
||||
"zero"
|
||||
111
|
||||
"one hundred and eleven"
|
||||
1230000
|
||||
"one million, two hundred and thirty thousand"
|
||||
123456
|
||||
"one hundred and twenty three thousand, four hundred and fifty six"
|
||||
123456789
|
||||
"one hundred and twenty three million, four hundred and fifty six thousand, seven hundred and eighty nine"
|
||||
-123000000123
|
||||
"minus one hundred and twenty three billion, one hundred and twenty three"
|
||||
12345678912345678
|
||||
"twelve quadrillion, three hundred and forty five trillion, six hundred and seventy eight billion, nine hundred and twelve million, three hundred and forty five thousand, six hundred and seventy eight"
|
||||
Loading…
Add table
Add a link
Reference in a new issue