Ad

# 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"));
	}
}
Code
Diff
  • var tr = {
      "I": 1,
      "V": 5,
      "X": 10,
      "L": 50,
      "C": 100,
      "D": 500,
      "M": 1000
    }
    
    function solution (roman) {
    	letters = roman.split('');
      var res = 0;
      
      letters.forEach((cur, i) => {
        var next = letters[i + 1];
        
        if (tr[next] && tr[cur] < tr[next]) {
          res -= tr[cur];
        } else {
          res += tr[cur];
        }
      })
      
      return res;
    }
    • package kata
    • var tr = {
    • "I": 1,
    • "V": 5,
    • "X": 10,
    • "L": 50,
    • "C": 100,
    • "D": 500,
    • "M": 1000
    • }
    • func Decode(roman string) int {
    • return 0
    • function solution (roman) {
    • letters = roman.split('');
    • var res = 0;
    • letters.forEach((cur, i) => {
    • var next = letters[i + 1];
    • if (tr[next] && tr[cur] < tr[next]) {
    • res -= tr[cur];
    • } else {
    • res += tr[cur];
    • }
    • })
    • return res;
    • }

# 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"));
	}
}
package kata

func Decode(roman string) int {
  return 0
}