Jump to content

Numerical and alphabetical suffixes: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 134:
=={{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 243:
] each ;
 
MAIN: main</langsyntaxhighlight>
{{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.
<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 330:
] each ;
MAIN: num-alpha-suffix-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 353:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 517:
"9!!!!!!!", "9!!!!!!!!", "9!!!!!!!!!"}
process(numbers)
}</langsyntaxhighlight>
 
{{out}}
Line 541:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
 
partialsuffixes = Dict("PAIR" => "PAIRS", "SCO" => "SCORES", "DOZ" => "DOZENS",
Line 628:
 
testsuffixes()
</langsyntaxhighlight>{{out}}
<pre>
Testing: ["2greatGRo", "24Gros", "288Doz", "1,728pairs", "172.8SCOre"]
Line 650:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import re, strutils, parseutils, tables, math
const input = """
2greatGRo 24Gros 288Doz 1,728pairs 172.8SCOre
Line 707:
output &= sepFloat(res)&" "
echo(output)
</syntaxhighlight>
</lang>
{{out}}<pre>
Input:
Line 736:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/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!!!!!!!!!</langsyntaxhighlight>
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 922:
=={{header|Python}}==
{{works with|CPython|3.7.3}}
<syntaxhighlight lang="python">
<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>
</lang>
{{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" perl6line>use Rat::Precise;
 
my $googol = 10**100;
Line 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 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,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,216:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigRat
import "/str" for Str
import "/fmt" for Fmt
Line 1,324:
 
numbers = ["9!", "9!!", "9!!!", "9!!!!", "9!!!!!", "9!!!!!!", "9!!!!!!!", "9!!!!!!!!", "9!!!!!!!!!"]
process.call(numbers)</langsyntaxhighlight>
 
{{out}}
Line 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,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,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">
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.