-
Notifications
You must be signed in to change notification settings - Fork 1
/
operation.py
executable file
·61 lines (40 loc) · 1.09 KB
/
operation.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
import math
import abc
class Operation(abc.ABC):
@abc.abstractstaticmethod
def operate(a, b):
pass
@abc.abstractstaticmethod
def to_string(a, b):
pass
class Add(Operation):
def operate(a, b):
return a + b
def to_string(a, b):
return str(a) + " + " + str(b)
class Subtract(Operation):
def operate(a, b):
return a - b
def to_string(a, b):
return str(a) + " - " + str(b)
class Multiply(Operation):
def operate(a, b):
return a * b
def to_string(a, b):
return str(a) + " * " + str(b)
class Divide(Operation):
def operate(a, b):
return a / b
def to_string(a, b):
return str(a) + " / " + str(b)
class Exponent(Operation):
def operate(a, b):
# for some reason, python detects numeric overflow floats, but not ints
return a ** float(b)
def to_string(a, b):
return str(a) + " ^ " + str(b)
class Log(Operation):
def operate(a, b):
return math.log(a, b)
def to_string(a, b):
return "log_" + str(a) + "(" + str(b) + ")"