-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroman_numerals.py
More file actions
158 lines (143 loc) · 5.12 KB
/
Copy pathroman_numerals.py
File metadata and controls
158 lines (143 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class RomanNumerals:
def to_roman(val):
# rule out edge cases
if val == 0:
return "0"
elif val < 0:
return "out of range --> low"
elif val > 4000:
return "out of range --> high"
else:
# get quotient and divider (divmod)
thousand = divmod(val, 1000)
hundred = divmod(thousand[1], 100)
ten = divmod(hundred[1], 10)
# get single digits
one = thousand[0]
print(one)
two = hundred[0]
print(two)
three = ten[0]
print(three)
four = ten[1]
print(four)
# turn digits into numerals
# thousand
em = ("M" * one)
# hundred
if two == 0:
ce = ""
elif two < 5 and two != 4:
ce = ("C" * two)
elif two == 4:
ce = ("CD")
elif two == 5:
ce = ("D")
elif 5 < two < 9:
ce = ("D" + ("C" * (two - 5)))
elif two == 9:
ce = ("CM")
# ten
if three == 0:
ex = ""
elif three < 5 and three != 4:
ex = ("X" * three)
elif three == 4:
ex = ("XL")
elif three == 5:
ex = ("L")
elif 5 < three < 9:
ex = ("L" + ("X" * (three - 5)))
elif three == 9:
ex = ("XC")
# one
if four == 0:
ei = ""
elif four < 4:
ei = ("I" * four)
elif four == 4:
ei = ("IV")
elif four == 5:
ei = ("V")
elif 5 < four < 9:
ei = ("V" + ("I" * (four - 5)))
elif four == 9:
ei = ("IX")
# put together
all = (em + ce + ex + ei)
return all
def from_roman(roman_num):
substring = "MMMM"
# rule out edge cases (not roman numeral and/or too long)
roman_num_test = roman_num.upper()
validRomanNumerals = ["M", "D", "C", "L", "X", "V", "I", "(", ")"]
for letters in roman_num_test:
if letters not in validRomanNumerals:
return ("not a roman numeral")
else:
if roman_num == "":
return ""
elif roman_num.startswith(substring) and (len(roman_num)) > 4:
return "out of range"
else:
# calc start here
result = 0
if "CM" in roman_num:
result = 900
roman_num = roman_num.replace("CM", "")
print(result)
print(roman_num)
if "CD" in roman_num:
result += 400
roman_num = roman_num.replace("CD", "")
print(result)
print(roman_num)
if "XC" in roman_num:
result += 90
roman_num = roman_num.replace("XC", "")
print(result)
print(roman_num)
if "XL" in roman_num:
result += 40
roman_num = roman_num.replace("XL", "")
print(result)
print(roman_num)
if "IX" in roman_num:
result += 9
roman_num = roman_num.replace("IX", "")
print(result)
print(roman_num)
if "IV" in roman_num:
result += 4
roman_num = roman_num.replace("IV", "")
print(result)
print(roman_num)
if "D" in roman_num:
result += 500
roman_num = roman_num.replace("D", "")
print(result)
print(roman_num)
if "L" in roman_num:
result += 50
roman_num = roman_num.replace("L", "")
print(result)
print(roman_num)
if "V" in roman_num:
result += 5
roman_num = roman_num.replace("V", "")
print(result)
print(roman_num)
times_m = roman_num.count("M")
result += (1000 * times_m)
print(result)
times_c = roman_num.count("C")
result += (100 * times_c)
print(result)
times_x = roman_num.count("X")
result += (10 * times_x)
print(result)
times_i = roman_num.count("I")
result += (1 * times_i)
print(result)
return(result)
print(RomanNumerals.to_roman(3847))