From 1ac7c351d9ff07677c07f8bedf06cae2b897c0b2 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary Date: Wed, 6 Oct 2021 21:10:01 +0530 Subject: [PATCH 01/17] Added Integer to Roman Added Integer to Roman code with hint and explanation --- 0012/Integer_to_roman.py | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 0012/Integer_to_roman.py diff --git a/0012/Integer_to_roman.py b/0012/Integer_to_roman.py new file mode 100644 index 0000000..4b309f8 --- /dev/null +++ b/0012/Integer_to_roman.py @@ -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 From b77df013354d8d3395349fecacef9a7ef04341c9 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:13:08 +0530 Subject: [PATCH 02/17] Readme.md --- 0012/Readme.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 0012/Readme.md diff --git a/0012/Readme.md b/0012/Readme.md new file mode 100644 index 0000000..9bf1b48 --- /dev/null +++ b/0012/Readme.md @@ -0,0 +1,31 @@ +#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. + +-> Given an integer, convert it to a roman numeral +For Example: +Input: num = 58 as (50+5+3=58) +Output: "LVIII" +xplanation: 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. + From 01769e417ab18a0a93edc489cc34ad229f355b77 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:13:40 +0530 Subject: [PATCH 03/17] Update Readme.md --- 0012/Readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 9bf1b48..48be136 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -1,4 +1,4 @@ -#Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. +# Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol | Value I | 1 @@ -16,9 +16,9 @@ M | 1000 -> 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. +_ 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. -> Given an integer, convert it to a roman numeral For Example: From 6319604c77cd79c63d2444cb90463f5e4c7725e3 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:14:14 +0530 Subject: [PATCH 04/17] Update Readme.md --- 0012/Readme.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 48be136..878d79b 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -1,4 +1,5 @@ -# Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. +# 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 @@ -20,7 +21,7 @@ _ 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. --> Given an integer, convert it to a roman numeral + For Example: Input: num = 58 as (50+5+3=58) Output: "LVIII" From 7814a8cfe6753de49a0a723eafb5089c322a4f40 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:14:40 +0530 Subject: [PATCH 05/17] Update Readme.md --- 0012/Readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 878d79b..c395a5a 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -12,10 +12,10 @@ 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 +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. From b33c3225a02c91aaf149477248030e83783a327f Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:15:01 +0530 Subject: [PATCH 06/17] Update Readme.md --- 0012/Readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index c395a5a..0003261 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -17,9 +17,9 @@ However, the numeral for four is not IIII. Instead, the number four is written a 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. +_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: From 7b7aa9759d59a4925b5522106309edc16d837653 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:15:18 +0530 Subject: [PATCH 07/17] Update Readme.md --- 0012/Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 0003261..4b255c4 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -17,9 +17,9 @@ However, the numeral for four is not IIII. Instead, the number four is written a 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. +**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. +C can be placed before D (500) and M (1000) to make 400 and 900.** For Example: From 93227cf28b4a70f791b9b0a9179771396fc959b8 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:16:10 +0530 Subject: [PATCH 08/17] Update Readme.md --- 0012/Readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 4b255c4..2e75d67 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -22,10 +22,10 @@ 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" -xplanation: L = 50, V = 5, III = 3 +***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. From fb2b7601efe13aece8e55e11e89f343179f6625e Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:16:53 +0530 Subject: [PATCH 09/17] Update Readme.md --- 0012/Readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 2e75d67..ea7c096 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -21,11 +21,11 @@ The same principle applies to the number nine, which is written as IX. There are 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 +->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. From 5d31b4fcd5723f91d32eb31107b74dbf874e7a3b Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:17:30 +0530 Subject: [PATCH 10/17] Update Readme.md --- 0012/Readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/0012/Readme.md b/0012/Readme.md index ea7c096..87055f0 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -23,9 +23,14 @@ 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. From 7c36c297d7c2682601338772c6eb22b0a2bc8973 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:17:45 +0530 Subject: [PATCH 11/17] Update Readme.md --- 0012/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0012/Readme.md b/0012/Readme.md index 87055f0..3a0cb10 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -22,7 +22,7 @@ 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: +***For Example:*** ->Input: num = 58 as (50+5+3=58) From 4708d02638c87aee0435cc53ee996010dd84a492 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:18:28 +0530 Subject: [PATCH 12/17] Update Readme.md --- 0012/Readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/0012/Readme.md b/0012/Readme.md index 3a0cb10..30cf758 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -31,7 +31,9 @@ ________________________________________________________________________________ ->Explanation: L = 50, V = 5, III = 3 --> HINT +###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. From 688c9de37cc245162bfffb10e205843f29085312 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:18:42 +0530 Subject: [PATCH 13/17] Update Readme.md --- 0012/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0012/Readme.md b/0012/Readme.md index 30cf758..76935c7 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -31,7 +31,7 @@ ________________________________________________________________________________ ->Explanation: L = 50, V = 5, III = 3 -###HINT +### HINT Create a hashmap/array with roman numerals of numbers 1, 2, 3, ..., 9, 10, 20, 30, ..., 90, 100, 200, ..., 1000, 2000, 3000, 4000 From 99f6311e044e84a216831fa4d3af605341a93424 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Thu, 7 Oct 2021 07:19:44 +0530 Subject: [PATCH 14/17] Update Readme.md --- 0012/Readme.md | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/0012/Readme.md b/0012/Readme.md index 76935c7..7e3026b 100644 --- a/0012/Readme.md +++ b/0012/Readme.md @@ -1,6 +1,8 @@ # 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. +Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. + +
 Symbol   |    Value
 I        |     1
 V        |     5
@@ -9,31 +11,28 @@ 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 +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. +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:*** +C can be placed before D (500) and M (1000) to make 400 and 900. -->Input: num = 58 as (50+5+3=58) +*For Example: -->Output: "LVIII" +Input: num = 58 as (50+5+3=58) -->Explanation: L = 50, V = 5, III = 3 +Output: "LVIII" +Explanation: L = 50, V = 5, III = 3 -### HINT - +## 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. - From 319847c1d9b0e0702505aaea2537c2baeba7a66c Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Thu, 7 Oct 2021 07:26:49 +0530 Subject: [PATCH 15/17] Update Integer_to_roman.py --- 0012/Integer_to_roman.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/0012/Integer_to_roman.py b/0012/Integer_to_roman.py index 4b309f8..5a1ced3 100644 --- a/0012/Integer_to_roman.py +++ b/0012/Integer_to_roman.py @@ -1,8 +1,8 @@ -// ->First Try Yourself read Readme.md for a hint +# First Try Yourself read Readme.md for a hint -Python Code:- +# Python Code:- -1). +# 1). table = [ ("M", 1000), ("CM", 900), ("D", 500), @@ -34,7 +34,7 @@ def to_roman(val): print(to_roman(int(s))) -2). +# 2). class Solution: def intToRoman(self, num: int) -> str: @@ -45,7 +45,7 @@ def intToRoman(self, num: int) -> str: return M[num//1000]+C[(num//100)%10]+X[(num//10)%10]+I[num%10] -3). +# 3). class Solution: # @param A : integer From 2bd998b0103fe525ab5d352a4ad0ba4603f37f46 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Thu, 7 Oct 2021 07:31:42 +0530 Subject: [PATCH 16/17] Update Integer_to_roman.py --- 0012/Integer_to_roman.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/0012/Integer_to_roman.py b/0012/Integer_to_roman.py index 5a1ced3..3c1d43a 100644 --- a/0012/Integer_to_roman.py +++ b/0012/Integer_to_roman.py @@ -1,9 +1,4 @@ -# 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), From e27ebdf354fa39b8ed746d8ac15d2955f1715fb0 Mon Sep 17 00:00:00 2001 From: Shivam Choudhary <53565563+ShivamChoudhary17@users.noreply.github.com> Date: Thu, 7 Oct 2021 07:34:32 +0530 Subject: [PATCH 17/17] Update Integer_to_roman.py --- 0012/Integer_to_roman.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/0012/Integer_to_roman.py b/0012/Integer_to_roman.py index 3c1d43a..8b686b1 100644 --- a/0012/Integer_to_roman.py +++ b/0012/Integer_to_roman.py @@ -1,11 +1,5 @@ # 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) -] +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.