Numerical and alphabetical suffixes: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 134:
=={{header|Factor}}==
===Functional===
<
grouping grouping.extras kernel literals math math.functions
math.parser math.ranges qw regexp sequences sequences.deep
Line 243:
] each ;
MAIN: main</
{{out}}
<pre>
Line 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.
<
math.functions math.parser math.ranges multiline peg.ebnf
quotations qw sequences sequences.deep splitting strings unicode ;
Line 330:
] each ;
MAIN: num-alpha-suffix-demo</
{{out}}
<pre>
Line 353:
=={{header|Go}}==
<
import (
Line 517:
"9!!!!!!!", "9!!!!!!!!", "9!!!!!!!!!"}
process(numbers)
}</
{{out}}
Line 541:
=={{header|Julia}}==
<
partialsuffixes = Dict("PAIR" => "PAIRS", "SCO" => "SCORES", "DOZ" => "DOZENS",
Line 628:
testsuffixes()
</
<pre>
Testing: ["2greatGRo", "24Gros", "288Doz", "1,728pairs", "172.8SCOre"]
Line 650:
=={{header|Nim}}==
<
const input = """
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
Line 707:
output &= sepFloat(res)&" "
echo(output)
</syntaxhighlight>
{{out}}<pre>
Input:
Line 736:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/Numerical_and_alphabetical_suffixes
Line 772:
25.123kiKI 25.123Mi 2.5123e-00002Gi +.25123E-7Ei
-.25123e-34Vikki 2e-77gooGols
9! 9!! 9!!! 9!!!! 9!!!!! 9!!!!!! 9!!!!!!! 9!!!!!!!! 9!!!!!!!!!</
{{out}}
<pre>
Line 798:
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.
<!--<
<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>
Line 888:
<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>
<!--</
{{out}}
<pre>
Line 922:
=={{header|Python}}==
{{works with|CPython|3.7.3}}
<syntaxhighlight lang="python">
from functools import reduce
from operator import mul
Line 1,000:
print("Input:", ' '.join(test_cases))
print("Output:", ' '.join(format(result, ',f').strip('0').strip('.') for result in map(expand, test_cases)))
</syntaxhighlight>
{{out}}
<pre>
Line 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"
my $googol = 10**100;
Line 1,086:
# Task required stupid layout
# say "\n In: $_\nOut: ", .words.map({comma .&units}).join(' ') for $test.lines;</
{{out}}
<pre> 2greatGRo: 3,456
Line 1,117:
=={{header|REXX}}==
<
numeric digits 2000 /*allow the usage of ginormous numbers.*/
@.=; @.1= '2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre'
Line 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</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,216:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<
import "/str" for Str
import "/fmt" for Fmt
Line 1,324:
numbers = ["9!", "9!!", "9!!!", "9!!!!", "9!!!!!", "9!!!!!!", "9!!!!!!!", "9!!!!!!!!", "9!!!!!!!!!"]
process.call(numbers)</
{{out}}
Line 1,351:
Floats are limited to 64 bit IEEE754.
Error checking is nonexistent.
<
var kRE,kD, aRE,aD;
Line 1,418:
re=RegExp("(%s)".fmt(re.close().strip().replace(" ","|")));
return(re,abrevs);
}</
<
"1,567", "+1.567k", "0.1567e-2m",
"25.123kK", "25.123m", "2.5123e-00002G",
Line 1,430:
if((r:=naSuffixes(na)).isType(Float)) println("%16s : %,f".fmt(na,r));
else println("%16s : %,d".fmt(na,r));
}</
{{out}}
<pre style="height:45ex">
|