forked from yukuku/telebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numeralconverter.py
105 lines (96 loc) · 4 KB
/
numeralconverter.py
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
def is_number(s):
try:
float(s)
try:
test = int(s)
return True
except ValueError:
return False
except ValueError:
return False
# https://github.com/Walkman100/NumeralConverter/blob/17fe334fb34088fbd7c7c25ef31fba5df8961512/Python27/numeralconverter.py#L68
def checkAndReturnRomanNumeral(input):
if is_number(input):
if len(input) < 6:
return(returnRomanNumeral(int(input)))
else:
return("no.")
else:
return("\"" + input + "\" is not an Arabic number!")
def returnRomanNumeral(number):
returnString = ""
if number >= 1000:
for i in range(1, (number // 1000) +1):
returnString = returnString + "M"
number -= (number // 1000) * 1000
while number >= 900:
number -= 900
returnString = returnString + "CM"
while number >= 500:
number -= 500
returnString = returnString + "D"
while number >= 400:
number -= 400
returnString = returnString + "CD"
while number >= 100:
number -= 100
returnString = returnString + "C"
while number >= 90:
number -= 90
returnString = returnString + "XC"
while number >= 50:
number -= 50
returnString = returnString + "L"
while number >= 40:
number -= 40
returnString = returnString + "XL"
while number >= 10:
number -= 10
returnString = returnString + "X"
while number >= 9:
number -= 9
returnString = returnString + "IX"
while number >= 5:
number -= 5
returnString = returnString + "V"
while number >= 4:
number -= 4
returnString = returnString + "IV"
while number >= 1:
number -= 1
returnString = returnString + "I"
return("`" + returnString + "`")
def returnArabicNumber(RomanNumber):
RomanNumber = RomanNumber.upper()
for i in range(0, len(RomanNumber)):
# https://stackoverflow.com/a/1228327/2999220
if RomanNumber[i] == "I": RomanNumber = RomanNumber[:i] + "1" + RomanNumber[i + 1:]
elif RomanNumber[i] == "V": RomanNumber = RomanNumber[:i] + "2" + RomanNumber[i + 1:]
elif RomanNumber[i] == "X": RomanNumber = RomanNumber[:i] + "3" + RomanNumber[i + 1:]
elif RomanNumber[i] == "L": RomanNumber = RomanNumber[:i] + "4" + RomanNumber[i + 1:]
elif RomanNumber[i] == "C": RomanNumber = RomanNumber[:i] + "5" + RomanNumber[i + 1:]
elif RomanNumber[i] == "D": RomanNumber = RomanNumber[:i] + "6" + RomanNumber[i + 1:]
elif RomanNumber[i] == "M": RomanNumber = RomanNumber[:i] + "7" + RomanNumber[i + 1:]
else:
return("\"" + RomanNumber[i] + "\" is not a valid Roman Numeral character!")
# Now we have the roman number in arabic numbers (so we can use < and >), we just add it all
ArabicNumber = 0
RomanNumber = RomanNumber + "0" # Because loops, length calculation and next letter calculation
for i in range(0, len(RomanNumber)):
if i < len(RomanNumber) - 1 and RomanNumber[i] >= RomanNumber[i + 1]:
if RomanNumber[i] == "1": ArabicNumber += 1
elif RomanNumber[i] == "2": ArabicNumber += 5
elif RomanNumber[i] == "3": ArabicNumber += 10
elif RomanNumber[i] == "4": ArabicNumber += 50
elif RomanNumber[i] == "5": ArabicNumber += 100
elif RomanNumber[i] == "6": ArabicNumber += 500
elif RomanNumber[i] == "7": ArabicNumber += 1000
elif i < len(RomanNumber) - 1:
if RomanNumber[i] == "1": ArabicNumber -= 1
elif RomanNumber[i] == "2": ArabicNumber -= 5
elif RomanNumber[i] == "3": ArabicNumber -= 10
elif RomanNumber[i] == "4": ArabicNumber -= 50
elif RomanNumber[i] == "5": ArabicNumber -= 100
elif RomanNumber[i] == "6": ArabicNumber -= 500
elif RomanNumber[i] == "7": ArabicNumber -= 1000
return("`" + str(ArabicNumber) + "`")