aboutsummaryrefslogtreecommitdiff
path: root/challenge-227/eric-cheung/python/ch-2.py
blob: ce59ffaa51514b7a87264cd3aa69cd440c7a873e (plain)
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
import roman

nMaxLimit = 3999

def GetRomanArithOper (strInput):

    global nMaxLimit

    arrElem = strInput.split()
    nInt_01 = roman.fromRoman(arrElem[0])
    strOperator = arrElem[1]
    nInt_02 = roman.fromRoman(arrElem[2])

    if strOperator == "+":
        if nInt_01 + nInt_02 > nMaxLimit:
            return "non potest"
        return roman.toRoman(nInt_01 + nInt_02)

    if strOperator == "-":
        if nInt_01 > nInt_02:
            return roman.toRoman(nInt_01 - nInt_02)
        if nInt_01 == nInt_02:
            return "nulla"
        return "non potest"

    if strOperator == "/":
        if nInt_01 % nInt_02 == 0:
            return roman.toRoman(int(nInt_01 / nInt_02))
        return "non potest"

    if strOperator == "*":
        if nInt_01 * nInt_02 > nMaxLimit:
            return "non potest"
        return roman.toRoman(nInt_01 * nInt_02)

    if strOperator == "**":
        if nInt_01 ** nInt_02 > nMaxLimit:
            return "non potest"
        return roman.toRoman(nInt_01 ** nInt_02)


## strGivenExpr = "IV + V"
## strGivenExpr = "M - I"
## strGivenExpr = "X / II"
## strGivenExpr = "XI * VI"
## strGivenExpr = "VII ** III"
## strGivenExpr = "V - V"
## strGivenExpr = "V / II"
## strGivenExpr = "MMM + M"
strGivenExpr = "V - X"

print (GetRomanArithOper(strGivenExpr))