Palindrome initial import

[?]
Nov 29, 2020, 7:15 AM
MCHVA5DYOAOEBSHENAB4FYJBVE4A47B7GDDHOEM36BXTN63ZMQNAC

Dependencies

Change contents

  • file addition: snippets (dxwrxwrx-r)
    [1.0]
  • file addition: palindrome (dxwrxwrx-r)
    [0.11]
  • file addition: palindrome.go (-xw-x--x--)
    [0.25]
    // Palindrome is a string that is the same forward and backward
    package main
    import (
    "bufio"
    "fmt"
    "os"
    )
    func main() {
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
    var str string
    if 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 str
    func palindrome(str string) string {
    longest := ""
    jobs := 0
    results := 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 underrun
    if k-i < 0 || k+i > len(str)-1 {
    break
    }
    // two identical bytes
    if 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 (-xw-x--x--)
    [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 of
    workers, unknown number of workers and loaders, channel buffer size based
    channel 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