""" 0231.1 - Solution 1 - Iterative Division """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPowerOfTwo(self, n: int) -> bool:
"""Power of Two Function"""
if n <= 0:
return False
while n % 2 == 0:
n //= 2
return n == 1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPowerOfTwo(1)) # True (2^0)
print(Solution().isPowerOfTwo(16)) # True (2^4)
print(Solution().isPowerOfTwo(3)) # False
print(Solution().isPowerOfTwo(0)) # False
print(Solution().isPowerOfTwo(-4)) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0231.2 - Solution 2 - Bit Manipulation """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPowerOfTwo(self, n: int) -> bool:
"""Power of Two Function"""
return n > 0 and (n & (n - 1)) == 0
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPowerOfTwo(1)) # True (2^0)
print(Solution().isPowerOfTwo(16)) # True (2^4)
print(Solution().isPowerOfTwo(3)) # False
print(Solution().isPowerOfTwo(0)) # False
print(Solution().isPowerOfTwo(-4)) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()