Figuring out whether force b

I am currently using singpath.com to practice on my python, but I have a problem with the problem:

A number, a, is a power of b if it is divisible by b and a / b is a power of b. Write an is_power function that takes a and b and returns True if a is a power of b.

def is_power(a,b): c = a/b if (((a%b) == 0) and ((c%b) == 0)): return True else: return False 

Above is my solution, but the system will offer me to generalize my solution. Can someone tell me what is wrong with my decision?

+6
python
source share
15 answers

The reason your source code doesn't work is this: you just check (c%b) == 0) aka (a/b) is divisible by b , which is much weaker than part of the definition of a/b is a power of b .

If you want to solve such a problem, you should always start with trivial cases. In this case, there are two such cases: is_power(x,x) and is_power(1,x) - in the answer True , because x**1==x and x**0==1 .

Once you cover these cases, you just need to write down the rest of the definition. Write the code for (a is divisible by b) and (a/b is a power of b) and put it all together.

The last function will look like this:

 def is_power(a,b): if <trivial case 1> or <trivial case 2>: return True # its a recursive definition so you have to use `is_power` here return <a is divisible by b> and <a/b is a power of b> 

It remains only to answer the question <a/b is a power of b> . The easiest way to do this is to use the is_power function is_power - this is called recursion.

+8
source share

You only check the first two powers: divides b and a / b divides b. It may be that a = b ** 3 or b ** 4 (or b ** n in general), so the actual solution would have to include a recursion or loop.

+1
source share

I would not say to generalize it. I would say to fix this, as this is not true. Using your solution, is_power (12.2) returns True as is_power (18.3).

I think the reason the system says that it generalizes it is because it probably works correctly for some of their test cases, but not for others. It is likely that the test cases for which it works coincide with those for which it would work if it were hardcoded in a certain way (for example, only a check of degree 2).

0
source share

You check if a/b is divisible by b (in the expression (c%b) == 0 ), and not by a/b - the degree of b . Hint:. What function would you call to see if something is a power of b ?

0
source share

To understand recursion, you need to understand recursion first.

  def is_power(a, b): if a < b: # 3 is never a power of 10, right? return False # prevent recursion if a == b: # a is always a**1, right? return True # prevent recursion else: return is_power(a / b, b) # recursion! 

Note that for a / b integers you get rounding errors. Make sure you carry the floats.

0
source share

I do not think you have the correct implementation. Based on this problem, the is_power function should look something like this:

 def is_power(a,b): if a%b == 0 and is_power(float(a)/float(b), b): return True else: return False 
0
source share

You can use the magazine.

 import math def is_power(a, b): return math.log(a, b) % 1 == 0 
0
source share
 def is_power(a,b): if a == b: return True if a % b == 0 and is_power(a/b,b): return True return False 

The final condition, which is == b, is crucial here, which stops when both numbers are equal. If this is not enabled, the function can show False for even legit numbers, dividing a / b at the next iteration, which gives 1 where 1% b = 1, which in turn returns False instead of True.

0
source share

You answer the first restriction, but not the second,
You verify that (a/b)%b == 0 is a special case of " (a/b) is a power of b ". To do this, a generalization error occurs (try to come up with a generalization of this particular case.

What you wrote is not a solution for is power of , for example, you specify 12 as power 2 , because:

  • 12%2 = 0 ,
  • (12/2)%2 = 0

But this is clearly wrong.

As others have said, think about recursion (or a less preferred loop solution).

0
source share

I myself worked on this issue, and this is what I came up with.

To write this as a recursive function, you need a recursive part and a trivial part. For the recursive part, a number is the number of another number if:

 ((a%b)==0) and (is_power(a/b, b) # a is evenly divisible by b and a/b is also a power of b. 

For the trivial case, b is a power of a if a=b .

However, we are not done. Since we divide by b , we must make an exception if b is zero .

And another exception is when b = 1 . Since when b=1 , a/b is a , we end infinite recursion.

So, all together:

 def is_power(a,b): # trivial case if (a==b): return True # Exception Handling if (b==0) or (b==1): return False # unless a==b==0 or a==b==1, which are handled by the trivial case above # recursive case return ((a%b)==0) and (is_power(a/b,b)) 
0
source share

This example should fix your problem:

 def is_power(a,b): if a == 1: return True elif a%b == 0 and is_power(a/b, b) == True: return True else: return False 
0
source share

Here is my solution.

 def is_power(a,b): if a == 1: # base case for recursion return True elif b == 0 or a%b !=0 or a<b: # exception cases. return False return is_power(a//b,b) # recursion 

I tested several cases (16,2), (6,2), (1,2), (0,0), (0,1) and it works well.

0
source share

A simpler solution:

 def is_power(a, b): while a % b == 0: a //= b return a == 1 

Recursion is really not needed for this problem. In addition, recursion can cause a recursion restriction error if a = b ** 1001.

0
source share
 def is_power(a,b): '''this program checks if number1 is a power of number2''' if (a<b): # lesser number isn't a power of a greater number return False elif (b==0) or (b==1) : # Exception cases return False elif a%b == 0 and is_power(a/b, b) == True: # Condition check for is_power (Recursion!) return True else: return False 
0
source share

Your solution is correct, however you just need to remove ALL brackets in your if statement.

def ab (a, b): c = b / a, if a% b == 0 and c% b == 0: return True, otherwise: return False

print (ab (32, 2))

-one
source share

All Articles