-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexoInteger.py
261 lines (199 loc) · 8.81 KB
/
lexoInteger.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from typing import List
from lexoNumeralSystem import ILexoNumeralSystem
class LexoInteger:
def __init__(self, system: 'ILexoNumeralSystem', sign: int, mag: List[int]):
self.sys = system
self.sign = sign
self.mag = mag
@staticmethod
def parse(str_full: str, system: 'ILexoNumeralSystem') -> 'LexoInteger':
str_ = str_full
sign = 1
if str_full.startswith(system.get_positive_char()):
str_ = str_full[1:]
elif str_full.startswith(system.get_negative_char()):
str_ = str_full[1:]
sign = -1
mag = [0] * len(str_)
str_index = len(mag) - 1
for mag_index in range(len(mag)):
mag[mag_index] = system.to_digit(str_[str_index])
str_index -= 1
return LexoInteger.make(system, sign, mag)
@staticmethod
def zero(sys: 'ILexoNumeralSystem') -> 'LexoInteger':
return LexoInteger(sys, 0, [0])
@staticmethod
def one(sys: 'ILexoNumeralSystem') -> 'LexoInteger':
return LexoInteger.make(sys, 1, [1])
@staticmethod
def make(sys: 'ILexoNumeralSystem', sign: int, mag: List[int]) -> 'LexoInteger':
actual_length = len(mag)
while actual_length > 0 and mag[actual_length - 1] == 0:
actual_length -= 1
if actual_length == 0:
return LexoInteger.zero(sys)
if actual_length == len(mag):
return LexoInteger(sys, sign, mag)
nmag = [0] * actual_length
for i in range(actual_length):
nmag[i] = mag[i]
return LexoInteger(sys, sign, nmag)
@staticmethod
def add_3(sys: 'ILexoNumeralSystem', l: List[int], r: List[int]) -> List[int]:
estimated_size = max(len(l), len(r))
result = [0] * estimated_size
carry = 0
for i in range(estimated_size):
lnum = l[i] if i < len(l) else 0
rnum = r[i] if i < len(r) else 0
sum_ = lnum + rnum + carry
carry = 0
while sum_ >= sys.get_base():
sum_ -= sys.get_base()
carry += 1
result[i] = sum_
if carry > 0:
result.append(carry)
return result
@staticmethod
def subtract_3(sys: 'ILexoNumeralSystem', l: List[int], r: List[int]) -> List[int]:
r_complement = LexoInteger.complement_3(sys, r, len(l))
r_sum = LexoInteger.add_3(sys, l, r_complement)
r_sum[-1] = 0
return LexoInteger.add_3(sys, r_sum, [1])
@staticmethod
def multiply(sys: 'ILexoNumeralSystem', l: List[int], r: List[int]) -> List[int]:
result = [0] * (len(l) + len(r))
for li in range(len(l)):
for ri in range(len(r)):
result_index = li + ri
result[result_index] += l[li] * r[ri]
while result[result_index] >= sys.get_base():
result[result_index + 1] += 1
result[result_index] -= sys.get_base()
return result
@staticmethod
def complement_3(sys: 'ILexoNumeralSystem', mag: List[int], digits: int) -> List[int]:
if digits <= 0:
raise ValueError('Expected at least 1 digit')
nmag = [sys.get_base() - 1] * digits
for i in range(len(mag)):
nmag[i] = sys.get_base() - 1 - mag[i]
return nmag
@staticmethod
def compare(l: List[int], r: List[int]) -> int:
if len(l) < len(r):
return -1
if len(l) > len(r):
return 1
for i in range(len(l) - 1, -1, -1):
if l[i] < r[i]:
return -1
if l[i] > r[i]:
return 1
return 0
def add(self, other: 'LexoInteger') -> 'LexoInteger':
self.check_system(other)
if self.is_zero():
return other
if other.is_zero():
return self
if self.sign != other.sign:
pos = self.negate() if self.sign == -1 else other.negate()
val = pos.subtract(other if self.sign == -1 else self)
return val.negate()
result = LexoInteger.add_3(self.sys, self.mag, other.mag)
return LexoInteger.make(self.sys, self.sign, result)
def subtract(self, other: 'LexoInteger') -> 'LexoInteger':
self.check_system(other)
if self.is_zero():
return other.negate()
if other.is_zero():
return self
if self.sign != other.sign:
negate = self.negate() if self.sign == -1 else other.negate()
return self.add(negate)
cmp = LexoInteger.compare(self.mag, other.mag)
if cmp == 0:
return LexoInteger.zero(self.sys)
sign = -1 if cmp < 0 else 1
mag = LexoInteger.subtract_3(self.sys, other.mag, self.mag) if cmp < 0 else LexoInteger.subtract_3(self.sys, self.mag, other.mag)
return LexoInteger.make(self.sys, sign, mag)
def multiply(self, other: 'LexoInteger') -> 'LexoInteger':
self.check_system(other)
if self.is_zero() or other.is_zero():
return LexoInteger.zero(self.sys)
if self.is_oneish():
return LexoInteger.make(self.sys, self.sign * other.sign, other.mag)
if other.is_oneish():
return LexoInteger.make(self.sys, self.sign * other.sign, self.mag)
new_mag = LexoInteger.multiply(self.sys, self.mag, other.mag)
return LexoInteger.make(self.sys, self.sign * other.sign, new_mag)
def negate(self) -> 'LexoInteger':
return self if self.is_zero() else LexoInteger.make(self.sys, -self.sign, self.mag)
def shift_left(self, times: int = 1) -> 'LexoInteger':
if times == 0:
return self
if times < 0:
return self.shift_right(-times)
nmag = [0] * (len(self.mag) + times)
for i in range(len(self.mag)):
nmag[i + times] = self.mag[i]
return LexoInteger.make(self.sys, self.sign, nmag)
def shift_right(self, times: int = 1) -> 'LexoInteger':
if len(self.mag) - times <= 0:
return LexoInteger.zero(self.sys)
nmag = self.mag[times:]
return LexoInteger.make(self.sys, self.sign, nmag)
def complement(self) -> 'LexoInteger':
return self.complement_digits(len(self.mag))
def complement_digits(self, digits: int) -> 'LexoInteger':
return LexoInteger.make(self.sys, self.sign, LexoInteger.complement(self.sys, self.mag, digits))
def is_zero(self) -> bool:
return self.sign == 0 and len(self.mag) == 1 and self.mag[0] == 0
def is_one(self) -> bool:
return self.sign == 1 and len(self.mag) == 1 and self.mag[0] == 1
def get_mag(self, index: int) -> int:
return self.mag[index]
def compare_to(self, other: 'LexoInteger') -> int:
if self is other:
return 0
if other is None:
return 1
if self.sign == -1:
if other.sign == -1:
cmp = LexoInteger.compare(self.mag, other.mag)
return 1 if cmp == -1 else -1 if cmp == 1 else 0
return -1
if self.sign == 1:
return LexoInteger.compare(self.mag, other.mag) if other.sign == 1 else 1
if other.sign == -1:
return 1
return -1 if other.sign == 1 else 0
def get_system(self) -> 'ILexoNumeralSystem':
return self.sys
def format(self) -> str:
if self.is_zero():
return str(self.sys.to_char(0))
sb = []
for digit in self.mag:
sb.insert(0, self.sys.to_char(digit))
if self.sign == -1:
sb.insert(0, self.sys.get_negative_char())
return ''.join(sb)
def equals(self, other: 'LexoInteger') -> bool:
if self is other:
return True
if other is None:
return False
return self.sys.get_base() == other.sys.get_base() and self.compare_to(other) == 0
def __str__(self) -> str:
return self.format()
def is_oneish(self) -> bool:
return len(self.mag) == 1 and self.mag[0] == 1
def check_system(self, other: 'LexoInteger'):
if self.sys.get_base() != other.sys.get_base():
raise ValueError('Expected numbers of same numeral sys')
#This Python code translates the provided TypeScript code for the `LexoInteger` class. It uses native Python data structures and functions for similar data structures in the input code. The class provides methods for parsing, creating, adding, subtracting, multiplying, negating, shifting, complementing, and comparing `LexoInteger` objects. It also includes helper methods for performing arithmetic operations on the underlying magnitude arrays.
#Note that the `ILexoNumeralSystem` interface and the `lexoHelper` module are not provided in the input code, so they are represented as string types in the Python code. You may need to replace them with appropriate implementations or import statements.