Add solution for Challenge 21 by WHFF521#1892
Conversation
WalkthroughAdds three exported Go binary-search utilities for sorted integer slices and a ChangesBinary Search Utilities
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
challenge-21/submissions/WHFF521/solution-template.go (3)
29-41: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRemove TODO and apply standard Go formatting.
The implementation is functionally correct. You can optionally clean up the lingering
TODOcomment and apply standardgofmtstyling (such as adding spaces around operators) for better readability.♻️ Proposed refactor
- // TODO: Implement this function left := 0 - right := len(arr)-1 - for left<=right{ - mid := left + (right - left)/2 - if arr[mid] == target { - return mid - }else if arr[mid] < target{ - left = mid+1 - }else { - right = mid-1 - } - } + right := len(arr) - 1 + for left <= right { + mid := left + (right-left)/2 + if arr[mid] == target { + return mid + } else if arr[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + }
48-60: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRemove unreachable code and format.
The
return -1at the end of the function is unreachable because the precedingif/else if/elseblock covers all conditions and returns in every branch. This can be cleaned up alongside the lingeringTODOcomment and standard formatting.♻️ Proposed refactor
- // TODO: Implement this function - if left > right{ - return -1 - } - mid := left + (right - left)/2 - if arr[mid] == target{ - return mid - }else if arr[mid] < target{ - return BinarySearchRecursive(arr,target,mid+1,right) - }else { - return BinarySearchRecursive(arr,target,left,mid-1) - } - return -1 + if left > right { + return -1 + } + mid := left + (right-left)/2 + if arr[mid] == target { + return mid + } else if arr[mid] < target { + return BinarySearchRecursive(arr, target, mid+1, right) + } + return BinarySearchRecursive(arr, target, left, mid-1)
66-79: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRemove redundant variable assignment and format.
The
midvariable is initialized outside the loop but is immediately overwritten on the first iteration before being used. The initial calculation can be safely removed.♻️ Proposed refactor
- // TODO: Implement this function left := 0 - right := len(arr)-1 - mid := left + (right - left)/2 - for left<=right{ - mid = left + (right - left)/2 - if arr[mid] == target { - return mid - }else if arr[mid] < target{ - left = mid+1 - }else { - right = mid-1 - } - } + right := len(arr) - 1 + for left <= right { + mid := left + (right-left)/2 + if arr[mid] == target { + return mid + } else if arr[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + }
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 8b209eed-d6c7-4828-a24d-04588e496f1c
📒 Files selected for processing (1)
challenge-21/submissions/WHFF521/solution-template.go
|
🎉 Auto-merged! This PR was automatically merged after 2 days with all checks passing. Thank you for your contribution, @WHFF521! 📊 Scoreboards and badges will be updated shortly. |
Challenge 21 Solution
Submitted by: @WHFF521
Challenge: Challenge 21
Description
This PR contains my solution for Challenge 21.
Changes
challenge-21/submissions/WHFF521/solution-template.goTesting
Thank you for reviewing my submission! 🚀