How to resolve the algorithm Numerical and alphabetical suffixes step by step in the Julia programming language
How to resolve the algorithm Numerical and alphabetical suffixes step by step in the Julia programming language
Table of Contents
Problem Statement
This task is about expressing numbers with an attached (abutted) suffix multiplier(s), the suffix(es) could be:
The (decimal) numbers can be expressed generally as: where:
This isn't a pure or perfect definition of the way we express decimal numbers, but it should convey the intent for this task. The use of the word periods (thousands) is not meant to confuse, that word (as used above) is what the comma separates; the groups of decimal digits are called periods, and in almost all cases, are groups of three decimal digits. If an e or E is specified, there must be a legal number expressed before it, and there must be a legal (exponent) expressed after it. Also, there must be some digits expressed in all cases, not just a sign and/or decimal point. Superfluous signs, decimal points, exponent numbers, and zeros need not be preserved. I.E.: +7 007 7.00 7E-0 7E000 70e-1 could all be expressed as 7 All numbers to be "expanded" can be assumed to be valid and there won't be a requirement to verify their validity.
Note that the plurals are supported, even though they're usually used when expressing exact numbers (She has 2 dozen eggs, and dozens of quavas)
All of the metric and binary suffixes can be expressed in lowercase, uppercase, or mixed case. All of the metric and binary suffixes can be stacked (expressed multiple times), and also be intermixed: I.E.: 123k 123K 123GKi 12.3GiGG 12.3e-7T .78E100e
Factorial suffixes aren't, of course, the usual type of multipliers, but are used here in a similar vein.
Multifactorials aren't to be confused with super─factorials where (4!)! would be (24)!.
where the last number for the factorials has nine factorial symbols (!) after the 9
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numerical and alphabetical suffixes step by step in the Julia programming language
This Julia code defines functions for parsing and evaluating numbers with suffixes representing different orders of magnitude.
Functions
-
firstsuffix(s, x)
:- Takes a string
s
and a numberx
. - Iterates through a list of partial suffixes (e.g., "PAIR," "SCO") to find the longest match at the beginning of
s
. - If a match is found, it returns the corresponding multiplier (e.g., 2 for "PAIRS") and the number of characters matched.
- If no match is found, it checks for two-character suffixes (e.g., "KI") or one-character suffixes (e.g., "K").
- If any of these suffixes match, it returns the corresponding multiplier and the number of characters matched.
- Otherwise, it returns 1 (no suffix) and the length of
s
.
- Takes a string
-
parsesuffix(s, x)
:- Takes a string
s
and a numberx
. - Iterates through the string
s
from left to right, callingfirstsuffix
to parse and multiply the number by the appropriate multiplier for each suffix encountered. - Returns the final multiplied number.
- Takes a string
-
suffixednumber(s)
:- Takes a string
s
and removes commas from it. - Finds the last digit character in the string.
- If no digit is found, returns
NaN
. - Otherwise, calls
parsesuffix
to parse and multiply the number by the appropriate multiplier based on the suffix, if any. - Returns the final multiplied number.
- Takes a string
-
testsuffixes()
:- Defines a list of test cases containing strings with different numeric suffixes.
- Iterates through the test cases and prints the input and output for each case using the
suffixednumber
function.
Usage
The code is used to test the suffixednumber
function on various input strings with different numeric suffixes. It demonstrates how the function can handle partial suffixes ("PAIRS"), two-character suffixes ("KI"), one-character suffixes ("K"), and exclamation marks ("!") to represent large numbers. The test cases include numbers with multiple suffixes and negative numbers.
Example
Let's say you have the following string:
"25.123kK"
When you call suffixednumber
on this string, the following steps occur:
- The commas in the string are removed, resulting in "25.123kK".
- The function finds the last digit character in the string, which is '3'.
- The function calls
parsesuffix
with the string "25.123kK". parsesuffix
callsfirstsuffix
with "kK" and the number 25.123.firstsuffix
recognizes "kK" as the partial suffix "K" and returns the multiplier 10^3 and the number of characters matched (1).parsesuffix
multiplies 25.123 by 10^3, resulting in 25123.suffixednumber
returns the number 25123.
Source code in the julia programming language
using Formatting
partialsuffixes = Dict("PAIR" => "PAIRS", "SCO" => "SCORES", "DOZ" => "DOZENS",
"GR" => "GROSS", "GREATGR" => "GREATGROSS", "GOOGOL" => "GOOGOLS")
partials = sort(collect(keys(partialsuffixes)), lt=(a,b)->length(a)<length(b), rev=true)
multicharsuffixes = Dict("PAIRS" => 2, "SCORES" => 20, "DOZENS" => 12, "GROSS" => 144,
"GREATGROSS" => 1728, "GOOGOLS" => BigInt(10)^100)
twocharsuffixes = Dict(
"KI" => BigInt(2)^10, "MI" => BigInt(2)^20, "GI" => BigInt(2)^30, "TI" => BigInt(2)^40,
"PI" => BigInt(2)^50, "EI" => BigInt(2)^60, "ZI" => BigInt(2)^70, "YI" => BigInt(2)^80,
"XI" => BigInt(2)^90, "WI" => BigInt(2)^100, "VI" => BigInt(2)^110, "UI" => BigInt(2)^120)
twosuff = collect(keys(twocharsuffixes))
onecharsuffixes = Dict("K" => 10^3, "M" => 10^6, "G" => 10^9, "T" => 10^12, "P" => 10^15,
"E" => 10^18, "Z" => 10^21, "Y" => BigInt(10)^24,
"X" => BigInt(10)^27, "W" => BigInt(10)^30,
"V" => BigInt(10)^33, "U" => BigInt(10)^36)
onesuff = collect(keys(onecharsuffixes))
function firstsuffix(s, x)
str = uppercase(s)
if str[1] == '!'
lastbang = something(findfirst(x -> x != '!', str), length(str))
return prod(x:-lastbang:1) / x, lastbang
end
for pstr in partials
if match(Regex("^" * pstr), str) != nothing
fullsuffix = partialsuffixes[pstr]
n = length(pstr)
while n < length(fullsuffix) && n < length(str) && fullsuffix[n+1] == str[n+1]
n += 1
end
return BigInt(multicharsuffixes[fullsuffix]), n
end
end
for pstr in twosuff
if match(Regex("^" * pstr), str) != nothing
return BigInt(twocharsuffixes[pstr]), 2
end
end
for pstr in onesuff
if match(Regex("^" * pstr), str) != nothing
return BigInt(onecharsuffixes[pstr]), 1
end
end
return 1, length(s)
end
function parsesuffix(s, x)
str = s
mult = BigInt(1)
n = 1
while n <= length(str)
multiplier, n = firstsuffix(str, x)
mult *= multiplier
str = str[n+1:end]
end
mult
end
function suffixednumber(s)
if (endnum = findlast(isdigit, s)) == nothing
return NaN
end
x = BigFloat(replace(s[1:endnum], "," => ""))
return x * (endnum < length(s) ? parsesuffix(s[endnum + 1:end], x) : 1)
end
const testcases =
["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!!!!!!!!!"]
function testsuffixes()
for line in testcases
cases = split(line)
println("Testing: ", string.(cases))
println("Results: ", join(map(x -> format(suffixednumber(x), commas=true), cases), " "), "\n")
end
end
testsuffixes()
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Brilliant numbers step by step in the Prolog programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Java programming language
You may also check:How to resolve the algorithm String comparison step by step in the Lasso programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Erlang programming language