""" 0001.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0001.1 - Solution 1 - Using Enumerate """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for key1, num1 in enumerate(nums):
for key2, num2 in enumerate(nums[key1 + 1 :], key1 + 1):
# enumerate(iterable, start=1) -> start value determines starting index
# Printing value and key will output 1 i , 2 j, 3 k, 4 l
if num1 + num2 == target:
return [key1, key2]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1342.1 - Solution 1 - Iterative (Simulation) """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def numberOfSteps(self, num: int) -> int:
"""Number of Steps to Reduce a Number to Zero Function"""
steps = 0
while num > 0:
if num % 2 == 0:
num //= 2
else:
num -= 1
steps += 1
return steps
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().numberOfSteps(14)) # Expected: 6
print(Solution().numberOfSteps(8)) # Expected: 4
print(Solution().numberOfSteps(123)) # Expected: 12
print(Solution().numberOfSteps(0)) # Expected: 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1342.2 - Solution 2 - Bit Manipulation """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def numberOfSteps(self, num: int) -> int:
"""Number of Steps to Reduce a Number to Zero Function"""
if num == 0:
return 0
steps = 0
while num > 0:
# If last bit is 1 (odd), subtract 1; if 0 (even), divide by 2
steps += 1 + (num & 1) # +1 for divide, +1 extra if odd (subtract)
num >>= 1
# We counted one extra divide for the final bit shift to 0
return steps - 1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().numberOfSteps(14)) # Expected: 6
print(Solution().numberOfSteps(8)) # Expected: 4
print(Solution().numberOfSteps(123)) # Expected: 12
print(Solution().numberOfSteps(0)) # Expected: 0
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()