Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c0eea19

Browse files
author
Amogh Singhal
authored
Update interger_to_roman_num.py
1 parent b72f0bd commit c0eea19

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

‎interger_to_roman_num.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Write a program to convert interger to Roman Representation
2-
def convertIntegerToRomanNum(inputNumber):
2+
def convertIntegerToRomanNum(input_num):
33

44
number_list = [
55
1000, 900, 500, 400,
@@ -18,12 +18,35 @@ def convertIntegerToRomanNum(inputNumber):
1818
result_str = ''
1919
i = 0
2020

21-
while inputNumber > 0:
22-
for _ in range(inputNumber//number_list[i]):
21+
while input_num > 0:
22+
# if input_num//number_list[i] > 0 , then we will get a string appended
23+
# else it will continue the loop
24+
for _ in range(input_num//number_list[i]):
2325
result_str = result_str + symbol_list[i]
24-
inputNumber = inputNumber - number_list[i]
26+
input_num = input_num - number_list[i]
2527
i = i + 1
2628
return result_str
2729

2830
roman = convertIntegerToRomanNum(289)
2931
print(roman)
32+
33+
# ===============
34+
# Explaination:
35+
# ===============
36+
# 289
37+
# 289//1000 --> 0, i=1
38+
# 289//900 --> 0, i=2
39+
# 289//500 --> 0, i=3
40+
# 289//400 --> 0, i=4
41+
# 289//100 --> 2, then i=4, result_str = 'C', input_num = 189
42+
# 198//100 --> 1, then i=4, result_str='CC', input_num = 89
43+
# 98//100 --> 0, then i=5
44+
# 98//50 --> 1, then i=6, result_str='CCL', input_num= 39
45+
# 39//50 --> 0, then i=6
46+
# 39//40 --> 0, then i=7
47+
# 39//10 --> 3, then i=8, result_str='CCLX', input_num=29
48+
# 29//10 --> 3, then i=8, result_str='CCLXX', input_num=19
49+
# 19//10 --> 3, then i=8, result_str='CCLXXX', input_num=9
50+
# 9//10 --> 0, then i=9
51+
# 9//9 --> 1, then i=9, result_str='CCLXXXIX', input_num=0
52+
# EXIT

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /