Skip to content

Instantly share code, notes, and snippets.

@ayubmetah
Created December 19, 2020 23:24
Show Gist options
  • Save ayubmetah/1e60e9b79fd1c1e599dc56616fcfa20b to your computer and use it in GitHub Desktop.
Save ayubmetah/1e60e9b79fd1c1e599dc56616fcfa20b to your computer and use it in GitHub Desktop.
Fill in the blanks to make the is_power_of function return whether the number is a power of the given base. Note: base is assumed to be a positive number. Tip: for functions that return a boolean value, you can return the result of a comparison.
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
Copy link
Author

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

@Moyosore3674
Copy link

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

@Vjbhaka
Copy link

Vjbhaka commented Jul 9, 2022

can anyone explain why we are doing number = number/base in line 3.

@dnyanesh-github
Copy link

dnyanesh-github commented Jul 31, 2022

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

@dnyanesh-github
Copy link

dnyanesh-github commented Jul 31, 2022

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
    

@Manash-git
Copy link

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

@Vedantroy475
Copy link

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

@iseiaki
Copy link

iseiaki commented Oct 26, 2022

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

@Fumes11
Copy link

Fumes11 commented Dec 2, 2022

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

@saidawangui
Copy link

saidawangui commented Dec 14, 2022

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

@saidawangui
Copy link

if you get the below error

Error on line 6: else : ^ SyntaxError: invalid syntax

ensure indention of if and else are similar. lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment