-
-
Notifications
You must be signed in to change notification settings - Fork 716
Add solution for Challenge 17 by hudazaan #587
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
WalkthroughAdds a Go palindrome checker program. Introduces exported function IsPalindrome(s string) bool that normalizes input (remove non-alphanumerics, lowercase) and uses two-pointer comparison. Includes a main function to read input, call IsPalindrome, and print the result. Uses only standard library packages. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant Main as main()
participant Pal as IsPalindrome(s)
User->>Main: Provide input string
Main->>Pal: IsPalindrome(input)
Note right of Pal: Normalize: remove non-alphanumerics, to lower
Pal->>Pal: Two-pointer compare
Pal-->>Main: bool (true/false)
Main-->>User: Print result
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 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
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
challenge-17/submissions/hudazaan/solution-template.go(1 hunks)
🔇 Additional comments (1)
challenge-17/submissions/hudazaan/solution-template.go (1)
24-50: LGTM! Clean and efficient implementation.The palindrome logic is correct and follows Go best practices:
- Efficient use of
strings.Builderfor string construction- Proper Unicode handling with
unicode.IsLetterandunicode.IsDigit- Optimal two-pointer approach for O(n) comparison
- Clear comments and readable code
| func main() { | ||
| // Get input from the user | ||
| var input string | ||
| fmt.Print("Enter a string to check if it's a palindrome: ") | ||
| fmt.Scanln(&input) | ||
|
|
||
| // Call the IsPalindrome function and print the result | ||
| result := IsPalindrome(input) | ||
| if result { | ||
| fmt.Println("The string is a palindrome.") | ||
| } else { | ||
| fmt.Println("The string is not a palindrome.") | ||
| } | ||
| } |
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.
Use bufio.Scanner to read multi-word input.
fmt.Scanln stops reading at the first whitespace, which breaks palindrome checking for common test cases like "race car" or "A man a plan a canal Panama". Palindrome checkers typically need to handle phrases with spaces.
Apply this diff to fix the input reading:
import (
+ "bufio"
"fmt"
+ "os"
"strings"
"unicode"
)
func main() {
// Get input from the user
- var input string
fmt.Print("Enter a string to check if it's a palindrome: ")
- fmt.Scanln(&input)
+ scanner := bufio.NewScanner(os.Stdin)
+ scanner.Scan()
+ input := scanner.Text()
// Call the IsPalindrome function and print the result📝 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.
| func main() { | |
| // Get input from the user | |
| var input string | |
| fmt.Print("Enter a string to check if it's a palindrome: ") | |
| fmt.Scanln(&input) | |
| // Call the IsPalindrome function and print the result | |
| result := IsPalindrome(input) | |
| if result { | |
| fmt.Println("The string is a palindrome.") | |
| } else { | |
| fmt.Println("The string is not a palindrome.") | |
| } | |
| } | |
| import ( | |
| "bufio" | |
| "fmt" | |
| "os" | |
| "strings" | |
| "unicode" | |
| ) | |
| func main() { | |
| // Get input from the user | |
| fmt.Print("Enter a string to check if it's a palindrome: ") | |
| scanner := bufio.NewScanner(os.Stdin) | |
| scanner.Scan() | |
| input := scanner.Text() | |
| // Call the IsPalindrome function and print the result | |
| result := IsPalindrome(input) | |
| if result { | |
| fmt.Println("The string is a palindrome.") | |
| } else { | |
| fmt.Println("The string is not a palindrome.") | |
| } | |
| } |
🤖 Prompt for AI Agents
In challenge-17/submissions/hudazaan/solution-template.go around lines 9 to 22,
replace the fmt.Scanln input reading with a bufio.Scanner (or bufio.NewReader)
to capture the full line including spaces: create a scanner for os.Stdin, call
scanner.Scan(), assign input = scanner.Text(), and handle scanner.Err() if
present. Also ensure bufio and os are imported (add them to the import block if
missing). This will allow multi-word phrases like "race car" to be read
correctly for palindrome checking.
Challenge 17 Solution
Submitted by: @hudazaan
Challenge: Challenge 17
Description:
This PR contains my solution file in challenge-17/submissions/hudazaan/solution-template.go for challenge 17.
Testing
Thank you for reviewing my PR. Looking forward to more meaningful contributions!! 🚀