# Roman to Integer

Given a roman numeral, convert it to an integer. \
\
Input is guaranteed to be within the range from 1 to 3999.

> Please read the above updated description carefully.

{% tabs %}
{% tab title="Scala" %}

```scala
val map: Map[Char, Int] = Map[Char, Int](
  'M' -> 1000,
  'D' -> 500,
  'C' -> 100,
  'L' -> 50,
  'X' -> 10,
  'V' -> 5,
  'I' -> 1
)

/**
  *
  * @param s
  * @return
  */
def romanToInt(s: String): Int = {
  var ret, prev, current = 0
  for (c <- s.reverse) {
    current = map(c)
    if (prev > current) ret -= current
    else {
      ret += current
      prev = current
    }
  }
  ret
}

romanToInt("MMMDCCXLV")
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.bernardw.com/problems/roman-to-integer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
