Skip to content Skip to sidebar Skip to footer

Implementing Karatsuba Recursion Function In Python Class, Errors

Previously I posted a question on this subject and it was answered quite well. Implementing merge sort function in python class, errors And yet there is still something that escape

Solution 1:

As I was typing up this question, I found myself looking over my code in a different way, i.e. thinking about how best to explain my problem clearly, and thinking about the ValueError, and I discovered the problem.

I was correct to follow the intuition provided by abarnert in the previously linked question. The problem was in how the rest of the function needed the values from the recursion subroutines; as you can see from the ValueError, the memory location of the recursion subroutine was being passed along instead of the value generated by the subroutine. The solution then was straightforward: Modify ac.karatsuba() to ac = ac.karatsuba(), etc... and voila!

I think this (and the previously linked problem) serves as a good tutorial for those trying to understand how to implement recursion in python classes.

I hope you agree and give me good votes!

Here is the working class code:

classKaratsuba(object):
    def__init__(self, x, y):
        self.x = x
        self.y = y

    defzeroPad(self, numberString, zeros, left = True):
        """Return the string with zeros added to the left or right."""for i inrange(zeros):
            if left:
                numberString = '0' + numberString
            else:
                numberString = numberString + '0'return numberString

    defkaratsuba(self):
        """Multiply two integers using Karatsuba's algorithm."""#convert to strings for easy access to digits
        self.x = str(self.x)
        self.y = str(self.y)
        #base case for recursioniflen(self.x) == 1andlen(self.y) == 1:
            returnint(self.x) * int(self.y)
        iflen(self.x) < len(self.y):
            self.x = self.zeroPad(self.x, len(self.y) - len(self.x))
        eliflen(self.y) < len(self.x):
            self.y = self.zeroPad(self.y, len(self.x) - len(self.y))
        n = len(self.x)
        j = n//2#for odd digit integersif (n % 2) != 0:
            j += 1    
        BZeroPadding = n - j
        AZeroPadding = BZeroPadding * 2
        self.a = int(self.x[:j])
        self.b = int(self.x[j:])
        self.c = int(self.y[:j])
        self.d = int(self.y[j:])
        #recursively calculate#         ac = self.karatsuba(self.a, self.c)#         bd = self.karatsuba(self.b, self.d)
        ac = Karatsuba(self.a, self.c)
        ac = ac.karatsuba()
        bd = Karatsuba(self.b, self.d)
        bd = bd.karatsuba()
        k = Karatsuba(self.a + self.b, self.c + self.d)
        k = k.karatsuba()
#         k = self.karatsuba(self.a + self.b, self.c + self.d)

        A = int(self.zeroPad(str(ac), AZeroPadding, False))
        B = int(self.zeroPad(str(k - ac - bd), BZeroPadding, False))
        return A + B + bd

Post a Comment for "Implementing Karatsuba Recursion Function In Python Class, Errors"