Skip to content

Instantly share code, notes, and snippets.

@yoshiki-0428
Created September 2, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshiki-0428/b08dfe26df3b3a777a34a00dab406c29 to your computer and use it in GitHub Desktop.
Save yoshiki-0428/b08dfe26df3b3a777a34a00dab406c29 to your computer and use it in GitHub Desktop.
class Solution:
def romanToInt(self, s: str) -> int:
symbol_map = {
"I": 1,
"V" : 5,
"X": 10,
"L" : 50,
"C" : 100,
"D" : 500,
"M" : 1000,
}
# convert to int
convert_nums = []
for i in range(len(s)):
try:
symbol_map[s[i]]
except KeyError:
continue
convert_nums.append(symbol_map[s[i]])
print(convert_nums)
result_nums = []
skip = False
# for j in reversed(range(len(convert_nums))):
# # big num is right there cause minus
# if convert_nums[j - 1] < convert_nums[j] and not j == 0:
# result_nums.append(convert_nums[j] - convert_nums[j - 1])
# skip = True
# elif skip:
# skip = False
# elif not skip:
# result_nums.append(convert_nums[j])
index = len(convert_nums) - 1
while index >= 0:
# big num is right there cause minus
if convert_nums[index - 1] < convert_nums[index] and not index == 0:
result_nums.append(convert_nums[index] - convert_nums[index - 1])
index -= 1
else:
result_nums.append(convert_nums[index])
index -= 1
print(result_nums)
return sum(result_nums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment