MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / parse_roman_numerals

Function parse_roman_numerals

project_euler/problem_089/sol1.py:36–59  ·  view source on GitHub ↗

Converts a string of roman numerals to an integer. e.g. >>> parse_roman_numerals("LXXXIX") 89 >>> parse_roman_numerals("IIII") 4

(numerals: str)

Source from the content-addressed store, hash-verified

34
35
36def parse_roman_numerals(numerals: str) -> int:
37 """
38 Converts a string of roman numerals to an integer.
39 e.g.
40 >>> parse_roman_numerals("LXXXIX")
41 89
42 >>> parse_roman_numerals("IIII")
43 4
44 """
45
46 total_value = 0
47
48 index = 0
49 while index < len(numerals) - 1:
50 current_value = SYMBOLS[numerals[index]]
51 next_value = SYMBOLS[numerals[index + 1]]
52 if current_value < next_value:
53 total_value -= current_value
54 else:
55 total_value += current_value
56 index += 1
57 total_value += SYMBOLS[numerals[index]]
58
59 return total_value
60
61
62def generate_roman_numerals(num: int) -> str:

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected