# TBS Engineering Morning Kick-off Dojo
Roman Numerals Decoder
Create a function that takes a Roman numeral as its argument, and returns its value as a numeric decimal integer.
Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately, starting with the leftmost digit and skipping any 0s. So 1990 is rendered "MCMXC" (1000 = M, 900 = CM, 90 = XC) and 2008 is rendered "MMVIII" (2000 = MM, 8 = VIII). The Roman numeral for 1666, "MDCLXVI", uses each letter in descending order.
Example:
solution('XXI'); // should return 21
You should assume that the Roman Numeral passed to your function is always valid.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
Getting Started
First, decide which language you want to tackle the problem with. I've included some snippets below to help get started with the languages that were declared in our Tech Radar session (JavaScript, Python, Golang, C#, Java).
Feel free to pick a langauge you are comfortable with to solve this problem, or if you fancy a challenge, one that you may not be so comfortable with! You can also pick from any of the other languages CodeWars supports, however you will need to write your own test cases to validate your solutions for these.
#### JavaScript
# Copy into "Code" (top-right box)
function solution (roman) {
return 0;
}
---
# Copy into "Test Cases" (bottom-right box)
const strictEqual = require('chai').assert.strictEqual;
function doTest (romanString, expected) {
const actual = solution(romanString);
strictEqual(actual, expected, `for roman number ${romanString}`);
}
describe("Tests", () => {
it("sample tests", () => {
doTest('XXI', 21);
doTest('I', 1);
doTest('IV', 4);
doTest('MMVIII', 2008);
doTest('MDCLXVI', 1666);
});
});
### Python
# Copy into "Code" (top-right box)
def solution(roman):
return 0
---
# Copy into "Test Cases" (bottom-right box)
test.describe("Example Tests")
test.assert_equals(solution('XXI'), 21, 'XXI should == 21')
test.assert_equals(solution('I'), 1, 'I should == 1')
test.assert_equals(solution('IV'), 4, 'IV should == 4')
test.assert_equals(solution('MMVIII'), 2008, 'MMVIII should == 2008')
test.assert_equals(solution('MDCLXVI'), 1666, 'MDCLXVI should == 1666')
Go
# Copy into "Code" (top-right box)
package kata
func Decode(roman string) int {
return 0
}
---
# Copy into "Test Cases" (bottom-right box)
package kata_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "codewarrior/kata"
)
var _ = Describe("test roman to decimal converter", func() {
It("should give decimal number from roman", func() {
Expect(Decode("XXI")).To(Equal(21))
})
It("should give decimal number from roman", func() {
Expect(Decode("I")).To(Equal(1))
})
It("should give decimal number from roman", func() {
Expect(Decode("IV")).To(Equal(4))
})
It("should give decimal number from roman", func() {
Expect(Decode("MMVIII")).To(Equal(2008))
})
It("should give decimal number from roman", func() {
Expect(Decode("MDCLXVI")).To(Equal(1666))
})
})
C#
# Copy into "Code" (top-right box)
using System;
public class RomanDecode
{
public static int Solution(string roman)
{
throw new NotImplementedException();
}
}
---
# Copy into "Test Cases" (bottom-right box)
using System;
using NUnit.Framework;
[TestFixture]
public class RomanDecodeTests
{
[TestCase(21, "XXI")]
public void Test(int expected, string roman)
{
Assert.AreEqual(expected, RomanDecode.Solution(roman));
}
[TestCase(1, "I")]
public void Test(int expected, string roman)
{
Assert.AreEqual(expected, RomanDecode.Solution(roman));
}
[TestCase(4, "IV")]
public void Test(int expected, string roman)
{
Assert.AreEqual(expected, RomanDecode.Solution(roman));
}
[TestCase(2008, "MMVIII")]
public void Test(int expected, string roman)
{
Assert.AreEqual(expected, RomanDecode.Solution(roman));
}
[TestCase(1666, "MDCLXVI")]
public void Test(int expected, string roman)
{
Assert.AreEqual(expected, RomanDecode.Solution(roman));
}
}
Java
# Copy into "Code" (top-right box)
public class Roman {
public static int solve(String roman) {
return 0;
}
}
---
# Copy into "Test Cases" (bottom-right box)
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Test
public void testXXI() {
assertEquals(21, Roman.solve("XXI"));
}
@Test
public void testI() {
assertEquals(1, Roman.solve("I"));
}
@Test
public void testIV() {
assertEquals(4, Roman.solve("IV"));
}
@Test
public void testMMVIII() {
assertEquals(2008, Roman.solve("MMVIII"));
}
@Test
public void testMDCLXVI() {
assertEquals(1666, Roman.solve("MDCLXVI"));
}
}
def solution(roman): list = [*roman] pervious = '' current = '' current_int = 0 pervious_int = 0 result = 0 for i in range(len(list)): if i == 0: pervious = list[i] continue current = list[i] pervious = list[i - 1] if current == 'I': current_int = 1 elif current == 'V': current_int = 5 elif current == 'X': current_int = 10 elif current == 'L': current_int = 50 elif current == 'C': current_int = 100 elif current == 'D': current_int = 500 elif current == 'M': current_int = 1000 if pervious == 'I': pervious_int = 1 elif pervious == 'V': pervious_int = 5 elif pervious == 'X': pervious_int = 10 elif pervious == 'L': pervious_int = 50 elif pervious == 'C': pervious_int = 100 elif pervious == 'D': pervious_int = 500 elif pervious == 'M': pervious_int = 1000 if pervious_int < current_int: result += current_int - pervious_int else: result += pervious_int if i == len(list) - 1: result += current_int print('Current: {} - {}, Pervious: {} - {}'.format(current, current_int, pervious, pervious_int)) # if item.index return result
package katafunc Decode(roman string) int {return 0}- def solution(roman):
- list = [*roman]
- pervious = ''
- current = ''
- current_int = 0
- pervious_int = 0
- result = 0
- for i in range(len(list)):
- if i == 0:
- pervious = list[i]
- continue
- current = list[i]
- pervious = list[i - 1]
- if current == 'I':
- current_int = 1
- elif current == 'V':
- current_int = 5
- elif current == 'X':
- current_int = 10
- elif current == 'L':
- current_int = 50
- elif current == 'C':
- current_int = 100
- elif current == 'D':
- current_int = 500
- elif current == 'M':
- current_int = 1000
- if pervious == 'I':
- pervious_int = 1
- elif pervious == 'V':
- pervious_int = 5
- elif pervious == 'X':
- pervious_int = 10
- elif pervious == 'L':
- pervious_int = 50
- elif pervious == 'C':
- pervious_int = 100
- elif pervious == 'D':
- pervious_int = 500
- elif pervious == 'M':
- pervious_int = 1000
- if pervious_int < current_int:
- result += current_int - pervious_int
- else:
- result += pervious_int
- if i == len(list) - 1:
- result += current_int
- print('Current: {} - {}, Pervious: {} - {}'.format(current, current_int, pervious, pervious_int))
- # if item.index
- return result
test.describe("Example Tests") test.assert_equals(solution('XXI'), 21, 'XXI should == 21') test.assert_equals(solution('I'), 1, 'I should == 1') test.assert_equals(solution('IV'), 4, 'IV should == 4') test.assert_equals(solution('MMVIII'), 2008, 'MMVIII should == 2008') test.assert_equals(solution('MDCLXVI'), 1666, 'MDCLXVI should == 1666')
package kata_testimport (. "github.com/onsi/ginkgo". "github.com/onsi/gomega". "codewarrior/kata")var _ = Describe("test roman to decimal converter", func() {It("should give decimal number from roman", func() {Expect(Decode("XXI")).To(Equal(21))})It("should give decimal number from roman", func() {Expect(Decode("I")).To(Equal(1))})It("should give decimal number from roman", func() {Expect(Decode("IV")).To(Equal(4))})It("should give decimal number from roman", func() {Expect(Decode("MMVIII")).To(Equal(2008))})It("should give decimal number from roman", func() {Expect(Decode("MDCLXVI")).To(Equal(1666))})})- test.describe("Example Tests")
- test.assert_equals(solution('XXI'), 21, 'XXI should == 21')
- test.assert_equals(solution('I'), 1, 'I should == 1')
- test.assert_equals(solution('IV'), 4, 'IV should == 4')
- test.assert_equals(solution('MMVIII'), 2008, 'MMVIII should == 2008')
- test.assert_equals(solution('MDCLXVI'), 1666, 'MDCLXVI should == 1666')