-
-
Save ayubmetah/1e60e9b79fd1c1e599dc56616fcfa20b to your computer and use it in GitHub Desktop.
def is_power_of(number, base): | |
# Base case: when number is smaller than base. | |
number = number/base | |
if number < base: | |
# If number is equal to 1, it's a power (base**0). | |
return False | |
else: | |
return True | |
# Recursive case: keep dividing number by base. | |
return is_power_of(number, base) | |
print(is_power_of(8,2)) # Should be True | |
print(is_power_of(64,4)) # Should be True | |
print(is_power_of(70,10)) # Should be False |
ayubmetah
commented
Dec 19, 2020
def is_power_of(number, base):
if number % base == 0:
number /= base
Base case: when number is smaller than base.
if number < base:
# If number is equal to 1, it's a power (base**0).
return False
else:
return True
Recursive case: keep dividing number by base.
return is_power_of(number, base)
print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False
can anyone explain why we are doing number = number/base in line 3.
def is_power_of(number, base): # Base case: when number is smaller than base. number = number/base if number < base: # If number is equal to 1, it's a power (base**0). return False else: return True return is_power_of(number, base) print(is_power_of(8,2)) # Should be True print(is_power_of(64,4)) # Should be True print(is_power_of(70,10)) # Should be False
I know it's a pretty old post. However, the code above is not right. It will return "True" even when a number is divisible by the base and not only when it's a "power of" the base. e.g. (18,3) is an input, 18 is divisible by 3, however 18 is not a power of 3. Still this code will return True.
And in recursion, you don't do the recursive operations without using the function. The whole purpose of recursion is lost with that.
def is_power_of(number, base):
# Base case: when number is smaller than base.
if number < base:
# If number is equal to 1, it's a power (base**0).
return number == 1
# Recursive case: keep dividing number by base.
return is_power_of(number/base, base)
print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False
can anyone explain why we are doing number = number/base in line 3.
It was wrong to do that!!
This is called as recursion in the programming, and you can say a code is using recursion(read more about it here: https://www.w3schools.com/python/gloss_python_function_recursion.asp), when the parent function is being called within the same function. It runs in loop until the base condition is met. Like the way "is_power_of" function which is also a parent function is being called within the same function.
Let's take the first example from the above code:(8,2)
if 8 < 2: ## Condition results in False. This also is the base condition, which is used to avoid infinite loop.
## Although, recursion can only go as deep as 1000 iterations
<Code block is not executed as condition failed>
# Parent function is called now which is converted into
print(is_power_of(4,2) ## 8/2 = 4
<The function with arguments is_power_of(4,2) is triggered now, which once again goes through the loop, until the number becomes less than the base>
Now the number which eventually becomes 1 due to recurring number/base operations is less than the base(2 in this case), we run a check if it's equal to 1 or not. If it's 1, that means it's a power of base(2 in this case) and thus returns True.
For (70,10) - 70/10 = 7
7 < 10
return 7 == 1: ## Returns False
This should be right answer
def is_power_of(number, base):
Base case: when number is smaller than base.
if number < base:
# If number is equal to 1, it's a power (base**0).
if number==1:
return True
else:
return False
Recursive case: keep dividing number by base.
return is_power_of(number//base, base)
print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False
def is_power_of(number,base):
if number%base==0:
number=number/base
if number==base:
return True
else:
return is_power_of(number,base)
else:
return False
print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False
print(is_power_of(18,3)) # Should be False
def is_power_of(number, base):
Base case: when number is smaller than base.
if number < base:
# If number is equal to 1, it's a power (base**0).
return number == 1
Recursive case: keep dividing number by base.
return is_power_of(number//base, base)
print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False
can anyone explain why we are doing number = number/base in line 3.
for example:
8/2 = 4
4/2 = 2
2/2 = 1
this shows us that that 8 can be divided by the 2 exactly with no remainders, and how many times becomes the power.
In this case, 2^3
use
def is_power_of(number, base):
if number < base:
return number == 1
return is_power_of(number//base, base)
print(is_power_of(8,2)) # Should be True
print(is_power_of(64,4)) # Should be True
print(is_power_of(70,10)) # Should be False
if you get the below error
Error on line 6: else : ^ SyntaxError: invalid syntax
ensure indention of if and else are similar. lol