-
-
Notifications
You must be signed in to change notification settings - Fork 709
Add solution for Challenge 1 by Dhar01 #737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughA new Go solution file is introduced for Challenge 1 that implements a Sum function accepting two integers and returning their sum if both inputs fall within the [-1e9, 1e9] range, otherwise returning 0. A main function reads two comma-separated integers from standard input and prints the result. Changes
Sequence Diagram(s)sequenceDiagram
participant Main as main()
participant Scanf as fmt.Scanf
participant Sum as Sum(a, b)
participant Math as math.Pow
Main->>Scanf: Read "%d, %d" from stdin
Scanf-->>Main: a, b, err
alt Input Read Error
Main->>Main: Handle error
else Input Valid
Main->>Sum: Call Sum(a, b)
Sum->>Math: Calculate bounds ±1e9
alt Both in [-1e9, 1e9]
Sum-->>Main: Return a + b
else Out of bounds
Sum-->>Main: Return 0
end
Main->>Main: fmt.Println(result)
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Possibly related PRs
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
| lowerLimit := math.Pow(-10, 9) | ||
| higherLimit := math.Pow(10, 9) | ||
|
|
||
| if int(lowerLimit) <= a && b <= int(higherLimit) { | ||
| return a + b | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Range enforcement is incomplete
Only a is checked against the lower bound and only b against the upper bound, so values like a = 1_000_000_001 or b = -1_000_000_001 still return a non-zero sum—breaking the spec that both operands must stay within [-1e9, 1e9]. Tighten the condition to cover both bounds for both operands.
-func Sum(a int, b int) int {
- // TODO: Implement the function
- lowerLimit := math.Pow(-10, 9)
- higherLimit := math.Pow(10, 9)
-
- if int(lowerLimit) <= a && b <= int(higherLimit) {
- return a + b
- }
-
- return 0
+func Sum(a int, b int) int {
+ const lowerLimit = -1_000_000_000
+ const upperLimit = 1_000_000_000
+
+ if a < lowerLimit || a > upperLimit || b < lowerLimit || b > upperLimit {
+ return 0
+ }
+
+ return a + b
}📝 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.
| lowerLimit := math.Pow(-10, 9) | |
| higherLimit := math.Pow(10, 9) | |
| if int(lowerLimit) <= a && b <= int(higherLimit) { | |
| return a + b | |
| } | |
| func Sum(a int, b int) int { | |
| const lowerLimit = -1_000_000_000 | |
| const upperLimit = 1_000_000_000 | |
| if a < lowerLimit || a > upperLimit || b < lowerLimit || b > upperLimit { | |
| return 0 | |
| } | |
| return a + b | |
| } |
🤖 Prompt for AI Agents
In challenge-1/submissions/Dhar01/solution-template.go around lines 25 to 30,
the range check only validates the lower bound for `a` and the upper bound for
`b`, allowing out-of-range values for the other sides; change the condition to
ensure both `a` and `b` are each >= -1e9 and <= 1e9 (i.e., check lower and upper
bounds for both operands) and return the sum only when both pass, otherwise
handle the out-of-range case as intended.
Challenge 1 Solution
Submitted by: @Dhar01
Challenge: Challenge 1
Description
This PR contains my solution for Challenge 1.
Changes
challenge-1/submissions/Dhar01/solution-template.goTesting
Thank you for reviewing my submission! 🚀