Numerical and alphabetical suffixes: Difference between revisions

m (→‎{{header|zkl}}: save a line)
Line 641:
9!!!!!!!!!: 9
.017k!!: 34,459,425</pre>
 
=={{header|Phix}}==
Using bigatom to get the full precision needed for the tests (esp the "Vikki" one).<br>
Note that comma-insertion was added to ba_sprintf() in version 0.8.0.
<lang Phix>include builtins/bigatom.e
{} = ba_scale(34) -- (min rqd for accuracy on the "Vikki" test)
-- (the default is 25, not quite enough here)
 
constant suffixes = {{"GREATGRoss",7,1728},
{"GOOGOLs",6,ba_new("1e100")},
{"SCOres",3,20},
{"DOZens",3,12},
{"GRoss",2,144},
{"PAIRs",4,2},
"KMGTPEZYXWVU"}
 
function decode(string suffix)
bigatom res = BA_ONE
suffix = upper(suffix)
while length(suffix)>0 do
bool found = false
for i=length(suffix) to 2 by -1 do
for s=1 to length(suffixes)-1 do
if i<=length(suffixes[s][1])
and i>=suffixes[s][2]
and suffix[1..i]=upper(suffixes[s][1][1..i]) then
res = ba_mul(res,suffixes[s][3])
suffix = suffix[i+1..$]
found = true
exit
end if
end for
if found then exit end if
end for
if not found then
integer k = find(suffix[1],suffixes[$])
if k=0 then ?9/0 end if
if length(suffix)>=2 and suffix[2]='I' then
res = ba_mul(res,ba_power(1024,k))
suffix = suffix[3..$]
else
res = ba_mul(res,ba_power(1000,k))
suffix = suffix[2..$]
end if
end if
end while
return res
end function
 
function facto(bigatom n, integer f)
if f!=0 then
bigatom nf = ba_sub(n,f)
while ba_compare(nf,2)>=0 do
n = ba_mul(n,nf)
nf = ba_sub(nf,f)
end while
end if
return n
end function
 
constant test_cases = """
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
1,567 +1.567k 0.1567e-2m
25.123kK 25.123m 2.5123e-00002G
25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
-.25123e-34Vikki 2e-77gooGols
9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
0.017k!!
"""
constant tests = split(substitute(test_cases,"\n"," "),no_empty:=true)
 
for i=1 to length(tests) do
string test = tests[i], suffix = "", facts = ""
for f=length(test) to 1 by -1 do
if test[f]!='!' then
{test,facts} = {test[1..f],test[f+1..$]}
exit
end if
end for
for d=length(test) to 1 by -1 do
integer digit = test[d]
if digit>='0' and digit<='9' then
{test,suffix} = {test[1..d],test[d+1..$]}
exit
end if
end for
bigatom n = ba_new(substitute(test,",",""))
n = ba_mul(n,decode(suffix))
n = facto(n,length(facts))
string ns = ba_sprintf("%,B",n)
printf(1,"%30s : %s\n",{tests[i],ns})
end for</lang>
{{out}}
<pre>
2greatGRo : 3,456
24Gros : 3,456
288Doz : 3,456
1,728pairs : 3,456
172.8SCOre : 3,456
1,567 : 1,567
+1.567k : 1,567
0.1567e-2m : 1,567
25.123kK : 25,123,000
25.123m : 25,123,000
2.5123e-00002G : 25,123,000
25.123kiKI : 26,343,374.848
25.123Mi : 26,343,374.848
2.5123e-00002Gi : 26,975,615.844352
+.25123E-7Ei : 28,964,846,960.237816578048
-.25123e-34Vikki : -33,394.194938104441474962344775423096782848
2e-77gooGols : 200,000,000,000,000,000,000,000
9! : 362,880
9!! : 945
9!!! : 162
9!!!! : 45
9!!!!! : 36
9!!!!!! : 27
9!!!!!!! : 18
9!!!!!!!! : 9
9!!!!!!!!! : 9
0.017k!! : 34,459,425
</pre>
 
=={{header|REXX}}==
7,830

edits