Skip to content

Commit 78c1c5b

Browse files
committed
finish roman nuerals decoder
1 parent 96c882c commit 78c1c5b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

[4 kyu]Roman Numerals Decoder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def solution(roman):
2+
trans = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
3+
signToValue = [trans[c] for c in roman]
4+
values = 0
5+
current = 0
6+
for v in signToValue[::-1]:
7+
values += v if v >= current else -v
8+
current = v
9+
return values
10+
11+
print(solution('XX'))

[leetcode]Roman to Integer.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
# @param {string} s
3+
# @return {integer}
4+
def romanToInt(self, s):
5+
trans = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
6+
signToValue = [trans[c] for c in s]
7+
values = 0
8+
current = 0
9+
for v in signToValue[::-1]:
10+
values += v if v >= current else -v
11+
current = v
12+
return values
13+
14+
15+
16+
test = Solution()
17+
print(test.romanToInt('XXXVII')) #37
18+
print(test.romanToInt('IXXX')) #29
19+
print(test.romanToInt('MCMXCVI')) #1996

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy