How to resolve the algorithm Roman numerals/Encode step by step in the Groovy programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Roman numerals/Encode step by step in the Groovy programming language
Table of Contents
Problem Statement
Create a function taking a positive integer as its parameter and returning a string containing the Roman numeral representation of that integer. Modern Roman numerals are written by expressing each digit separately, starting with the left most digit and skipping any digit with a value of zero.
In Roman numerals:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Roman numerals/Encode step by step in the Groovy programming language
Source code in the groovy programming language
symbols = [ 1:'I', 4:'IV', 5:'V', 9:'IX', 10:'X', 40:'XL', 50:'L', 90:'XC', 100:'C', 400:'CD', 500:'D', 900:'CM', 1000:'M' ]
def roman(arabic) {
def result = ""
symbols.keySet().sort().reverse().each {
while (arabic >= it) {
arabic-=it
result+=symbols[it]
}
}
return result
}
assert roman(1) == 'I'
assert roman(2) == 'II'
assert roman(4) == 'IV'
assert roman(8) == 'VIII'
assert roman(16) == 'XVI'
assert roman(32) == 'XXXII'
assert roman(25) == 'XXV'
assert roman(64) == 'LXIV'
assert roman(128) == 'CXXVIII'
assert roman(256) == 'CCLVI'
assert roman(512) == 'DXII'
assert roman(954) == 'CMLIV'
assert roman(1024) == 'MXXIV'
assert roman(1666) == 'MDCLXVI'
assert roman(1990) == 'MCMXC'
assert roman(2008) == 'MMVIII'
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Non-transitive dice step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Forth programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the zkl programming language