-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic_classes.py
118 lines (97 loc) · 3.28 KB
/
arithmetic_classes.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
from dataclasses import dataclass
from math import sqrt, factorial
class Arithmetic:
'''
right shift operator will be overridden for relevant subclasses
the override is used to chain mathematical operations
'''
pass
@dataclass
class Number(Arithmetic):
value: float
repr: str
'''
Class to represent the numbers.
Stores both value and the steps used to arrive at that value.
Not to be initialized directly, use get method.
'''
@staticmethod
def get(value):
return Number(value, str(value))
def is_positive(self):
if self.value >= 0:
return True
else:
return False
def integer_root(self):
return sqrt(self.value * self.is_positive() - self.value * (not self.is_positive())).is_integer()
def __rshift__(self, other):
if isinstance(other, DuoOperator):
return PartialOperation(self, other)
elif isinstance(other, PostSingularOperator):
assert self.value == int(self.value)
return Number(factorial(int(self.value)), '(' + self.repr + '!)')
else:
raise NotImplementedError('Number can only be followed by PostSingularOperator or DuoOperator')
@dataclass
class Operator(Arithmetic):
value: str
repr: str
'''
Base operator class.
Not to be initialized directly, use get method.
'''
@staticmethod
def get(value):
if value == '!':
return PostSingularOperator(value, value)
elif value in ['sqrt', '-()']:
return PreSingularOperator(value.rstrip('()'), value.rstrip('()'))
elif value in ['+', '-', '*', '/']:
return DuoOperator(value, value)
else:
raise NotImplementedError('Unidentified Operator')
@dataclass
class PreSingularOperator(Operator):
'''
For operators that operate on a single number and precede it.
Currently only minus (-) and sqrt are defined.
'''
def __rshift__(self, other):
if isinstance(other, Number):
if self.value == 'sqrt':
return Number(sqrt(other.value), 'sqrt(' + other.repr + ')')
elif self.value == '-':
return Number(-other.value, '(-' + other.repr + ')')
else:
raise NotImplementedError('Unknown Singular Operator, shouldn\'t be here')
else:
raise NotImplementedError
@dataclass
class PostSingularOperator(Operator):
'''
for operators that operate on single number and follows it
currently only factorial is defined
'''
pass
@dataclass
class DuoOperator(Operator):
repr: str
'''
For operators that require 2 numbers
'''
pass
@dataclass
class PartialOperation(Arithmetic):
number: Number
next: DuoOperator
'''
As the attributes suggest, this is a partial merge between Number and following DuoOperator.
It needs to be followed by Number to complete the operation.
'''
def __rshift__(self, other):
if isinstance(other, Number):
return Number(eval(f'{self.number.value} {self.next.value} {other.value}'),
'(' + self.number.repr + self.next.repr + other.repr + ')')
else:
raise NotImplementedError('PartialOperation can only be followed by Number')