aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-06-24 20:02:16 +0100
committerGitHub <noreply@github.com>2022-06-24 20:02:16 +0100
commit242265f36e6c6d41b3996a25891c805b355f5bb7 (patch)
treedc0e2ee39466f47b62bc0886cc16306ead9504aa
parenta71e3289a55c2f295d704601405179df1caaa0b4 (diff)
parent5b657cbcb942bfd9ce7b3d67576ccc1f08e6e1d6 (diff)
downloadperlweeklychallenge-club-242265f36e6c6d41b3996a25891c805b355f5bb7.tar.gz
perlweeklychallenge-club-242265f36e6c6d41b3996a25891c805b355f5bb7.tar.bz2
perlweeklychallenge-club-242265f36e6c6d41b3996a25891c805b355f5bb7.zip
Merge pull request #6330 from pokgopun/pwc141
pwc141 solution in go
-rw-r--r--challenge-141/pokgopun/README1
-rw-r--r--challenge-141/pokgopun/go/ch-1.go40
-rw-r--r--challenge-141/pokgopun/go/ch-2.go58
-rw-r--r--challenge-141/pokgopun/go/likenums/likenums.go78
-rw-r--r--challenge-141/pokgopun/go/likenums/likenums_test.go59
5 files changed, 236 insertions, 0 deletions
diff --git a/challenge-141/pokgopun/README b/challenge-141/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-141/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-141/pokgopun/go/ch-1.go b/challenge-141/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..805952bc10
--- /dev/null
+++ b/challenge-141/pokgopun/go/ch-1.go
@@ -0,0 +1,40 @@
+/*
+TASK #1 › Number Divisors
+Submitted by: Mohammad S Anwar
+Write a script to find lowest 10 positive integers having exactly 8 divisors.
+
+Example
+24 is the first such number having exactly 8 divisors.
+1, 2, 3, 4, 6, 8, 12 and 24.
+*/
+package main
+
+import (
+ "fmt"
+ "math"
+)
+
+func main() {
+ var (
+ count, lim int
+ s []int
+ )
+ n := 1
+ for len(s) < 10 {
+ n++
+ lim = int(math.Floor(math.Sqrt(float64(n))))
+ if n == lim*lim {
+ continue
+ }
+ for d := 1; d <= lim; d++ {
+ if n%d == 0 {
+ count += 2
+ }
+ }
+ if count == 8 {
+ s = append(s, n)
+ }
+ count = 0
+ }
+ fmt.Println(s)
+}
diff --git a/challenge-141/pokgopun/go/ch-2.go b/challenge-141/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..b7db295481
--- /dev/null
+++ b/challenge-141/pokgopun/go/ch-2.go
@@ -0,0 +1,58 @@
+/*
+TASK #2 › Like Numbers
+Submitted by: Mohammad S Anwar
+You are given positive integers, $m and $n.
+
+Write a script to find total count of integers created using the digits of $m which is also divisible by $n.
+
+Repeating of digits are not allowed. Order/Sequence of digits can’t be altered. You are only allowed to use (n-1) digits at the most. For example, 432 is not acceptable integer created using the digits of 1234. Also for 1234, you can only have integers having no more than three digits.
+
+Example 1:
+Input: $m = 1234, $n = 2
+Output: 9
+
+Possible integers created using the digits of 1234 are:
+1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234.
+
+There are 9 integers divisible by 2 such as:
+2, 4, 12, 14, 24, 34, 124, 134 and 234.
+Example 2:
+Input: $m = 768, $n = 4
+Output: 3
+
+Possible integers created using the digits of 768 are:
+7, 6, 8, 76, 78 and 68.
+
+There are 3 integers divisible by 4 such as:
+8, 76 and 68.
+*/
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strings"
+
+ "github.com/pokgopun/go/likenums"
+)
+
+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{1234, 2}
+ s = append(s, [2]uint{768, 4})
+ }
+ for _, v := range s {
+ c, err := likenums.Count(v[0], v[1])
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("Input: m = %d, n = %d\nOutput: %d\n\n", v[0], v[1], c)
+ }
+}
diff --git a/challenge-141/pokgopun/go/likenums/likenums.go b/challenge-141/pokgopun/go/likenums/likenums.go
new file mode 100644
index 0000000000..c72b1dea0a
--- /dev/null
+++ b/challenge-141/pokgopun/go/likenums/likenums.go
@@ -0,0 +1,78 @@
+/*
+TASK #2 › Like Numbers
+Submitted by: Mohammad S Anwar
+You are given positive integers, $m and $n.
+
+Write a script to find total count of integers created using the digits of $m which is also divisible by $n.
+
+Repeating of digits are not allowed. Order/Sequence of digits can’t be altered. You are only allowed to use (n-1) digits at the most. For example, 432 is not acceptable integer created using the digits of 1234. Also for 1234, you can only have integers having no more than three digits.
+
+Example 1:
+Input: $m = 1234, $n = 2
+Output: 9
+
+Possible integers created using the digits of 1234 are:
+1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234.
+
+There are 9 integers divisible by 2 such as:
+2, 4, 12, 14, 24, 34, 124, 134 and 234.
+Example 2:
+Input: $m = 768, $n = 4
+Output: 3
+
+Possible integers created using the digits of 768 are:
+7, 6, 8, 76, 78 and 68.
+
+There are 3 integers divisible by 4 such as:
+8, 76 and 68.
+*/
+package likenums
+
+import (
+ "errors"
+ "strconv"
+)
+
+func Count(m, n uint) (c uint, err error) {
+ if n == 0 {
+ return 0, errors.New("divisor must be greater than zero")
+ }
+ if m < 10 {
+ return 0, errors.New("must have at least two digits")
+ }
+ s := strconv.FormatUint(uint64(m), 10)
+ seen := make(map[byte]int)
+ for _, v := range []byte(s) {
+ seen[v]++
+ if seen[v] > 1 {
+ return 0, errors.New("repeating of digits are not allowed")
+ }
+ }
+ l := len(s)
+ var num uint64
+ for i := 1; i < l; i++ {
+ for _, v := range getCombo(i, s) {
+ num, _ = strconv.ParseUint(v, 10, 64)
+ if uint(num)%n == 0 {
+ c++
+ }
+ }
+ }
+ return c, nil
+}
+func getCombo(n int, e string) (r []string) {
+ var c string
+ cTree(n, e, c, func(s string) {
+ r = append(r, s)
+ })
+ return r
+}
+func cTree(n int, e string, c string, f func(s string)) {
+ if len(c) == n || len(c)+len(e) == n {
+ f((c + e)[:n])
+ } else {
+ for i := 0; len(c)+len(e)-i >= n; i++ {
+ cTree(n, e[i+1:], c+string(e[i]), f)
+ }
+ }
+}
diff --git a/challenge-141/pokgopun/go/likenums/likenums_test.go b/challenge-141/pokgopun/go/likenums/likenums_test.go
new file mode 100644
index 0000000000..742c44296b
--- /dev/null
+++ b/challenge-141/pokgopun/go/likenums/likenums_test.go
@@ -0,0 +1,59 @@
+/*
+TASK #2 › Like Numbers
+Submitted by: Mohammad S Anwar
+You are given positive integers, $m and $n.
+
+Write a script to find total count of integers created using the digits of $m which is also divisible by $n.
+
+Repeating of digits are not allowed. Order/Sequence of digits can’t be altered. You are only allowed to use (n-1) digits at the most. For example, 432 is not acceptable integer created using the digits of 1234. Also for 1234, you can only have integers having no more than three digits.
+
+Example 1:
+Input: $m = 1234, $n = 2
+Output: 9
+
+Possible integers created using the digits of 1234 are:
+1, 2, 3, 4, 12, 13, 14, 23, 24, 34, 123, 124, 134 and 234.
+
+There are 9 integers divisible by 2 such as:
+2, 4, 12, 14, 24, 34, 124, 134 and 234.
+Example 2:
+Input: $m = 768, $n = 4
+Output: 3
+
+Possible integers created using the digits of 768 are:
+7, 6, 8, 76, 78 and 68.
+
+There are 3 integers divisible by 4 such as:
+8, 76 and 68.
+*/
+package likenums
+
+import "testing"
+
+func TestCount(t *testing.T) {
+ data := []struct {
+ m, n, o uint
+ errMsg string
+ }{
+ {1234, 2, 9, ""},
+ {1234, 1, 14, ""},
+ {768, 4, 3, ""},
+ {768, 1, 6, ""},
+ {7, 1, 0, "must have at least two digits"},
+ {43212, 1, 0, "repeating of digits are not allowed"},
+ {78, 0, 0, "divisor must be greater than zero"},
+ }
+ for _, d := range data {
+ res, err := Count(d.m, d.n)
+ if res != d.o {
+ t.Errorf("incorrect result: expected %d, got %d", d.o, res)
+ }
+ var errMsg string
+ if err != nil {
+ errMsg = err.Error()
+ }
+ if errMsg != d.errMsg {
+ t.Errorf("Expected error message `%s`, got `%s`", d.errMsg, errMsg)
+ }
+ }
+}