Palindrome initial import
[?]
Nov 29, 2020, 7:15 AM
MCHVA5DYOAOEBSHENAB4FYJBVE4A47B7GDDHOEM36BXTN63ZMQNACDependencies
Change contents
- file addition: snippets[1.0]
- file addition: palindrome[0.11]
- file addition: palindrome.go[0.25]
// Palindrome is a string that is the same forward and backwardpackage mainimport ("bufio""fmt""os")func main() {scanner := bufio.NewScanner(os.Stdin)for scanner.Scan() {var str stringif len(scanner.Text()) > 2 {str = palindrome(scanner.Text())}if len(str) > 2 {fmt.Println(str)}}if scanner.Err() != nil {fmt.Fprintln(os.Stderr, "Error reading console input")}}// Returns the longest palindrome in strfunc palindrome(str string) string {longest := ""jobs := 0results := make(chan string)for k, _ := range str {if k > 0 {go pal_worker(str, k, results)jobs++}}for {select {case candidate := <-results:jobs--if len(candidate) > len(longest) {longest = candidate}if jobs == 0 {close(results)return longest}}}}func pal_worker(str string, k int, result chan<- string) {longest := string(str[k])for i := 1; true; i++ {// avoid overrun or underrunif k-i < 0 || k+i > len(str)-1 {break}// two identical bytesif str[k-i] == str[k+i] {longest = string(str[k-i]) + longest + string(str[k+i])} else {break}}//fmt.Fprintf(os.Stderr, "Worker(%d) result: %s\n", k, longest)result <- longest} - file addition: TODO.md[0.25]
# Couple of ideas- make a note of these just to order them in my head- write the basic version with no channels- test channel closing methods: no more load, no more work, know number ofworkers, unknown number of workers and loaders, channel buffer size basedchannel closing?- create separate channel for loader and worker- measure (profile) loader and worker- make suggestions: more loader or more worker is needed- write tests for the functions