Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Roman number to decimal #2018

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Roman number to decimal
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

def value(r):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use switch statement, they are far better instead of this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea

if (r == 'I'):
return 1
if (r == 'V'):
return 5
if (r == 'X'):
return 10
if (r == 'L'):
return 50
if (r == 'C'):
return 100
if (r == 'D'):
return 500
if (r == 'M'):
return 1000
return -1

def romanToDecimal(str):
res = 0
i = 0

while (i < len(str)):
s1 = value(str[i])
if (i + 1 < len(str)):
s2 = value(str[i + 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leetcode problem idea...I see.

if (s1 >= s2):
res = res + s1
i = i + 1
else:
res = res + s2 - s1
i = i + 2
else:
res = res + s1
i = i + 1

return res

romannumber = input("Enter the Roman Number you want to convert: ")
print("Integer form of Roman Numeral is: " + str(romanToDecimal(romannumber)))