""" 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()
""" 0405.1 - Convert a Number to Hexadecimal - Solution 1 - Bit Manipulation """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def toHex(self, num: int) -> str:
"""Convert integer to hexadecimal string"""
if num == 0:
return "0"
hex_chars = "0123456789abcdef"
# Mask to 32 bits to handle negative numbers (two's complement)
num &= 0xFFFFFFFF
result = []
while num:
result.append(hex_chars[num & 0xF])
num >>= 4
return "".join(reversed(result))
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().toHex(26)) # Expected: "1a"
print(Solution().toHex(-1)) # Expected: "ffffffff"
print(Solution().toHex(0)) # Expected: "0"
print(Solution().toHex(16)) # Expected: "10"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0405.2 - Convert a Number to Hexadecimal - Solution 2 - Division and Modulo """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def toHex(self, num: int) -> str:
"""Convert integer to hexadecimal string"""
if num == 0:
return "0"
hex_chars = "0123456789abcdef"
# Mask to 32 bits for two's complement
num &= 0xFFFFFFFF
result = []
while num > 0:
result.append(hex_chars[num % 16])
num //= 16
return "".join(reversed(result))
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().toHex(26)) # Expected: "1a"
print(Solution().toHex(-1)) # Expected: "ffffffff"
print(Solution().toHex(0)) # Expected: "0"
print(Solution().toHex(255)) # Expected: "ff"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()