diff options
| author | Michael Manring <michael@manring> | 2022-06-23 17:28:14 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-06-23 17:28:14 +0700 |
| commit | f99ddd5d4e83a13dda34da1c5ca83529cc210f6d (patch) | |
| tree | d69480e1b8fa01c26524dbbdd2c2d4ac4d0220a3 | |
| parent | 810991ea0dfcc6edb1ab06252a19cfe5bee6477a (diff) | |
| download | perlweeklychallenge-club-f99ddd5d4e83a13dda34da1c5ca83529cc210f6d.tar.gz perlweeklychallenge-club-f99ddd5d4e83a13dda34da1c5ca83529cc210f6d.tar.bz2 perlweeklychallenge-club-f99ddd5d4e83a13dda34da1c5ca83529cc210f6d.zip | |
pwc143 solution in go
| -rw-r--r-- | challenge-143/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-143/pokgopun/go/ch-1.go | 28 | ||||
| -rw-r--r-- | challenge-143/pokgopun/go/ch-2.go | 35 | ||||
| -rw-r--r-- | challenge-143/pokgopun/go/stealthy/stealthy.go | 22 | ||||
| -rw-r--r-- | challenge-143/pokgopun/go/stealthy/stealthy_test.go | 17 |
5 files changed, 103 insertions, 0 deletions
diff --git a/challenge-143/pokgopun/README b/challenge-143/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-143/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-143/pokgopun/go/ch-1.go b/challenge-143/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..21a469bc60 --- /dev/null +++ b/challenge-143/pokgopun/go/ch-1.go @@ -0,0 +1,28 @@ +/* +You are given a string, $s, containing mathematical expression. + +Write a script to print the result of the mathematical expression. To keep it simple, please only accept + - * (). +*/ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/Knetic/govaluate" +) + +func main() { + if len(os.Args) < 2 { + os.Args = append(os.Args, "10 + 20 - 5", "(10 + 20 - 5) * 2", "8 * .7 - (3 - 13.5)") + } + for _, v := range os.Args[1:] { + expression, err := govaluate.NewEvaluableExpression(v) + result, err := expression.Evaluate(nil) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Input: s = %q\nOutput: %v\n\n", v, result) + } +} diff --git a/challenge-143/pokgopun/go/ch-2.go b/challenge-143/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..7b85518d74 --- /dev/null +++ b/challenge-143/pokgopun/go/ch-2.go @@ -0,0 +1,35 @@ +/* +You are given a positive number, $n. + +Write a script to find out if the given number is Stealthy Number. + +A positive integer N is stealthy, if there exist positive integers a, b, c, d such that a * b = c * d = N and a + b = c + d + 1. +*/ +package main + +import ( + "fmt" + "log" + "os" + "strconv" + + "github.com/pokgopun/go/stealthy" +) + +func main() { + var nums []uint + if len(os.Args) > 1 { + for _, v := range os.Args[1:] { + n, err := strconv.ParseUint(v, 10, 64) + if err != nil { + log.Fatal(err) + } + nums = append(nums, uint(n)) + } + } else { + nums = []uint{36, 12, 6} + } + for _, v := range nums { + fmt.Printf("Input: n = %d\nOutput: %t\n\n", v, stealthy.IsStealthy(v)) + } +} diff --git a/challenge-143/pokgopun/go/stealthy/stealthy.go b/challenge-143/pokgopun/go/stealthy/stealthy.go new file mode 100644 index 0000000000..cce77dfd12 --- /dev/null +++ b/challenge-143/pokgopun/go/stealthy/stealthy.go @@ -0,0 +1,22 @@ +package stealthy + +import ( + "math" +) + +func IsStealthy(n uint) bool { + lim := uint(math.Floor(math.Sqrt(float64(n)))) + prev := n + 1 + var sum uint + for d := uint(2); d <= lim; d++ { + if n%d == 0 { + sum = d + n/d + //fmt.Println("n=", n, "prev=", prev, "sum=", sum) + if prev-sum == 1 { + return true + } + prev = sum + } + } + return false +} diff --git a/challenge-143/pokgopun/go/stealthy/stealthy_test.go b/challenge-143/pokgopun/go/stealthy/stealthy_test.go new file mode 100644 index 0000000000..840bc71d0f --- /dev/null +++ b/challenge-143/pokgopun/go/stealthy/stealthy_test.go @@ -0,0 +1,17 @@ +package stealthy + +import "testing" + +func TestIsStealthy(t *testing.T) { + m := map[uint]bool{ + 36: true, + 12: true, + 6: false, + } + for n, ans := range m { + res := IsStealthy(n) + if res != ans { + t.Errorf("incorrect result for %d: expected %t, got %t", n, ans, res) + } + } +} |
