Add Python doctests to karatsuba.py (#161)
* Add Python doctests to karatsuba.py `subtract()` can give the wrong answer when `y` > `x`. @atin Your review please. * add() and absolute_difference() * absolute_difference("1", "0") * Update karatsuba.py * Update karatsuba.pypull/173/head
parent
c3db2ee9f9
commit
c245d22869
|
@ -1,4 +1,18 @@
|
||||||
|
"""
|
||||||
|
https://en.wikipedia.org/wiki/Karatsuba_algorithm
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def equalize_strings(x, y):
|
def equalize_strings(x, y):
|
||||||
|
"""
|
||||||
|
>>> def eq_str(x, y):
|
||||||
|
... max_len = max(len(x), len(y))
|
||||||
|
... return x.zfill(max_len), y.zfill(max_len), max_len
|
||||||
|
>>>
|
||||||
|
>>> all(equalize_strings(x, y) == eq_str(x, y) for x, y
|
||||||
|
... in (('x' * i, 'y' * (i // 2)) for i in range(10)))
|
||||||
|
True
|
||||||
|
"""
|
||||||
n = len(x)
|
n = len(x)
|
||||||
m = len(y)
|
m = len(y)
|
||||||
zeros = abs(n - m) * '0'
|
zeros = abs(n - m) * '0'
|
||||||
|
@ -8,7 +22,13 @@ def equalize_strings(x, y):
|
||||||
x = zeros + x
|
x = zeros + x
|
||||||
return x, y, len(x)
|
return x, y, len(x)
|
||||||
|
|
||||||
def sum(x, y):
|
|
||||||
|
def add(x, y):
|
||||||
|
"""
|
||||||
|
>>> all(add(str(x), str(y)) == str(x + y) for x, y
|
||||||
|
... in ((0, 0), (0, 1), (1, 0), (1, 1234567890), (543210, 9876543)))
|
||||||
|
True
|
||||||
|
"""
|
||||||
x, y, size = equalize_strings(x, y)
|
x, y, size = equalize_strings(x, y)
|
||||||
carry = 0
|
carry = 0
|
||||||
result = ''
|
result = ''
|
||||||
|
@ -25,7 +45,13 @@ def sum(x, y):
|
||||||
result = '1' + result
|
result = '1' + result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def subtract(x, y):
|
|
||||||
|
def absolute_difference(x, y):
|
||||||
|
"""
|
||||||
|
>>> all(absolute_difference(str(x), str(y)) == str(abs(x - y)) for x, y
|
||||||
|
... in ((0, 0), (0, 1), (1, 0), (1, 1234567890), (543210, 9876543)))
|
||||||
|
True
|
||||||
|
"""
|
||||||
x, y, size = equalize_strings(x, y)
|
x, y, size = equalize_strings(x, y)
|
||||||
carry = 0
|
carry = 0
|
||||||
result = ""
|
result = ""
|
||||||
|
@ -50,7 +76,13 @@ def subtract(x, y):
|
||||||
result = str(dgt3) + result
|
result = str(dgt3) + result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def kmul(x ,y):
|
def kmul(x ,y):
|
||||||
|
"""
|
||||||
|
>>> all(kmul(str(x), str(y)) == str(x * y) for x, y
|
||||||
|
... in ((0, 0), (0, 1), (1, 0), (1, 1234567890), (543210, 9876543)))
|
||||||
|
True
|
||||||
|
"""
|
||||||
x, y, size = equalize_strings(x, y)
|
x, y, size = equalize_strings(x, y)
|
||||||
if size == 1:
|
if size == 1:
|
||||||
return str(int(x) * int(y))
|
return str(int(x) * int(y))
|
||||||
|
@ -61,15 +93,17 @@ def kmul(x ,y):
|
||||||
d = y[mid:]
|
d = y[mid:]
|
||||||
ac = kmul(a, c)
|
ac = kmul(a, c)
|
||||||
bd = kmul(b, d)
|
bd = kmul(b, d)
|
||||||
ab_cd = kmul(sum(a, b), sum(c, d))
|
ab_cd = kmul(add(a, b), add(c, d))
|
||||||
ad_bc = subtract(ab_cd, sum(ac, bd))
|
ad_bc = absolute_difference(ab_cd, add(ac, bd))
|
||||||
zeros = "0" * (size - mid)
|
zeros = "0" * (size - mid)
|
||||||
|
|
||||||
result = sum(ac + 2 * zeros, ad_bc + zeros)
|
result = add(ac + 2 * zeros, ad_bc + zeros)
|
||||||
result = sum(result, bd)
|
result = add(result, bd)
|
||||||
result = result.lstrip('0')
|
result = result.lstrip('0')
|
||||||
return result
|
return result
|
||||||
|
|
||||||
x = "134231412"
|
|
||||||
y = "141324"
|
if __name__ == "__main__":
|
||||||
print(kmul(x, y))
|
x = "134231412"
|
||||||
|
y = "141324"
|
||||||
|
print(kmul(x, y))
|
||||||
|
|
Loading…
Reference in New Issue