Skip to content

Add solution for Challenge 21 by WHFF521#1892

Merged
github-actions[bot] merged 1 commit into
RezaSi:mainfrom
WHFF521:challenge-21-WHFF521
Jul 16, 2026
Merged

Add solution for Challenge 21 by WHFF521#1892
github-actions[bot] merged 1 commit into
RezaSi:mainfrom
WHFF521:challenge-21-WHFF521

Conversation

@WHFF521

@WHFF521 WHFF521 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Challenge 21 Solution

Submitted by: @WHFF521
Challenge: Challenge 21

Description

This PR contains my solution for Challenge 21.

Changes

  • Added solution file to challenge-21/submissions/WHFF521/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds three exported Go binary-search utilities for sorted integer slices and a main function that demonstrates their returned indices.

Changes

Binary Search Utilities

Layer / File(s) Summary
Binary search implementations
challenge-21/submissions/WHFF521/solution-template.go
Adds iterative and recursive searches returning an index or -1, plus insertion-position calculation that preserves sorted order.
Example execution
challenge-21/submissions/WHFF521/solution-template.go
Runs each helper against an example sorted slice and prints the results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

A rabbit hops through sorted hay,
Finds each carrot on the way.
Recursive paws search left and right,
New carrots fit in order just right.
Three tools printed—what a delight! 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding a Challenge 21 solution submission.
Description check ✅ Passed The description is directly related to the submitted Challenge 21 solution and its added file.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
challenge-21/submissions/WHFF521/solution-template.go (3)

29-41: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Remove TODO and apply standard Go formatting.

The implementation is functionally correct. You can optionally clean up the lingering TODO comment and apply standard gofmt styling (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 value

Remove unreachable code and format.

The return -1 at the end of the function is unreachable because the preceding if/else if/else block covers all conditions and returns in every branch. This can be cleaned up alongside the lingering TODO comment 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 value

Remove redundant variable assignment and format.

The mid variable 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

📥 Commits

Reviewing files that changed from the base of the PR and between 8522a72 and 621915f.

📒 Files selected for processing (1)
  • challenge-21/submissions/WHFF521/solution-template.go

@github-actions
github-actions Bot merged commit 5d64ef2 into RezaSi:main Jul 16, 2026
6 checks passed
@github-actions

Copy link
Copy Markdown

🎉 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant