-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Module to validate Roman Numerals using regular expressions pattern. | ||
""" | ||
|
||
import re | ||
pattern = ''' | ||
^ # beginning of string | ||
M{0,3} # thousands - 0 to 3 Ms | ||
(CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 Cs), | ||
# or 500-800 (D, followed by 0 to 3 Cs) | ||
(XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 Xs), | ||
# or 50-80 (L, followed by 0 to 3 Xs) | ||
(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 Is), | ||
# or 5-8 (V, followed by 0 to 3 Is) | ||
$ # end of string | ||
''' | ||
|
||
print(re.search(pattern, 'M', re.VERBOSE)) | ||
# <re.Match object at 0x008EEB48> | ||
|
||
print(re.search(pattern, 'MCMLXXXIX', re.VERBOSE)) | ||
# <re.Match object at 0x008EEB48> | ||
|
||
print(re.search(pattern, 'MMMDCCCLXXXVIII', re.VERBOSE)) | ||
# <re.Match object at 0x008EEB48> | ||
|
||
print(re.search(pattern, "MCMLXXXIX", re.VERBOSE)) | ||
# <re.Match object at 0x008EEB48> | ||
|
||
print(re.search(pattern, 'M')) | ||
# None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Module to convert integers between 0-3999 from user's input into equivalent Roman Numerals. | ||
Stores the Roman Numeral as a string. | ||
""" | ||
|
||
# Integer provided by user. | ||
number = int(input("Enter a number from 0 to 3999: ")) | ||
|
||
# Dictionary mapping an integer to equivalent Roman Numeral as a string. | ||
roman = { | ||
1000: "M", | ||
900: "CM", | ||
500: "D", | ||
400: "CD", | ||
100: "C", | ||
90: "XC", | ||
50: "L", | ||
40: "XL", | ||
10: "X", | ||
9: "IX", | ||
5: "V", | ||
4: "IV", | ||
1: "I" | ||
} | ||
|
||
if number > 3999: | ||
print("Unable to convert to equivalent Roman Numeral as", | ||
number, "is greater than 3999") | ||
elif number < 0: | ||
print("You have entered a negative integer:", number) | ||
print("Negative Integers cannot be converted to equivalent Roman Numerals.") | ||
else: | ||
result = "" | ||
unit = 1 # One's place initially. | ||
while number: | ||
"""Gather successive digits in the number from right to left. | ||
Modulo operator evaluates to a float type. | ||
Convert the value of the operation to int type before storing it in variabe digit. | ||
The variable digit is used in integer division with is not compatible with float types. | ||
""" | ||
digit = int(number % 10) * unit | ||
if digit == 0: | ||
# Unit placeholder one's, ten's, hundered's and thousand's place in an integer. | ||
unit *= 10 | ||
number /= 10 | ||
continue | ||
elif digit > 0 and digit < 4: | ||
result = roman[1] * digit + result | ||
elif digit == 4: | ||
result = roman[digit] + result | ||
elif digit >= 5 and digit < 9: | ||
result = roman[5] + roman[1] * (digit - 5) + result | ||
elif digit == 9: | ||
result = roman[digit] + result | ||
elif digit >= 10 and digit < 40: | ||
result = roman[10] * (digit // unit) + result | ||
elif digit == 40: | ||
result = roman[digit] + result | ||
elif digit >= 50 and digit < 90: | ||
result = roman[50] + roman[10] * ((digit - 50) // unit) + result | ||
elif digit == 90: | ||
result = roman[digit] + result | ||
elif digit >= 100 and digit < 400: | ||
result = roman[100] * (digit // unit) * result | ||
elif digit == 400: | ||
result = roman[digit] + result | ||
elif digit >= 500 and digit < 900: | ||
result = roman[500] + roman[100] * ((digit - 500) // unit) + result | ||
elif digit == 900: | ||
result = roman[digit] + result | ||
else: | ||
result = roman[1000] * (digit // unit) + result | ||
# Multiply the placeholder by 10 to represent the next digit in the number. | ||
unit *= 10 | ||
# remove the digits from the number from right to left. | ||
number /= 10 | ||
print(result) |