""" 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()
""" 0374.1 - Solution 1 - Binary Search """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# API (provided by LeetCode)
#####################################################################################
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
# 1 if num is lower than the picked number
# otherwise return 0
# def guess(num: int) -> int:
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def guessNumber(self, n: int) -> int:
"""Guess Number Higher or Lower Function"""
low = 1
high = n
while low <= high:
mid = (low + high) // 2
result = guess(mid)
if result == 0:
return mid
elif result == -1:
high = mid - 1
else:
low = mid + 1
return -1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
# Simulating the guess API for local testing
pick = 6
global guess
def guess(num: int) -> int:
if num > pick:
return -1
elif num < pick:
return 1
else:
return 0
print(Solution().guessNumber(10)) # 6
pick = 1
print(Solution().guessNumber(1)) # 1
pick = 2
print(Solution().guessNumber(2)) # 2
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()