""" 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()
""" 1295.1 - Solution 1 - String Length Check """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findNumbers(self, nums: List[int]) -> int:
"""Find Numbers with Even Number of Digits Function"""
count = 0
for num in nums:
if len(str(num)) % 2 == 0:
count += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findNumbers([12, 345, 2, 6, 7896])) # Expected: 2
print(Solution().findNumbers([555, 901, 482, 1771])) # Expected: 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1295.2 - Solution 2 - Mathematical Digit Count """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findNumbers(self, nums: List[int]) -> int:
"""Find Numbers with Even Number of Digits Function"""
count = 0
for num in nums:
digits = 0
n = num
while n > 0:
digits += 1
n //= 10
if digits % 2 == 0:
count += 1
return count
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findNumbers([12, 345, 2, 6, 7896])) # Expected: 2
print(Solution().findNumbers([555, 901, 482, 1771])) # Expected: 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 1295.3 - Solution 3 - One-Liner with Sum and Comprehension """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findNumbers(self, nums: List[int]) -> int:
"""Find Numbers with Even Number of Digits Function"""
return sum(1 for num in nums if len(str(num)) % 2 == 0)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findNumbers([12, 345, 2, 6, 7896])) # Expected: 2
print(Solution().findNumbers([555, 901, 482, 1771])) # Expected: 1
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
1295 - Find Numbers with Even Number of Digits