---
# System prepended metadata

title: 'Roman Numerals Decoder [6 kyu]'
tags: [C++, CodeWars]

---

# Roman Numerals Decoder [6 kyu]

[Roman Numerals Decoder](https://www.codewars.com/kata/51b6249c4612257ac0000005)
6 kyu

## Solution
```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-07-28
**/
#include <cctype>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <unordered_map>
using std::unordered_map;

/*
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
*/
using RefMap = unordered_map<char, int>;
RefMap ref_map({{'I', 1},
                {'V', 5},
                {'X', 10},
                {'L', 50},
                {'C', 100},
                {'D', 500},
                {'M', 1000}});

struct TestCaseStruct {
  string input;
  int expected_output;
};

class RomanNumeralsDecoder {
 public:
  int solution(const string &roman) {
    int output = 0;

    int previous_value = 0;
    for (auto it = roman.rbegin(); it != roman.rend(); ++it) {
      int current_value = ref_map[*it];
      if (current_value >= previous_value) {
        output += current_value;
      } else {
        output -= current_value;
      }
      previous_value = current_value;
    }

    return output;
  }
};

int main(int argc, char *argv[]) {
  /*
  Assert::That(solution("XXI"), Equals(21)); }
  It(should_work_for_a_few_other_examples) {
        Assert::That(solution("I"), Equals(1));
        Assert::That(solution("IV"), Equals(4));
        Assert::That(solution("MMVIII"), Equals(2008));
        Assert::That(solution("MDCLXVI"), Equals(1666));
  */
  TestCaseStruct test_cases[] = {
      {"XXI", 21}, {"I", 1}, {"IV", 4}, {"MMVIII", 2008}, {"MDCLXVI", 1666}};

  RomanNumeralsDecoder solution;
  for (const auto& test_case : test_cases) {
    if (solution.solution(test_case.input) == test_case.expected_output) {
      cout << "test case \"" << test_case.input << "\" is pass." << std::endl;
    } else {
      cout << "test case \"" << test_case.input << "\" is fail." << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

```

## Result

PASS


###### tags: `CodeWars` `C++`
