Welcome to our Monthly Code Challenge! Each month, we present a new coding challenge designed to test your skills and expand your knowledge. This month's challenge focuses on algorithmic problem-solving and is suitable for beginners to advanced programmers.
Challenge Overview
- Difficulty Level: Intermediate
- Theme: Sorting and Searching Algorithms
- Description: Implement a function to sort a list of integers and then find the index of the given target number within the sorted list.
Objective
Your task is to create a program that:
- Accepts a list of integers as input.
- Sorts the list in ascending order using your chosen sorting algorithm.
- Returns the index of the target number within the sorted list. If the target is not found, return -1.
Sample Input
[4, 2, 5, 1, 3], 5
Expected Output
3
Resources
- Sorting Algorithms - Learn more about different sorting algorithms and their complexities.
- Searching Algorithms - Understand various searching techniques and their applications.
Example Code
Here's a simple implementation in Python:
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
def sort_and_search(arr, target):
sorted_arr = sorted(arr)
return binary_search(sorted_arr, target)
# Example usage
input_list = [4, 2, 5, 1, 3]
target_value = 5
result = sort_and_search(input_list, target_value)
print(result) # Output: 3
Submission
Share your code in the comments below or via our GitHub repository. We look forward to seeing your solutions!
Happy coding! 🎉