""" 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()
""" 0507.1 - Perfect Number - Solution 1 - Iterate Up to Square Root """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def checkPerfectNumber(self, num: int) -> bool:
"""Check Perfect Number Function"""
if num <= 1:
return False
total = 1
i = 2
while i * i <= num:
if num % i == 0:
total += i
if i != num // i:
total += num // i
i += 1
return total == num
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().checkPerfectNumber(28)) # Expected: True
print(Solution().checkPerfectNumber(7)) # Expected: False
print(Solution().checkPerfectNumber(6)) # Expected: True
print(Solution().checkPerfectNumber(496)) # Expected: True
print(Solution().checkPerfectNumber(1)) # Expected: False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0507.2 - Perfect Number - Solution 2 - Brute Force (Check All Divisors) """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def checkPerfectNumber(self, num: int) -> bool:
"""Check Perfect Number Function"""
if num <= 1:
return False
total = 0
for i in range(1, num):
if num % i == 0:
total += i
return total == num
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().checkPerfectNumber(28)) # Expected: True
print(Solution().checkPerfectNumber(7)) # Expected: False
print(Solution().checkPerfectNumber(6)) # Expected: True
print(Solution().checkPerfectNumber(496)) # Expected: True
print(Solution().checkPerfectNumber(1)) # Expected: False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()