-
Notifications
You must be signed in to change notification settings - Fork 5
/
calFunctions.py
161 lines (134 loc) · 3.04 KB
/
calFunctions.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
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
159
160
# This is the calFunctions python file for the mycalc10.py under the directory of 07-2 in swp2 class.
from math import factorial as fact
def factorial(numStr):
try:
n = int(numStr)
r = str(fact(n))
except:
r = 'Error!'
return r
def decToBin(numStr):
try:
n = int(numStr)
r = bin(n)[2:]
except:
r = 'Error!'
return r
def binToDec(numStr):
try:
n = int(numStr, 2)
r = str(n)
except:
r = 'Error!'
return r
#def decToRoman(numStr):
# return 'dec -> Roman'
def decToRoman(numStr):
try:
n = int(numStr)
except:
return 'Error!'
if n >= 4000:
return 'Error!'
result = ''
while n >= 1000:
result += "M"
n -= 1000
while n >= 900:
result += "CM"
n -= 900
while n >= 500:
result += "D"
n -= 500
while n >= 400:
result += "CD"
n -= 400
while n >= 100:
result += "C"
n -= 100
while n >= 90:
result += "XC"
n -= 90
while n >= 50:
result += "L"
n -= 50
while n >= 40:
result += "XL"
n -= 40
while n >= 10:
result += "X"
n -= 10
while n >= 9:
result += "IX"
n -= 9
while n >= 5:
result += "V"
n -= 5
while n >= 4:
result += "IV"
n -= 4
while n >= 1:
result += "I"
n -= 1
return result
'''''''''
# 리스트와 사전을 이용해 보자!
def decToRoman(numStr):
try:
n = int(numStr)
except:
return 'Error!'
if n>= 4000:
return 'Error!'
numberBreaks = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
letters = {
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'
}
result = ''
for value in numberBreaks:
while n >= value:
result += letters[value]
n -= value
return result
'''''''''
'''''''''
def decToRoman(numStr):
try:
n = int(numStr)
except:
return 'Error!'
if n >= 4000:
return 'Error!'
romans = [
(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')
]
result = ''
for value, letters in romans:
while n >= value:
result += letters
n -= value
return result
'''''''''
def decToRoman(numStr):
try:
n = int(numStr)
except:
return 'Error!'
romans = {
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'
}
result = ''
for value in romans.keys():
while n >= value:
result += romans[value]
n -= value
return result