Numerical and alphabetical suffixes: Difference between revisions

Content added Content deleted
(→‎{{header|Factor}}: add EBNF solution)
(→‎{{header|Perl 6}}: extend the precision some more)
Line 552: Line 552:
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.
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.


<lang perl6>my $googol = 10**100;
<lang perl6>use Rat::Precise;

my $googol = 10**100;
«PAIRs 2 SCOres 20 DOZens 12 GRoss 144 GREATGRoss 1728 GOOGOLs $googol»
«PAIRs 2 SCOres 20 DOZens 12 GRoss 144 GREATGRoss 1728 GOOGOLs $googol»
~~ m:g/ ((<.:Lu>+) <.:Ll>*) \s+ (\S+) /;
~~ m:g/ ((<.:Lu>+) <.:Ll>*) \s+ (\S+) /;
Line 564: Line 566:


my %suffix = flat %abr,
my %suffix = flat %abr,
(<K M G T P E Z Y X W V U>».fc Z=> (1000, * * 1000 … *)),
(<K M G T P E Z Y X W V U>».fc Z=> (1000, * * 1000 … *)),
(<Ki Mi Gi Ti Pi Ei Zi Yi Xi Wi Vi Ui>».fc Z=> (1024, * * 1024 … *));
(<Ki Mi Gi Ti Pi Ei Zi Yi Xi Wi Vi Ui>».fc Z=> (1024, * * 1024 … *));


Line 571: Line 573:
sub comma ($i is copy) {
sub comma ($i is copy) {
my $s = $i < 0 ?? '-' !! '';
my $s = $i < 0 ?? '-' !! '';
my $whole = $i.abs.floor;
my ($whole, $frac) = $i.split('.');
my $frac = $i.abs - $whole ?? '.' ~ $i.abs.split('.')[1] !! '';
$frac = $frac.defined ?? ".$frac" !! '';
$s ~ $whole.flip.comb(3).join(',').flip ~ $frac
$s ~ $whole.abs.flip.comb(3).join(',').flip ~ $frac
}
}


Line 594: Line 596:
$ret = [*] $ret, |@suf.map: { %suffix{$_} } if @suf[0];
$ret = [*] $ret, |@suf.map: { %suffix{$_} } if @suf[0];
$ret = [*] ($ret, * - $fact.chars …^ * < 2) if $fact.chars;
$ret = [*] ($ret, * - $fact.chars …^ * < 2) if $fact.chars;
$ret.?precise ?? $ret.precise !! $ret
$ret
}
}


Line 610: Line 612:


# Task required stupid layout
# Task required stupid layout
# say "\n In: $_\nOut : ", .words.map({comma .&units}).join(' ') for $test.lines;</lang>
# say "\n In: $_\nOut: ", .words.map({comma .&units}).join(' ') for $test.lines;</lang>
{{out}}
{{out}}
<pre> 2greatGRo: 3,456
<pre> 2greatGRo: 3,456
Line 627: Line 629:
2.5123e-00002Gi: 26,975,615.844352
2.5123e-00002Gi: 26,975,615.844352
+.25123E-7Ei: 28,964,846,960.237816578048
+.25123E-7Ei: 28,964,846,960.237816578048
-.25123e-34Vikki: -33,394.19493810444147496234477542309678
-.25123e-34Vikki: -33,394.194938104441474962344775423096782848
2e-77gooGols: 200,000,000,000,000,000,000,000
2e-77gooGols: 200,000,000,000,000,000,000,000
9!: 362,880
9!: 362,880
Line 638: Line 640:
9!!!!!!!!: 9
9!!!!!!!!: 9
9!!!!!!!!!: 9
9!!!!!!!!!: 9
.017K!!: 34,459,425</pre>
.017k!!: 34,459,425</pre>


=={{header|REXX}}==
=={{header|REXX}}==