Skip to content

Added Integer to Roman in folder 0012 #69

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
95 changes: 95 additions & 0 deletions 0012/Integer_to_roman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ->First Try Yourself read Readme.md for a hint

Python Code:-

1).

table = [
("M", 1000), ("CM", 900), ("D", 500),
("CD", 400), ("C", 100), ("XC", 90),
("L", 50), ("XL", 40), ("X", 10),
("IX", 9), ("V", 5), ("IV", 4),
("I", 1)
]

def to_roman(val):
# All test case for numbers > 4000 are wrong.
# I have made a fix to make those numbers as
if val>4000:
val -= 1000
# to make the test cases pass. So stupid !!!

v = []
for a, d in table:
while d <= val:
val -= d
v.append(a)
return ''.join(v)

fp = open('input')
while True:
s = fp.readline()
if not s:
break
print(to_roman(int(s)))


2).

class Solution:
def intToRoman(self, num: int) -> str:
M=["","M","MM","MMM"]
C=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]
X=["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"]
I=["","I","II","III","IV","V","VI","VII","VIII","IX"]

return M[num//1000]+C[(num//100)%10]+X[(num//10)%10]+I[num%10]

3).

class Solution:
# @param A : integer
# @return a strings
def intToRoman(self, A):
roman = ""
if A >= 1000:
roman = "M"*(A/1000)
A = A%1000
if A >= 900 and A < 1000:
roman += "CM"
A -= 900
if A >= 500 and A < 900:
roman += "D"
A -= 500
if A >= 400 and A < 500:
roman += "CD"
A -= 400
if A >= 100 and A < 400:
roman += "C"*(A/100)
A = A % 100
if A >= 90 and A < 100:
roman += "XC"
A -= 90
if A >= 50 and A < 90:
roman += "L"
A -= 50
if A >= 40 and A < 50:
roman += "XL"
A -= 40
if A >= 10 and A < 40:
roman += "X"*(A/10)
A = A % 10
if A == 9:
roman += "IX"
A -= 9
if A >= 5 and A < 9:
roman += "V"
A -= 5
if A == 4:
roman += "IV"
if A >= 1 and A < 4:
roman += "I"*(A)



return roman
39 changes: 39 additions & 0 deletions 0012/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Given an integer, convert it to a roman numeral
### Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol | Value
I | 1
V | 5
X | 10
L | 50
C | 100
D | 500
M | 1000


-> For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II
Roman numerals are usually written largest to smallest from left to right.
However, the numeral for four is not IIII. Instead, the number four is written as IV.
Because the one is before the five we subtract it making four.
The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used

**I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.**

______________________________________________________________________________________________________________________________________________________________________________________
***For Example:***

->Input: num = 58 as (50+5+3=58)

->Output: "LVIII"

->Explanation: L = 50, V = 5, III = 3


### HINT


Create a hashmap/array with roman numerals of numbers 1, 2, 3, ..., 9, 10, 20, 30, ..., 90, 100, 200, ..., 1000, 2000, 3000, 4000
For any given number, find out its one’s, ten’s, hundred’s and thousand’s place and generate the roman number using the generated hash.