65 views
Напишите функцию на Go, которая проверяет, является ли заданная строка палиндромом. Функция должна игнорировать регистр и пробелы. Кроме того, специальные символы также должны игнорироваться. Например, строка "A man, a plan, a canal: Panama" должна считаться палиндромом.
Asked at
AI answer
You can keep only letters and digits, convert them to lowercase, and compare the resulting sequence with its reverse. unicode allows you to correctly handle more than just ASCII. In Go, range over a string and []rune work with Unicode code points.
func IsPalindrome(s string) bool {
runes := make([]rune, 0, len(s))
for _, r := range s {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
runes = append(runes, unicode.ToLower(r))
}
}
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
if runes[i] != runes[j] {
return false
}
}
return true
}
Add the unicode import. In this implementation, spaces, punctuation, and other characters are ignored; an empty string is considered a palindrome.
Also asked at Астон
- If you are offered a job and accept it, how soon would you be ready to start working?
- What is your level of English, including conversational?
- In which direction would you like to develop further: architecture, management, or other?
- Are you actively looking for a job now?
- How do you assess your level as a Go developer: junior, middle, or senior? Have you conducted technical interviews or mentored employees?
Similar questions
- // isPalindrome checks if a string is a palindrome. // Only letters and digits are considered (alphanumeric characters), case-insensitive comparison. // O(1) in memory + O(n) in time // s := "racecar" func isPalindrome(s string) bool { }
- Implement the Go function processParallel(in <-chan int, out chan<- int, numWorkers int) and pass a context (context.Context) to it.
- Implement a Go function (worker pool) that accepts a cancellation context, an input task channel, and a number of workers; executes no more than N tasks concurrently; returns results in the order the tasks arrived; stops on the first error or context cancellation; and shuts down the workers correctly when the input channel is closed.
- We need to implement concurrent document searches across servers. There is a function `search(ctx, server, query) ([]string, error)`. There are N identical replica servers (up to 10). We need to call the function concurrently for all servers and return the first successful response without waiting for the others. If a server returns an error, we ignore it. If all of them return errors, return an error.
- How does Go code review work on your team, and what are the most common reasons you reject PRs?