""" 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()
""" 0506.1 - Relative Ranks - Solution 1 - Sorting with Index Mapping """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findRelativeRanks(self, score: List[int]) -> List[str]:
"""Find Relative Ranks Function"""
n = len(score)
# Create list of (score, original_index) and sort descending by score
sorted_scores = sorted(enumerate(score), key=lambda x: x[1], reverse=True)
medals = ["Gold Medal", "Silver Medal", "Bronze Medal"]
result = [""] * n
for rank, (original_index, _) in enumerate(sorted_scores):
if rank < 3:
result[original_index] = medals[rank]
else:
result[original_index] = str(rank + 1)
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findRelativeRanks([5, 4, 3, 2, 1]))
# ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
print(Solution().findRelativeRanks([10, 3, 8, 9, 4]))
# ["Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"]
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0506.2 - Relative Ranks - Solution 2 - Dictionary Mapping """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def findRelativeRanks(self, score: List[int]) -> List[str]:
"""Find Relative Ranks Function"""
sorted_scores = sorted(score, reverse=True)
medals = ["Gold Medal", "Silver Medal", "Bronze Medal"]
rank_map = {}
for i, s in enumerate(sorted_scores):
if i < 3:
rank_map[s] = medals[i]
else:
rank_map[s] = str(i + 1)
return [rank_map[s] for s in score]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().findRelativeRanks([5, 4, 3, 2, 1]))
# ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
print(Solution().findRelativeRanks([10, 3, 8, 9, 4]))
# ["Gold Medal", "5", "Bronze Medal", "Silver Medal", "4"]
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()