diff options
| -rw-r--r-- | challenge-019/pokgopun/go/ch-1.go | 36 | ||||
| -rw-r--r-- | challenge-019/pokgopun/go/ch-2.go | 42 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-019/pokgopun/go/ch-1.go b/challenge-019/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..60f72d8951 --- /dev/null +++ b/challenge-019/pokgopun/go/ch-1.go @@ -0,0 +1,36 @@ +package main + +// Write a script to display months from the year 1900 to 2019 where you find 5 weekends i.e. 5 Friday, 5 Saturday and 5 Sunday. +// From further analysis, this is only the case for a 31-days month which 1st of the month is Friday +import ( + "fmt" + "time" +) + +func main() { + startY := 1900 + endY := 2019 + for y := startY; y <= endY; y++ { + fmt.Println(y, happyMonths(y)) + } +} +func happyMonths(y int) (hm []time.Month) { + for _, m := range monthsWith31Days() { + t := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC) + if t.Weekday() == time.Friday { + hm = append(hm, m) + } + } + return hm +} +func monthsWith31Days() [7]time.Month { + return [7]time.Month{ + time.January, + time.March, + time.May, + time.July, + time.August, + time.October, + time.December, + } +} diff --git a/challenge-019/pokgopun/go/ch-2.go b/challenge-019/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..8ca53a0018 --- /dev/null +++ b/challenge-019/pokgopun/go/ch-2.go @@ -0,0 +1,42 @@ +package main + +import ( + "bufio" + "log" + "os" + "strconv" + "strings" +) + +func main() { + var width int + if len(os.Args) > 1 { + n, err := strconv.ParseUint(os.Args[1], 10, 32) + if err != nil { + log.Fatal(err) + } + width = int(n) + } + // An artificial input source. + const input = `Write a script that can wrap the given paragraph at a specified column using the greedy algorithm.` + scanner := bufio.NewScanner(strings.NewReader(input)) + scanner.Split(bufio.ScanWords) + var ( + b []byte + lw int + ww int + ) + w := bufio.NewWriter(os.Stdout) + for scanner.Scan() { + b = scanner.Bytes() + ww = len(b) + if lw+ww > width { + w.WriteByte('\n') + lw = 0 + } + w.Write(append(b, ' ')) + lw += ww + 1 + } + w.WriteByte('\n') + w.Flush() +} |
