Solution 1 - Symbol-value scan
Scan left to right, subtracting a numeral when a larger numeral follows it; otherwise add it.
Scan left to right, subtracting a numeral when a larger numeral follows it; otherwise add it.
""" 0013.1 - Roman to Integer - Solution 1 - Symbol-value scan """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def romanToInt(self, s: str) -> int:
"""Roman to Integer Function"""
value = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
total = 0
for i, ch in enumerate(s):
if i + 1 < len(s) and value[ch] < value[s[i + 1]]:
total -= value[ch]
else:
total += value[ch]
return total
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
assert Solution().romanToInt("III") == 3
assert Solution().romanToInt("LVIII") == 58
assert Solution().romanToInt("MCMXCIV") == 1994
print("tests passed")
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()/** 0013.1 - Roman to Integer - Solution 1 - Symbol-value scan */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Roman to Integer Function */
romanToInt(s) {
const value = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };
let total = 0;
for (let i = 0; i < s.length; i++) {
const cur = value[s[i]];
const next = i + 1 < s.length ? value[s[i + 1]] : 0;
if (cur < next) {
total -= cur;
} else {
total += cur;
}
}
return total;
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase() {
/** Test Function */
const solution = new Solution();
console.assert(solution.romanToInt("III") === 3);
console.assert(solution.romanToInt("LVIII") === 58);
console.assert(solution.romanToInt("MCMXCIV") === 1994);
console.log("tests passed");
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();/** 0013.1 - Roman to Integer - Solution 1 - Symbol-value scan */
/////////////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////////////
class Solution {
/** Roman to Integer Function */
romanToInt(s: string): number {
const value: Record<string, number> = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
};
let total: number = 0;
for (let i = 0; i < s.length; i++) {
const cur: number = value[s[i]];
const next: number = i + 1 < s.length ? value[s[i + 1]] : 0;
if (cur < next) {
total -= cur;
} else {
total += cur;
}
}
return total;
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
function testcase(): void {
/** Test Function */
const solution = new Solution();
console.assert(solution.romanToInt("III") === 3);
console.assert(solution.romanToInt("LVIII") === 58);
console.assert(solution.romanToInt("MCMXCIV") === 1994);
console.log("tests passed");
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
testcase();// 0013.1 - Roman to Integer - Solution 1 - Symbol-value scan
package main
/////////////////////////////////////////////////////////////////////////////////////
// Imports
/////////////////////////////////////////////////////////////////////////////////////
import "fmt"
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
func romanToInt(s string) int {
value := map[byte]int{
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000,
}
total := 0
for i := 0; i < len(s); i++ {
cur := value[s[i]]
next := 0
if i+1 < len(s) {
next = value[s[i+1]]
}
if cur < next {
total -= cur
} else {
total += cur
}
}
return total
}
func testcase() {
// Test Function
fmt.Println(romanToInt("III") == 3)
fmt.Println(romanToInt("LVIII") == 58)
fmt.Println(romanToInt("MCMXCIV") == 1994)
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
func main() {
testcase()
}// 0013.1 - Roman to Integer - Solution 1 - Symbol-value scan
/////////////////////////////////////////////////////////////////////////////////////
// Structs
/////////////////////////////////////////////////////////////////////////////////////
struct Solution;
/////////////////////////////////////////////////////////////////////////////////////
// Implementations
/////////////////////////////////////////////////////////////////////////////////////
impl Solution {
/// Roman to Integer Function
pub fn roman_to_int(s: String) -> i32 {
fn value(ch: u8) -> i32 {
match ch {
b'I' => 1,
b'V' => 5,
b'X' => 10,
b'L' => 50,
b'C' => 100,
b'D' => 500,
_ => 1000, // 'M'
}
}
let bytes = s.as_bytes();
let mut total: i32 = 0;
for i in 0..bytes.len() {
let cur = value(bytes[i]);
let next = if i + 1 < bytes.len() { value(bytes[i + 1]) } else { 0 };
if cur < next {
total -= cur;
} else {
total += cur;
}
}
total
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////////////
fn testcase() {
// Test Function
assert_eq!(Solution::roman_to_int("III".to_string()), 3);
assert_eq!(Solution::roman_to_int("LVIII".to_string()), 58);
assert_eq!(Solution::roman_to_int("MCMXCIV".to_string()), 1994);
println!("tests passed");
}
/////////////////////////////////////////////////////////////////////////////////////
// Main
/////////////////////////////////////////////////////////////////////////////////////
fn main() {
testcase();
}