How to resolve the algorithm Spelling of ordinal numbers step by step in the Julia programming language
How to resolve the algorithm Spelling of ordinal numbers step by step in the Julia programming language
Table of Contents
Problem Statement
Ordinal numbers (as used in this Rosetta Code task), are numbers that describe the position of something in a list. It is this context that ordinal numbers will be used, using an English-spelled name of an ordinal number.
The ordinal numbers are (at least, one form of them): sometimes expressed as:
For this task, the following (English-spelled form) will be used:
Furthermore, the short scale numbering system (i.e. 2,000,000,000 is two billion) will be used here. wp:Long and short scales 2,000,000,000 is two billion, not two milliard.
Write a driver and a function (subroutine/routine ···) that returns the English-spelled ordinal version of a specified number (a positive integer). Optionally, try to support as many forms of an integer that can be expressed: 123 00123.0 1.23e2 all are forms of the same integer. Show all output here.
Use (at least) the test cases of:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Spelling of ordinal numbers step by step in the Julia programming language
Code Overview:
This code converts number text or string representations of numbers into ordinal numbers, which are used to indicate the position or order of items in a sequence. It handles special cases for irregular ordinals and applies appropriate suffixes to regular ordinals.
Implementation Details:
1. Regular and Irregular Ordinals:
const irregular
is a dictionary that maps irregular ordinal numbers (e.g., "one" -> "first") to their standard equivalents.
2. Suffixes:
const suffix
represents the standard "th" suffix for regular ordinals.
const ysuffix
represents the "ieth" suffix for numbers ending in "y" (e.g., "twenty-nine" -> "twenty-ninth").
3. numtext2ordinal
Function:
-
Split Last Word:
- Splits the input string into words.
- Stores the last word and checks if it has a "-" indicating multiple words or just a single word.
-
Separate First Part:
- If the last word has a "-" indicating multiple words, split it at the "-" and remove the last part.
- If the last word has no "-", use the entire input string.
-
Irregular/Regular Ordinal Conversion:
- If the last word is an irregular ordinal, replace it with its standard equivalent from
irregular
. - If the last word ends in "y", replace it with the "ieth" suffix.
- Otherwise, append the standard "th" suffix.
- If the last word is an irregular ordinal, replace it with its standard equivalent from
-
Assemble Ordinal:
- Combine the first part (e.g., "twenty") with the ordinalized last word.
4. Test Cases:
The code includes a list of test cases in testcases
. It iterates through these test cases and converts the numbers (represented as strings) to ordinal numbers.
5. Output:
For each test case, the code prints the original number and its ordinal representation.
Source code in the julia programming language
const irregular = Dict("one" => "first", "two" => "second", "three" => "third", "five" => "fifth",
"eight" => "eighth", "nine" => "ninth", "twelve" => "twelfth")
const suffix = "th"
const ysuffix = "ieth"
function numtext2ordinal(s)
lastword = split(s)[end]
redolast = split(lastword, "-")[end]
if redolast != lastword
lastsplit = "-"
word = redolast
else
lastsplit = " "
word = lastword
end
firstpart = reverse(split(reverse(s), lastsplit, limit=2)[end])
firstpart = (firstpart == word) ? "": firstpart * lastsplit
if haskey(irregular, word)
word = irregular[word]
elseif word[end] == 'y'
word = word[1:end-1] * ysuffix
else
word = word * suffix
end
firstpart * word
end
const testcases = [1 2 3 4 5 11 65 100 101 272 23456 8007006005004003]
for n in testcases
println("$n => $(numtext2ordinal(num2text(n)))")
end
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Batch File programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the CLU programming language
You may also check:How to resolve the algorithm Get system command output step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Amicable pairs step by step in the UTFool programming language
You may also check:How to resolve the algorithm Text processing/2 step by step in the Eiffel programming language