How to resolve the algorithm Roman numerals/Decode step by step in the XQuery programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Roman numerals/Decode step by step in the XQuery programming language
Table of Contents
Problem Statement
Create a function that takes a Roman numeral as its argument and returns its value as a numeric decimal integer. You don't need to validate the form of the Roman numeral. Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately, starting with the leftmost decimal digit and skipping any 0s (zeroes). 1990 is rendered as MCMXC (1000 = M, 900 = CM, 90 = XC) and 2008 is rendered as MMVIII (2000 = MM, 8 = VIII). The Roman numeral for 1666, MDCLXVI, uses each letter in descending order.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Roman numerals/Decode step by step in the XQuery programming language
Source code in the xquery programming language
xquery version "3.1";
declare function local:decode-roman-numeral($roman-numeral as xs:string) {
$roman-numeral
=> upper-case()
=> for-each(
function($roman-numeral-uppercase) {
analyze-string($roman-numeral-uppercase, ".")/fn:match
! map { "M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1 }(.)
}
)
=> fold-right([0,0],
function($number as xs:integer, $accumulator as array(*)) {
let $running-total := $accumulator?1
let $previous-number := $accumulator?2
return
if ($number lt $previous-number) then
[ $running-total - $number, $number ]
else
[ $running-total + $number, $number ]
}
)
=> array:get(1)
};
let $roman-numerals :=
map {
"MCMXCIX": 1999,
"MDCLXVI": 1666,
"XXV": 25,
"XIX": 19,
"XI": 11,
"CMLIV": 954,
"MMXI": 2011,
"CD": 400,
"MCMXC": 1990,
"MMVIII": 2008,
"MMIX": 2009,
"MMMDCCCLXXXVIII": 3888
}
return
map:for-each(
$roman-numerals,
function($roman-numeral, $expected-value) {
local:decode-roman-numeral($roman-numeral) eq $expected-value
}
)
You may also check:How to resolve the algorithm Multisplit step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm ABC problem step by step in the Ring programming language
You may also check:How to resolve the algorithm Count in factors step by step in the Forth programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the X86 Assembly programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Bracmat programming language