diff options
| author | Michael Manring <michael@manring> | 2022-06-24 00:11:15 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-06-24 11:46:44 +0700 |
| commit | 4c72ecc3c879a0479f2ec53c02acbb4d22a1893b (patch) | |
| tree | c7da2b8b989e20dc3a71d4c7f973d7a0d61b5792 /challenge-142 | |
| parent | dc0541bcb73b41aa3d2960dffaa884226ae035af (diff) | |
| download | perlweeklychallenge-club-4c72ecc3c879a0479f2ec53c02acbb4d22a1893b.tar.gz perlweeklychallenge-club-4c72ecc3c879a0479f2ec53c02acbb4d22a1893b.tar.bz2 perlweeklychallenge-club-4c72ecc3c879a0479f2ec53c02acbb4d22a1893b.zip | |
pwc142 solution in go
Diffstat (limited to 'challenge-142')
| -rw-r--r-- | challenge-142/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-142/pokgopun/go/ch-1.go | 34 | ||||
| -rw-r--r-- | challenge-142/pokgopun/go/ch-2.go | 57 | ||||
| -rw-r--r-- | challenge-142/pokgopun/go/dldcount/dldcount.go | 22 | ||||
| -rw-r--r-- | challenge-142/pokgopun/go/dldcount/dldcount_test.go | 22 | ||||
| -rw-r--r-- | challenge-142/pokgopun/go/sleepsort/sleepsort.go | 32 | ||||
| -rw-r--r-- | challenge-142/pokgopun/go/sleepsort/sleepsort_test.go | 43 |
7 files changed, 211 insertions, 0 deletions
diff --git a/challenge-142/pokgopun/README b/challenge-142/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-142/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-142/pokgopun/go/ch-1.go b/challenge-142/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..f87c7adb11 --- /dev/null +++ b/challenge-142/pokgopun/go/ch-1.go @@ -0,0 +1,34 @@ +/* +TASK #1 › Divisor Last Digit +Submitted by: Mohammad S Anwar +You are given positive integers, $m and $n. + +Write a script to find total count of divisors of $m having last digit $n. + +*/ +package main + +import ( + "fmt" + "log" + "os" + "strings" + + "github.com/pokgopun/go/dldcount" +) + +func main() { + s := make([][2]uint, 1) + if len(os.Args) > 2 { + _, err := fmt.Sscanf(strings.Join(os.Args[1:], " "), "%d %d", &s[0][0], &s[0][1]) + if err != nil { + log.Fatal(err) + } + } else { + s[0] = [2]uint{24, 2} + s = append(s, [2]uint{30, 5}) + } + for _, v := range s { + fmt.Printf("Input: m = %d, n = %d\nOutput: %d\n\n", v[0], v[1], dldcount.Count(v[0], v[1])) + } +} diff --git a/challenge-142/pokgopun/go/ch-2.go b/challenge-142/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..c85eed17c4 --- /dev/null +++ b/challenge-142/pokgopun/go/ch-2.go @@ -0,0 +1,57 @@ +/* +TASK #2 › Sleep Sort +Submitted by: Adam Russell +Another joke sort similar to JortSort suggested by champion Adam Russell. + +You are given a list of numbers. + +Write a script to implement Sleep Sort. For more information, please checkout this post. +*/ +package main + +import ( + "fmt" + "log" + "os" + "sort" + "strconv" + "time" + + "github.com/google/go-cmp/cmp" + "github.com/pokgopun/go/sleepsort" +) + +func main() { + sos := [][]uint{ + []uint{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9}, + []uint{2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9}, + } + if len(os.Args) > 1 { + sos = [][]uint{make([]uint, len(os.Args[1:]))} + for i, v := range os.Args[1:] { + n, err := strconv.ParseUint(v, 10, 64) + if err != nil { + log.Fatal(err) + } + sos[0][i] = uint(n) + } + } + sor := make([][]uint, len(sos)) + copy(sor, sos) + var res []uint + durations := []time.Duration{time.Nanosecond, time.Microsecond, time.Millisecond} + for i, s := range sos { + fmt.Println("=> sleep sort", sor[i]) + sort.SliceStable(sor[i], func(m, n int) bool { + return sor[i][m] < sor[i][n] + }) + for _, d := range durations { + fmt.Println("->", d) + for j := 0; j < 3; j++ { + res = sleepsort.Sort(s, d) + fmt.Println("round", j+1, res, "ascending order:", cmp.Diff(sor[i], res) == "") + } + } + fmt.Printf("\n") + } +} diff --git a/challenge-142/pokgopun/go/dldcount/dldcount.go b/challenge-142/pokgopun/go/dldcount/dldcount.go new file mode 100644 index 0000000000..2a1088aac2 --- /dev/null +++ b/challenge-142/pokgopun/go/dldcount/dldcount.go @@ -0,0 +1,22 @@ +package dldcount + +import ( + "math" +) + +func Count(m, n uint) (c uint) { + lim := uint(math.Floor(math.Sqrt(float64(m)))) + if n == 1 { + c++ + } + for d := uint(2); d <= lim; d++ { + if m%d == 0 { + for _, v := range []uint{d, m / d} { + if v%10 == n { + c++ + } + } + } + } + return c +} diff --git a/challenge-142/pokgopun/go/dldcount/dldcount_test.go b/challenge-142/pokgopun/go/dldcount/dldcount_test.go new file mode 100644 index 0000000000..fcab676796 --- /dev/null +++ b/challenge-142/pokgopun/go/dldcount/dldcount_test.go @@ -0,0 +1,22 @@ +package dldcount + +import "testing" + +func TestDldCount(t *testing.T) { + data := []struct { + m, n, o uint + }{ + {24, 2, 2}, + {24, 1, 1}, + {24, 3, 1}, + {30, 5, 2}, + {30, 0, 1}, + {30, 6, 1}, + } + for _, d := range data { + res := Count(d.m, d.n) + if res != d.o { + t.Errorf("incorrect result: expected %d, got %d", d.o, res) + } + } +} diff --git a/challenge-142/pokgopun/go/sleepsort/sleepsort.go b/challenge-142/pokgopun/go/sleepsort/sleepsort.go new file mode 100644 index 0000000000..5bd271230e --- /dev/null +++ b/challenge-142/pokgopun/go/sleepsort/sleepsort.go @@ -0,0 +1,32 @@ +package sleepsort + +import ( + "sync" + "time" +) + +func Sort(s []uint, d time.Duration) (r []uint) { + l := len(s) + //done := make(chan struct{}) + out := make(chan uint, l) + var wg sync.WaitGroup + wg.Add(l) + for _, v := range s { + go func(i uint) { + //<-done + defer wg.Done() + time.Sleep(d * time.Duration(i)) + out <- i + }(v) + } + go func() { + //close(done) + wg.Wait() + close(out) + }() + + for v := range out { + r = append(r, v) + } + return r +} diff --git a/challenge-142/pokgopun/go/sleepsort/sleepsort_test.go b/challenge-142/pokgopun/go/sleepsort/sleepsort_test.go new file mode 100644 index 0000000000..2688144f82 --- /dev/null +++ b/challenge-142/pokgopun/go/sleepsort/sleepsort_test.go @@ -0,0 +1,43 @@ +package sleepsort + +import ( + "fmt" + "sort" + "testing" + "time" + + "github.com/google/go-cmp/cmp" +) + +var blackhole []uint + +func BenchmarkSort(b *testing.B) { + s := [][]uint{ + []uint{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9}, + []uint{2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9}, + } + r := make([][]uint, 2) + copy(r, s) + for _, v := range r { + sort.SliceStable(v, func(i, j int) bool { + return v[i] < v[j] + }) + } + //for _, d := range []time.Duration{time.Nanosecond, time.Microsecond, time.Millisecond, time.Second} { + for _, d := range []time.Duration{time.Nanosecond, time.Microsecond, time.Millisecond} { + b.Run(fmt.Sprintf("Sort-%v", d), func(b *testing.B) { + for j := 0; j < b.N; j++ { + for i, v := range s { + res := Sort(v, d) + //blackhole = res + /**/ + if diff := cmp.Diff(r[i], res); diff != "" { + //b.Error(diff) + b.Error("not in ascending order =>", res) + } + /**/ + } + } + }) + } +} |
