Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions challenge-21/submissions/Johrespi/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
)

func main() {
// Example sorted array for testing
arr := []int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

// Test binary search
target := 7
index := BinarySearch(arr, target)
fmt.Printf("BinarySearch: %d found at index %d\n", target, index)

// Test recursive binary search
recursiveIndex := BinarySearchRecursive(arr, target, 0, len(arr)-1)
fmt.Printf("BinarySearchRecursive: %d found at index %d\n", target, recursiveIndex)

// Test find insert position
insertTarget := 8
insertPos := FindInsertPosition(arr, insertTarget)
fmt.Printf("FindInsertPosition: %d should be inserted at index %d\n", insertTarget, insertPos)
}

// BinarySearch performs a standard binary search to find the target in the sorted array.
// Returns the index of the target if found, or -1 if not found.
func BinarySearch(arr []int, target int) int {
// TODO: Implement this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove TODO comment.

The TODO comment should be removed as the function is now implemented.

Apply this diff:

-	// TODO: Implement this function
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// TODO: Implement this function
🤖 Prompt for AI Agents
In challenge-21/submissions/Johrespi/solution-template.go around line 29, remove
the remaining "// TODO: Implement this function" comment now that the function
is implemented; delete that line and ensure surrounding code formatting/newlines
remain correct so the file compiles.

low := 0
high := len(arr) - 1

for low <= high {
mid := low + (high - low)/2
if target == arr[mid]{
return mid
} else if target > arr[mid]{
low = mid + 1
} else {
high = mid - 1
}
}

return -1
}

// BinarySearchRecursive performs binary search using recursion.
// Returns the index of the target if found, or -1 if not found.
func BinarySearchRecursive(arr []int, target int, left int, right int) int {
// TODO: Implement this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove TODO comment.

The TODO comment should be removed as the function is now implemented.

Apply this diff:

-	// TODO: Implement this function
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// TODO: Implement this function
🤖 Prompt for AI Agents
In challenge-21/submissions/Johrespi/solution-template.go around line 50, remove
the "// TODO: Implement this function" comment because the function is
implemented; simply delete that single-line TODO comment so the file contains
only the implemented function and no leftover placeholder comment.


mid := left + (right - left)/2

if left > right{
return -1

}

if target == arr[mid]{
return mid
} else if target > arr[mid]{
return BinarySearchRecursive(arr, target, mid + 1, right)
} else {
return BinarySearchRecursive(arr, target, left, mid - 1)
}
}

// FindInsertPosition returns the index where the target should be inserted
// to maintain the sorted order of the array.
func FindInsertPosition(arr []int, target int) int {
// TODO: Implement this function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove TODO comment.

The TODO comment should be removed as the function is now implemented.

Apply this diff:

-	// TODO: Implement this function
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// TODO: Implement this function
🤖 Prompt for AI Agents
In challenge-21/submissions/Johrespi/solution-template.go around line 71, remove
the leftover TODO comment ("// TODO: Implement this function") since the
function is already implemented; delete that comment line and ensure surrounding
formatting remains correct (no extra blank lines introduced).


left := 0
right := len(arr) - 1

for left <= right {
mid := left + (right - left)/2
if target > arr[mid] {
left = mid + 1
} else {
right = mid - 1
}
}

return left
}
Loading