Numerical and alphabetical suffixes: Difference between revisions

m
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 7 users not shown)
Line 96:
··· the number of factorial symbols that can be specified is to be unlimited   (as per what can be entered/typed) ···
 
 
Note that these factorial products aren't   ''super─factorials''   where (4!)! would be (24)!.
 
Factorial suffixes aren't, of course, the usual type of multipliers, but are used here in a similar vein.
Line 127 ⟶ 125:
 
;Related tasks:
:*   [[Multifactorial]]                 (which has a clearer and more succinct definition of multifactorials.)
:*   [[Factorial]]
 
:*   [[Abbreviations, simple]]
 
:*   [[Abbreviations, easy]]
{{Template:Strings}}
:*   [[Abbreviations, automatic]]
:*   [[Commatizing numbers]]
:*   [[Longest common prefix]]
<br><br>
 
=={{header|Factor}}==
===Functional===
<langsyntaxhighlight lang="factor">USING: combinators combinators.short-circuit formatting fry
grouping grouping.extras kernel literals math math.functions
math.parser math.ranges qw regexp sequences sequences.deep
Line 247 ⟶ 243:
] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 271 ⟶ 267:
===EBNF===
This solution uses Factor's extended Backus-Naur form (EBNF) language to define a grammar for parsing numerical/alphabetical suffix numbers. The goal was to describe as much of the suffix-number as possible in a declarative manner, minimizing the use of actions (Factor code that is run on a rule before being added to the abstract syntax tree) and helper functions. The biggest departure from this goal was to parse the metric/binary suffixes based on their index in a collection, as this method is less verbose than defining a rule for each suffix.
<langsyntaxhighlight lang="factor">USING: formatting fry grouping kernel literals math
math.functions math.parser math.ranges multiline peg.ebnf
quotations qw sequences sequences.deep splitting strings unicode ;
Line 334 ⟶ 330:
] each ;
MAIN: num-alpha-suffix-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 357 ⟶ 353:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 521 ⟶ 517:
"9!!!!!!!", "9!!!!!!!!", "9!!!!!!!!!"}
process(numbers)
}</langsyntaxhighlight>
 
{{out}}
Line 545 ⟶ 541:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
 
partialsuffixes = Dict("PAIR" => "PAIRS", "SCO" => "SCORES", "DOZ" => "DOZENS",
Line 632 ⟶ 628:
 
testsuffixes()
</langsyntaxhighlight>{{out}}
<pre>
Testing: ["2greatGRo", "24Gros", "288Doz", "1,728pairs", "172.8SCOre"]
Line 653 ⟶ 649:
</pre>
 
=={{header|PhixNim}}==
<syntaxhighlight lang="nim">import re, strutils, parseutils, tables, math
Using bigatom to get the full precision needed for the tests (esp the "Vikki" one).<br>
const input = """
Note that comma-insertion was added to ba_sprintf() in version 0.8.0.
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
<lang Phix>include builtins/bigatom.e
1,567 +1.567k 0.1567e-2m
{} = ba_scale(34) -- (min rqd for accuracy on the "Vikki" test)
25.123kK 25.123m 2.5123e-00002G
-- (the default is 25, not quite enough here)
25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
-.25123e-34Vikki 2e-77gooGols
9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
"""
let
abbrAlpha = re(r"(PAIR)(s)?$|(SCO)(r|re|res)?$|(DOZ)(e|en|ens)?$|(GR)(o|os|oss)?$|(GREATGR)(o|os|oss)?$|(GOOGOL)(s)?$",
flags = {reIgnoreCase})
metricBinary = re(r"[KMGTPEZYXWVU]i?", flags = {reIgnoreCase})
factorial = re"!+$"
const
alphaValues = [2e0, 20e0, 12e0, 144e0, 1728e0, 1e100]
metricBinaryValueTab = {"K": 10.0^3, "KI": 2.0^10, "M": 10.0^6, "MI": 2.0^20,
"G": 10.0^9, "GI": 2.0^30, "T": 10.0^12, "TI": 2.0^40, "P": 10.0^15,
"PI": 2.0^50, "E": 10.0^18, "EI": 2.0^60, "Z": 10.0^21, "ZI": 2.0^70,
"Y": 10.0^24, "YI": 2.0^80, "X": 10.0^27, "XI": 2.0^90, "W": 10.0^30,
"WI": 2.0^100, "V": 10.0^33, "VI": 2.0^110, "U": 10.0^36,
"UI": 2.0^120}.toTable
var
matches: array[12, string]
res, fac: float
suffix: string
proc sepFloat(f: float): string =
var
s = $f
i, num: int
num = parseInt(s, i)
if num == 0:
return s
else:
return insertSep($i, ',', 3)&s[num..^1]
for line in input.splitLines():
if not isEmptyOrWhiteSpace(line):
echo("Input:\n", line, "\nOutput:")
var output = " "
for raw_number in line.replace(",", "").splitWhiteSpace():
suffix = raw_number[parseutils.parseFloat(raw_number, res)..^1].toUpper()
if suffix.match(abbrAlpha, matches):
for i, v in matches.mpairs():
if i mod 2 == 0 and not v.isEmptyOrWhiteSpace():
res*=alphaValues[i div 2]
v = ""
break
elif suffix.match(factorial):
fac = abs(res)-toFloat(len(suffix))
while fac > 0:
res*=fac
fac-=toFloat(len(suffix))
elif suffix.match(metricBinary):
for m in suffix.findAll(metricBinary):
res*=metricBinaryValueTab[m]
output &= sepFloat(res)&" "
echo(output)
</syntaxhighlight>
{{out}}<pre>
Input:
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
Output:
3,456.0 3,456.0 3,456.0 3,456.0 3,456.0
Input:
1,567 +1.567k 0.1567e-2m
Output:
1,567.0 1,567.0 1,567.0
Input:
25.123kK 25.123m 2.5123e-00002G
Output:
25,123,000.0 25,123,000.0 25,123,000.0
Input:
25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
Output:
26,343,374.848 26,343,374.848 26,975,615.844352 28,964,846,960.23782
Input:
-.25123e-34Vikki 2e-77gooGols
Output:
-33,394.19493810444 2e+23
Input:
9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
Output:
362,880.0 945.0 162.0 45.0 36.0 27.0 18.0 9.0 9.0
</pre>
 
=={{header|Perl}}==
constant suffixes = {{"GREATGRoss",7,1728},
<syntaxhighlight lang="perl">#!/usr/bin/perl
{"GOOGOLs",6,ba_new("1e100")},
{"SCOres",3,20},
{"DOZens",3,12},
{"GRoss",2,144},
{"PAIRs",4,2},
"KMGTPEZYXWVU"}
 
use strict; # https://rosettacode.org/wiki/Numerical_and_alphabetical_suffixes
function decode(string suffix)
use warnings;
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
 
my %suffix = qw( k 1e3 m 1e6 g 1e9 t 1e12 p 1e15 e 1e18 z 1e21 y 1e24 x 1e27
function facto(bigatom n, integer f)
w 1e30 v 1e33 u 1e36 ki 2**10 mi 2**20 gi 2**30 ti 2**40 pi 2**50 ei 2**60
if f!=0 then
zi 2**70 yi 2**80 xi 2**90 wi 2**100 vi 2**110 ui 2**120 );
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
 
local $" = ' '; # strange rule...
constant test_cases = """
print "numbers = ${_}results = @{[ map suffix($_), split ]}\n\n" while <DATA>;
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
 
1,567 +1.567k 0.1567e-2m
sub suffix
25.123kK 25.123m 2.5123e-00002G
{
my ($value, $mods) = shift =~ tr/,//dr =~ /([+-]?[\d.]+(?:e[+-]\d+)?)(.*)/i;
$value *= $^R while $mods =~ /
PAIRs? (?{ 2 })
| SCO(re?)? (?{ 20 })
| DOZ(e(ns?)?)? (?{ 12 })
| GREATGR(o(ss?)?)? (?{ 1728 }) # must be before GRoss
| GR(o(ss?)?)? (?{ 144 })
| GOOGOLs? (?{ 10**100 })
| [kmgtpezyxwvu]i? (?{ eval $suffix{ lc $& } })
| !+ (?{ my $factor = $value;
$value *= $factor while ($factor -= length $&) > 1;
1 })
/gix;
return $value =~ s/(\..*)|\B(?=(\d\d\d)+(?!\d))/$1 || ','/ger;
}
 
__DATA__
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!!!!!!!!!</syntaxhighlight>
{{out}}
0.017k!!
<pre>
"""
numbers = 2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
constant tests = split(substitute(test_cases,"\n"," "),no_empty:=true)
results = 3,456 3,456 3,456 3,456 3,456
 
numbers = 1,567 +1.567k 0.1567e-2m
for i=1 to length(tests) do
results = 1,567 1,567 1,567
string test = tests[i], suffix = "", facts = ""
 
for f=length(test) to 1 by -1 do
numbers = 25.123kK 25.123m 2.5123e-00002G
if test[f]!='!' then
results = 25,123,000 25,123,000 25,123,000
{test,facts} = {test[1..f],test[f+1..$]}
 
exit
numbers = 25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
end if
results = 26,343,374.848 26,343,374.848 26,975,615.844352 28,964,846,960.2378
end for
 
for d=length(test) to 1 by -1 do
numbers = -.25123e-34Vikki 2e-77gooGols
integer digit = test[d]
results = -33,394.1949381044 2e+23
if digit>='0' and digit<='9' then
 
{test,suffix} = {test[1..d],test[d+1..$]}
numbers = 9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
exit
results = 362,880 945 162 45 36 27 18 9 9
end if
 
end for
</pre>
bigatom n = ba_new(substitute(test,",",""))
 
n = ba_mul(n,decode(suffix))
=={{header|Phix}}==
n = facto(n,length(facts))
Using bigatom to get the full precision needed for the tests (esp the "Vikki" one).<br>
string ns = ba_sprintf("%,B",n)
Note that comma-insertion was added to ba_sprintf() in version 0.8.0.
printf(1,"%30s : %s\n",{tests[i],ns})
<!--<syntaxhighlight lang="phix">(phixonline)-->
end for</lang>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">bigatom</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_scale</span><span style="color: #0000FF;">(</span><span style="color: #000000;">34</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (min rqd for accuracy on the "Vikki" test)
-- (the default is 25, not quite enough here)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">suffixes</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"GREATGRoss"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1728</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"GOOGOLs"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ba_new</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1e100"</span><span style="color: #0000FF;">)},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"SCOres"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"DOZens"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"GRoss"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">144</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"PAIRs"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span>
<span style="color: #008000;">"KMGTPEZYXWVU"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">suffix</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">bigatom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">BA_ONE</span>
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">suffix</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">suffix</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
<span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">found</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">found</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">suffixes</span><span style="color: #0000FF;">[$])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">2</span> <span style="color: #008080;">and</span> <span style="color: #000000;">suffix</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'I'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ba_power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1024</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">suffix</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ba_power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">suffix</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">facto</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bigatom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">bigatom</span> <span style="color: #000000;">nf</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">ba_compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nf</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nf</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">nf</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nf</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">test_cases</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
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!!
"""</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test_cases</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">),</span><span style="color: #000000;">no_empty</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">test</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">suffix</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">facts</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">f</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'!'</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">test</span><span style="color: #0000FF;">,</span><span style="color: #000000;">facts</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">f</span><span style="color: #0000FF;">],</span><span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">f</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]}</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">digit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'9'</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">test</span><span style="color: #0000FF;">,</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">d</span><span style="color: #0000FF;">],</span><span style="color: #000000;">test</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]}</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">bigatom</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_new</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">,</span><span style="color: #008000;">","</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">decode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suffix</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">facto</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">facts</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ba_sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%,B"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%30s : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">ns</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 777 ⟶ 922:
=={{header|Python}}==
{{works with|CPython|3.7.3}}
<syntaxhighlight lang="python">
<lang Python>
from functools import reduce
from operator import mul
Line 855 ⟶ 1,000:
print("Input:", ' '.join(test_cases))
print("Output:", ' '.join(format(result, ',f').strip('0').strip('.') for result in map(expand, test_cases)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 875 ⟶ 1,020:
(formerly Perl 6)
{{works with|Rakudo|2018.09}}
Scientific notation, while supported in Perl 6Raku, is limited to IEEE-754 64bit accuracy so there is some rounding on values using it. Implements a custom "high precision" conversion routine.
 
Unfortunately, this suffix routine is of limited use for practical everyday purposes. It focuses on handling excessively large and archaic units (googol, greatgross) and completely ignores or makes unusable (due to forcing case insensitivity) many common current ones: c(centi), m(milli), μ(micro). Ah well.
Line 881 ⟶ 1,026:
Note: I am blatantly and deliberately ignoring the task guidelines for formatting the output. It has no bearing on the core of the task. If you really, ''really'','' '''REALLY''' ''want to see badly formatted output, uncomment the last line.
 
<syntaxhighlight lang="raku" perl6line>use Rat::Precise;
 
my $googol = 10**100;
Line 941 ⟶ 1,086:
 
# Task required stupid layout
# say "\n In: $_\nOut: ", .words.map({comma .&units}).join(' ') for $test.lines;</langsyntaxhighlight>
{{out}}
<pre> 2greatGRo: 3,456
Line 972 ⟶ 1,117:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm converts numbers (with commas) with suffix multipliers──►pure decimal numbers*/
numeric digits 2000 /*allow the usage of ginormous numbers.*/
@.=; @.1= '2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre'
Line 1,044 ⟶ 1,189:
if \isNum(x) then x= $sfx!(x); if isNum(x) then return x/1
if q==1 then return x
if q=='' then call ser "argument isn't numeric or doesn't have a legal suffix:" x</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,064 ⟶ 1,209:
numbers= 9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
result= 362,880 945 162 45 36 27 18 9 9
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-big}}
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./big" for BigRat
import "./str" for Str
import "./fmt" for Fmt
 
var abbrevs = {
"PAIRs": [4, 2], "SCOres": [3, 20], "DOZens": [3, 12],
"GRoss": [2, 144], "GREATGRoss": [7, 1728], "GOOGOLs": [6, 1e100]
}
 
var metric = {
"K": 1e3, "M": 1e6, "G": 1e9, "T": 1e12, "P": 1e15, "E": 1e18,
"Z": 1e21, "Y": 1e24, "X": 1e27, "W": 1e30, "V": 1e33, "U": 1e36
}
 
var b = Fn.new { |e| BigRat.two.pow(e) }
 
var binary = {
"Ki": b.call(10), "Mi": b.call(20), "Gi": b.call(30), "Ti": b.call(40),
"Pi": b.call(50), "Ei": b.call(60), "Zi": b.call(70), "Yi": b.call(80),
"Xi": b.call(90), "Wi": b.call(100), "Vi": b.call(110), "Ui": b.call(120)
}
 
var googol = BigRat.fromDecimal("1e100")
 
var fact = Fn.new { |num, d|
var prod = 1
var n = Num.fromString(num)
var i = n
while (i > 0) {
prod = prod * i
i = i - d
}
return prod
}
 
var parse = Fn.new { |number|
// find index of last digit
var i = number.count - 1
while (i >= 0) {
if (48 <= number.bytes[i] && number.bytes[i] <= 57) break
i = i - 1
}
var num = number[0..i]
num = num.replace(",", "") // get rid of any commas
var suf = Str.upper(number[i+1..-1])
if (suf == "") return BigRat.fromDecimal(num)
if (suf[0] == "!") {
var prod = fact.call(num, suf.count)
return BigRat.new(prod)
}
for (me in abbrevs) {
var kk = Str.upper(me.key)
if (kk.startsWith(suf) && suf.count >= me.value[0]) {
var t1 = BigRat.fromDecimal(num)
var t2 = (me.key != "GOOGOLS") ? BigRat.fromDecimal(me.value[1]) : googol
return t1 * t2
}
}
var bf = BigRat.fromDecimal(num)
for (me in metric) {
var j = 0
while (j < suf.count) {
if (me.key == suf[j]) {
if (j < suf.count-1 && suf[j+1] == "I") {
bf = bf * binary[me.key + "i"]
j = j + 1
} else {
bf = bf * me.value
}
}
j = j + 1
}
}
return bf
}
 
var process = Fn.new { |numbers|
System.write("numbers = ")
for (number in numbers) Fmt.write("$s ", number)
System.write("\nresults = ")
for (number in numbers) {
var res = parse.call(number)
if (res.isInteger) {
Fmt.write("$,s ", res.toDecimal(50))
} else {
var sres = Fmt.swrite("$,s", res.truncate.toDecimal)
Fmt.write("$s ", sres + res.fraction.abs.toDecimal(50)[1..-1])
}
}
System.print("\n")
}
 
var numbers = ["2greatGRo", "24Gros", "288Doz", "1,728pairs", "172.8SCOre"]
process.call(numbers)
 
numbers = ["1,567", "+1.567k", "0.1567e-2m"]
process.call(numbers)
 
numbers = ["25.123kK", "25.123m", "2.5123e-00002G"]
process.call(numbers)
 
numbers = ["25.123kiKI", "25.123Mi", "2.5123e-00002Gi", "+.25123E-7Ei"]
process.call(numbers)
 
numbers = ["-.25123e-34Vikki", "2e-77gooGols"]
process.call(numbers)
 
numbers = ["9!", "9!!", "9!!!", "9!!!!", "9!!!!!", "9!!!!!!", "9!!!!!!!", "9!!!!!!!!", "9!!!!!!!!!"]
process.call(numbers)</syntaxhighlight>
 
{{out}}
<pre>
numbers = 2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
results = 3,456 3,456 3,456 3,456 3,456
 
numbers = 1,567 +1.567k 0.1567e-2m
results = 1,567 1,567 1,567
 
numbers = 25.123kK 25.123m 2.5123e-00002G
results = 25,123,000 25,123,000 25,123,000
 
numbers = 25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
results = 26,343,374.848 26,343,374.848 26,975,615.844352 28,964,846,960.237816578048
 
numbers = -.25123e-34Vikki 2e-77gooGols
results = -33,394.194938104441474962344775423096782848 200,000,000,000,000,000,000,000
 
numbers = 9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!
results = 362,880 945 162 45 36 27 18 9 9
</pre>
 
Line 1,070 ⟶ 1,351:
Floats are limited to 64 bit IEEE754.
Error checking is nonexistent.
<langsyntaxhighlight lang="zkl">var [const] BI=Import.lib("zklBigNum"); // GMP
var kRE,kD, aRE,aD;
 
Line 1,137 ⟶ 1,418:
re=RegExp("(%s)".fmt(re.close().strip().replace(" ","|")));
return(re,abrevs);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach na in (T("2greatGRo", "24Gros", "288Doz", "1,728pairs", "172.8SCOre",
"1,567", "+1.567k", "0.1567e-2m",
"25.123kK", "25.123m", "2.5123e-00002G",
Line 1,149 ⟶ 1,430:
if((r:=naSuffixes(na)).isType(Float)) println("%16s : %,f".fmt(na,r));
else println("%16s : %,d".fmt(na,r));
}</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
9,476

edits