-1

As a new python programmer, I'm working on a Leetcode question and don't know why my code does not work, so I really appreciate your advice:

Question:

Implement pow(x, n), which calculates x raised to the power n.

Example: Input: 2.00000, 10

Output: 1024.00000

Here is my python code (I tried to use divide and conquer concept):

class Solution:
    def myPow(self, x, n):
        if n == 0:
            return 0
        if n == 1:
            return x
        return self.power(x,n)
    def power(self,x,n):
        if n == 1:
            return x
        self.power(x,n//2)
        self.multiply(x,x)
        return x
    def multiply(self,x,y):
        x = x*y
        return x
test3=Solution()
test3.myPow(2,4)

But the results give 2 instead of 16. I expect the above code to work as follows:

power(2,4)->power(2,2)->power(2,1), which reaches the base case since n==1, and then we proceed with power(2,2),due to function multiply(x,x) or multiply(2,2) in this case, I expect x to become 4 (x = 2*2), and then we proceed with power(2,4), due to function multiply(x,x), x = 4*4 = 16

I don't know why I am wrong, could any expert give me some advice?

M00000001
  • 309
  • 2
  • 10

4 Answers4

1
class Solution:
    def myPow(self, x, n):
        if n == 0:
            return 1
        if n == 1:
            return x
        return self.power(x,n)

    def power(self,x,n):
        if n == 1:
            return x
        x = self.power(x,n//2)
        return self.multiply(x,x)

    def multiply(self,x,y):
        x = x*y
        return x

test3=Solution()
test3.myPow(2,4)

This code only fixes some minor problems in your code, but still you should consider cases when the power n is an odd number.

aminrd
  • 4,300
  • 4
  • 23
  • 45
1

x^0 always equals 1, so your first "if" in myPow() is not accurate. Also, your power() function always returns x, because the lines:

self.power(x,n//2)
self.multiply(x,x)

don't assign the value they return to anything.

mlg
  • 38
  • 5
1

You aren't storing the return values from your self.power() and self.multiply() within power().

This is due to function scope. When you change x within multiply(), it is only changed within that function. You are correctly returning the changed value but you don't store it in the calling function.

Changing power() to the following works for your example (2^4).

def power(self,x,n):
    if n == 1:
        return x
    x = self.power(x,n//2)
    x = self.multiply(x,x)
    return x

However, your algorithm is flawed as 2^3 returns 4 rather than 8.

1

First, I would note that x ^ 0 = 1, but your code states that it should equal zero within your first if statement within myPow. Second, your big problem is that you are not storing any of your intermediate results. Within the power function, you have:

def power(self,x,n):
  if n == 1:
    return x
  self.power(x,n//2)
  self.multiply(x,x)
  return x

This function is taking in x and n, then computing subproblems using these variables, and then returning the original x. Therefore, in your example, calling test3.power(x, y) will always return the original x value. Instead, do the following.

def power(self,x,n):
  if n == 1:
    return x

  # Account for when n is not even.
  n1 = n // 2
  n2 = n - n1

  # Calculate powers.
  x1 = self.power(x, n1)
  x2 = self.power(x, n2) 

  # Combine divide-and-conquer answers and return.
  x = self.multiply(x,x)
  return x

Also notice that I changed the function to break the problem into powers of n1 and n2. This is because your function would not correctly handle something like power(2, 3). In this case, the original function would compute power(2, 3 // 2) = power(2, 1) which is not what you want. I hope this helps.

Matthew Miller
  • 555
  • 3
  • 15