diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-05-28 18:06:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-28 18:06:39 +0100 |
| commit | 6238d5a789e2741bb94ca7b22903b62637774175 (patch) | |
| tree | e27a0ae9509b69280b501c04791b8b733f18316a | |
| parent | 738a0a50cf33dfe03fd8b182a06e7b13907cd5ca (diff) | |
| parent | 89819967055c5f5d39f78d143f7880f6b602fe48 (diff) | |
| download | perlweeklychallenge-club-6238d5a789e2741bb94ca7b22903b62637774175.tar.gz perlweeklychallenge-club-6238d5a789e2741bb94ca7b22903b62637774175.tar.bz2 perlweeklychallenge-club-6238d5a789e2741bb94ca7b22903b62637774175.zip | |
Merge pull request #6163 from pokgopun/pwc019
Pwc019
| -rw-r--r-- | challenge-019/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-019/pokgopun/go/ch-1.go | 36 | ||||
| -rw-r--r-- | challenge-019/pokgopun/go/ch-2.go | 42 |
3 files changed, 79 insertions, 0 deletions
diff --git a/challenge-019/pokgopun/README b/challenge-019/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-019/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun 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() +} |
