aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-013/pokgopun/README1
-rw-r--r--challenge-013/pokgopun/go/ch-1.go42
-rw-r--r--challenge-013/pokgopun/go/ch-2.go39
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-013/pokgopun/README b/challenge-013/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-013/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-013/pokgopun/go/ch-1.go b/challenge-013/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..d7e704436e
--- /dev/null
+++ b/challenge-013/pokgopun/go/ch-1.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "fmt"
+ "time"
+)
+
+func main() {
+ fmt.Println(newLastWeekdayOfMonth(time.Friday, 2019))
+}
+func monthdayCount(y int) (r [12]int) {
+ r = [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
+ if y%4 == 0 {
+ r[time.February]++
+ }
+ return r
+}
+
+type lastWeekdayOfMonth [12]time.Time
+
+func (lwdom lastWeekdayOfMonth) String() (r string) {
+ for _, t := range lwdom {
+ r += fmt.Sprintln(t.Format("2006/01/02"))
+ }
+ return r
+}
+func newLastWeekdayOfMonth(wd time.Weekday, y int) (lwdom lastWeekdayOfMonth) {
+ for i, v := range monthdayCount(y) {
+ // Find offset between the last day of the month and the specified weekday
+ t := time.Date(y, time.Month(i+1), v, 0, 0, 0, 0, time.UTC)
+ o := int(wd - t.Weekday())
+ // The offset is the number of day we need to go back to the latest specified weekday in certain month
+ if o != 0 {
+ // plus sign indicate the offset between specified weekday and the current weekday of the previous week
+ if o > 0 {
+ o -= 7
+ }
+ }
+ lwdom[i] = t.AddDate(0, 0, o)
+ }
+ return lwdom
+}
diff --git a/challenge-013/pokgopun/go/ch-2.go b/challenge-013/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..7cee43a19a
--- /dev/null
+++ b/challenge-013/pokgopun/go/ch-2.go
@@ -0,0 +1,39 @@
+/*
+ F ( 0 ) = 1 ; M ( 0 ) = 0
+ F ( n ) = n − M ( F ( n − 1 ) ) , n > 0
+ M ( n ) = n − F ( M ( n − 1 ) ) , n > 0.
+*/
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "strings"
+)
+
+func main() {
+ w := bufio.NewWriter(os.Stdout)
+ fmt.Fprintf(w, "| %4s | %4s | %4s |", "n", "F(n)", "M(n)")
+ w.WriteString("\n" + strings.Repeat("-", w.Buffered()) + "\n")
+ var n uint
+ for n <= 20 {
+ fmt.Fprintf(w, "| %4d | %4d | %4d |\n", n, female(n), male(n))
+ n++
+ }
+ w.Flush()
+}
+
+func female(n uint) uint {
+ if n == 0 {
+ return 1
+ }
+ return n - male(female(n-1))
+}
+
+func male(n uint) uint {
+ if n == 0 {
+ return 0
+ }
+ return n - female(male(n-1))
+}