-
Notifications
You must be signed in to change notification settings - Fork 0
/
strmath.py
197 lines (161 loc) · 5.23 KB
/
strmath.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
def truncate_left(s):
return s[1:]
def truncate_right(s):
return s[:-1]
def last(s):
return s[-1]
class StrNum(object):
"""
Created on Jan 31, 2016
@author: Erik Colban
"""
# Defining 32-bit numbers in little-endian
digit_to_binary = {
'0':'00000000000000000000000000000000',
'1':'10000000000000000000000000000000',
'2':'01000000000000000000000000000000',
'3':'11000000000000000000000000000000',
'4':'00100000000000000000000000000000',
'5':'10100000000000000000000000000000',
'6':'01100000000000000000000000000000',
'7':'11100000000000000000000000000000',
'8':'00010000000000000000000000000000',
'9':'10010000000000000000000000000000'}
binary_to_digit = {b: d for d, b in digit_to_binary.items()}
def __init__(self, s, binary=False):
# should have validated s, but trusting the client instead
if binary:
self._value = s
elif s in StrNum.digit_to_binary:
self._value = StrNum.digit_to_binary[s]
else:
n = zero
sgn = one
for d in s:
if d == '-': sgn = -sgn
else:
n *= ten
n += StrNum(d)
self._value = (sgn * n)._value
def __add__(self, other):
def bit_gen():
carry = '0'
for x in zip(self._value, other._value):
if x == ('0', '0'):
yield carry
carry = '0'
elif x == ('1', '1'):
yield carry
carry = '1'
elif carry == '1':
yield '0'
else:
yield '1'
return StrNum(''.join(bit_gen()), binary=True)
def __lshift__(self, other):
b = one
s = self._value
while b <= other:
s = truncate_right('0' + s)
b = b + one
return StrNum(s, binary=True)
def __rshift__(self, other):
b = one
s = self._value
c = '1' if self.__is_negative() else '0'
while b <= other:
s = truncate_left(s + c)
b += one
return StrNum(s, binary=True)
def __neg__(self):
one_complement = ''.join('1' if c == '0' else '0' for c in self._value)
return StrNum(one_complement, binary=True) + one
def __sub__(self, other):
return self + -other
def __is_negative(self):
return last(self._value) == '1'
def __abs__(self):
if self.__is_negative(): return -self
else: return self
def __eq__(self, other):
return self._value == other._value
def __ne__(self, other):
return self._value != other._value
def __gt__(self, other):
return (other - self).__is_negative()
def __ge__(self, other):
return self == other or self > other
def __lt__(self, other):
return (self - other).__is_negative()
def __le__(self, other):
return self == other or self < other
def __mul__(self, other, mod=None):
if self.__is_negative():
return -(-self).__mul__(other, mod)
result = zero
for b in reversed(self._value):
result <<= one
if b == '1':
result += other
while mod and result >= mod:
result -= mod
return result
def __divmod__(self, other):
if self.__is_negative() and other.__is_negative():
q, r = divmod(-self, -other)
return q, -r
elif self.__is_negative():
q, r = divmod(-self, other)
q, r = -q, -r
if r != zero:
r += other
q -= one
return q, r
elif other.__is_negative():
q, r = divmod(self, -other)
q = -q
if r != zero:
r += other
q -= one
return q, r
else: # neither self nor other is negative
q, r = zero, zero
for b in reversed(self._value):
q, r = q << one, r << one
if b == '1':
r += one
if r >= other:
r -= other
q += one
return q, r
def __floordiv__(self, other):
q, _ = divmod(self, other)
return q
def __mod__(self, other):
_, r = divmod(self, other)
return r
def __pow__(self, n, mod=None):
if mod and self >= mod:
return pow((self % mod), n, mod)
p = one
for b in reversed(n._value):
p = p.__mul__(p, mod)
if b == '1':
p = p.__mul__(self, mod)
return p
def __repr__(self):
return "StrNum('%s')" % str(self)
def __str__(self):
if self == zero: return '0'
if self < zero: return '-' + str(-self)
result = ""
s = self
while s > zero:
s, r = s.__divmod__(ten)
result = StrNum.binary_to_digit[r._value] + result
return result
def __hash__(self):
return hash(self._value)
zero = StrNum('0')
one = StrNum('1')
ten = StrNum('01010000000000000000000000000000', binary=True)