""" 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()
""" 1290.1 - Solution 1 - Bit Shifting (Iterative) """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val: int = 0, next: Optional['ListNode'] = None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def getDecimalValue(self, head: ListNode) -> int:
"""Get Decimal Value Function"""
result = 0
current = head
while current:
result = result * 2 + current.val
current = current.next
return result
#####################################################################################
# Functions
#####################################################################################
def build_list(values):
"""Helper to build linked list from list of values"""
dummy = ListNode(0)
current = dummy
for v in values:
current.next = ListNode(v)
current = current.next
return dummy.next
def testcase():
"""Test Function"""
print(Solution().getDecimalValue(build_list([1, 0, 1]))) # 5
print(Solution().getDecimalValue(build_list([0]))) # 0
print(Solution().getDecimalValue(build_list([1, 1, 0, 1]))) # 13
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1290.2 - Solution 2 - Bitwise OR with Left Shift """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val: int = 0, next: Optional['ListNode'] = None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def getDecimalValue(self, head: ListNode) -> int:
"""Get Decimal Value Function"""
result = 0
current = head
while current:
result = (result << 1) | current.val
current = current.next
return result
#####################################################################################
# Functions
#####################################################################################
def build_list(values):
"""Helper to build linked list from list of values"""
dummy = ListNode(0)
current = dummy
for v in values:
current.next = ListNode(v)
current = current.next
return dummy.next
def testcase():
"""Test Function"""
print(Solution().getDecimalValue(build_list([1, 0, 1]))) # 5
print(Solution().getDecimalValue(build_list([0]))) # 0
print(Solution().getDecimalValue(build_list([1, 1, 0, 1]))) # 13
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1290.3 - Solution 3 - String Conversion """
#####################################################################################
# Imports
#####################################################################################
from typing import Optional
#####################################################################################
# Classes
#####################################################################################
class ListNode:
"""ListNode Class"""
def __init__(self, val: int = 0, next: Optional['ListNode'] = None):
self.val = val
self.next = next
class Solution:
"""Solution Class"""
def getDecimalValue(self, head: ListNode) -> int:
"""Get Decimal Value Function"""
binary_str = ""
current = head
while current:
binary_str += str(current.val)
current = current.next
return int(binary_str, 2)
#####################################################################################
# Functions
#####################################################################################
def build_list(values):
"""Helper to build linked list from list of values"""
dummy = ListNode(0)
current = dummy
for v in values:
current.next = ListNode(v)
current = current.next
return dummy.next
def testcase():
"""Test Function"""
print(Solution().getDecimalValue(build_list([1, 0, 1]))) # 5
print(Solution().getDecimalValue(build_list([0]))) # 0
print(Solution().getDecimalValue(build_list([1, 1, 0, 1]))) # 13
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()