""" 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()
""" 0482.1 - Solution 1 - Remove Dashes, Uppercase, and Reverse Grouping """
#####################################################################################
# Imports
#####################################################################################
# No additional imports needed
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def licenseKeyFormatting(self, s: str, k: int) -> str:
"""License Key Formatting Function"""
# Remove all dashes and convert to uppercase
s = s.replace("-", "").upper()
# Calculate the size of the first group
first_group = len(s) % k
# Build result
parts = []
if first_group:
parts.append(s[:first_group])
for i in range(first_group, len(s), k):
parts.append(s[i:i + k])
return "-".join(parts)
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().licenseKeyFormatting("5F3Z-2e-9-w", 4)) # "5F3Z-2E9W"
print(Solution().licenseKeyFormatting("2-5g-3-J", 2)) # "2-5G-3J"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0482.2 - Solution 2 - Build from Reversed String """
#####################################################################################
# Imports
#####################################################################################
# No additional imports needed
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def licenseKeyFormatting(self, s: str, k: int) -> str:
"""License Key Formatting Function"""
# Remove dashes and uppercase
cleaned = s.replace("-", "").upper()
# Build groups from the end
result = []
count = 0
for ch in reversed(cleaned):
result.append(ch)
count += 1
if count % k == 0:
result.append("-")
# Remove trailing dash if present
if result and result[-1] == "-":
result.pop()
return "".join(reversed(result))
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().licenseKeyFormatting("5F3Z-2e-9-w", 4)) # "5F3Z-2E9W"
print(Solution().licenseKeyFormatting("2-5g-3-J", 2)) # "2-5G-3J"
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()