aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-06-08 12:32:45 +0100
committerGitHub <noreply@github.com>2022-06-08 12:32:45 +0100
commit7e59edfaf725cce91153a2ef5a38671aa804fc29 (patch)
treef1eefd3b94f0afc1c56bebde3aeffef821ecf148
parent0c7f061325cd07213c4bba4f1c34790d98bbab02 (diff)
parent471b056ad154010f4c8a1f5bc830b20a3390c665 (diff)
downloadperlweeklychallenge-club-7e59edfaf725cce91153a2ef5a38671aa804fc29.tar.gz
perlweeklychallenge-club-7e59edfaf725cce91153a2ef5a38671aa804fc29.tar.bz2
perlweeklychallenge-club-7e59edfaf725cce91153a2ef5a38671aa804fc29.zip
Merge pull request #6225 from pokgopun/pwc145
pwc145 solution in go
-rw-r--r--challenge-145/pokgopun/README1
-rw-r--r--challenge-145/pokgopun/go/ch-1.go53
-rw-r--r--challenge-145/pokgopun/go/ch-2.go80
-rw-r--r--challenge-146/pokgopun/go/ch-2.go35
4 files changed, 145 insertions, 24 deletions
diff --git a/challenge-145/pokgopun/README b/challenge-145/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-145/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-145/pokgopun/go/ch-1.go b/challenge-145/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..d39063d4ef
--- /dev/null
+++ b/challenge-145/pokgopun/go/ch-1.go
@@ -0,0 +1,53 @@
+// You are given 2 arrays of same size, @a and @b.
+// Write a script to implement Dot Product.
+// Usage: go run ch-1.go 1,2,3 4,5,6
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+ "strings"
+)
+
+func main() {
+ var sample [][]int
+ if len(os.Args) > 2 {
+ m := map[int]struct{}{}
+ for _, v := range os.Args[1:] {
+ strs := strings.Split(v, ",")
+ m[len(strs)] = struct{}{}
+ nums := make([]int, len(strs))
+ for i, v := range strs {
+ n, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ nums[i] = n
+ }
+ sample = append(sample, nums)
+ }
+ if len(m) > 1 {
+ log.Fatal("Arrays are not in the same size")
+ }
+ } else {
+ sample = [][]int{
+ []int{1, 2, 3},
+ []int{4, 5, 6},
+ }
+ }
+ fmt.Println(sample, "=>", dp(sample))
+}
+
+func dp(ss [][]int) (r int) {
+ l := len(ss[0])
+ for i := 0; i < l; i++ {
+ n := 1
+ for j := 0; j < len(ss); j++ {
+ n *= ss[j][i]
+ }
+ r += n
+ }
+ return r
+}
diff --git a/challenge-145/pokgopun/go/ch-2.go b/challenge-145/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..6ec499781f
--- /dev/null
+++ b/challenge-145/pokgopun/go/ch-2.go
@@ -0,0 +1,80 @@
+// Write a script to create a Palindromic Tree for the given string.
+// All examples do not have a case that a letter happens more than twice in given words.
+// As a result, will initially ignore the case here.
+package main
+
+import (
+ "fmt"
+ "os"
+ "regexp"
+ "sort"
+ "strings"
+)
+
+func main() {
+ var sample []string
+ if len(os.Args) > 1 {
+ sample = os.Args[1:]
+ } else {
+ sample = []string{
+ "redivider",
+ "deific",
+ "rotors",
+ "challenge",
+ "champion",
+ "christmas",
+ }
+ }
+ for _, v := range sample {
+ p := newPldt(v)
+ fmt.Println(p)
+ }
+}
+
+type pldt struct {
+ word string
+ seen map[string]bool
+}
+
+func newPldt(s string) (p pldt) {
+ p.word = s
+ p.seen = make(map[string]bool)
+ return p
+}
+
+func (p pldt) find(b byte) (s []string) {
+ for _, v := range regexp.MustCompile(string(b)+".*"+string(b)).FindAll([]byte(p.word), -1) {
+ o := string(v)
+ sort.SliceStable(v, func(i, j int) bool {
+ return true
+ })
+ if o == string(v) {
+ s = append(s, o)
+ }
+ //fmt.Println(string(v))
+ }
+ return s
+}
+
+func (p pldt) String() string {
+ var b strings.Builder
+ b.WriteString("Input: '" + p.word + "'\nOutput: ")
+ for _, c := range []byte(p.word) {
+ if p.seen[string(c)] {
+ continue
+ }
+ p.seen[string(c)] = true
+ b.Write([]byte{c, ' '})
+ /**/
+ for _, v := range p.find(c) {
+ if p.seen[v] {
+ continue
+ }
+ p.seen[v] = true
+ b.WriteString(v + " ")
+ }
+ /**/
+ }
+ b.WriteByte('\n')
+ return b.String()
+}
diff --git a/challenge-146/pokgopun/go/ch-2.go b/challenge-146/pokgopun/go/ch-2.go
index b8aea49f0f..a8545314f3 100644
--- a/challenge-146/pokgopun/go/ch-2.go
+++ b/challenge-146/pokgopun/go/ch-2.go
@@ -2,35 +2,22 @@ package main
import (
"fmt"
- "log"
"os"
)
func main() {
- samples := [][2]uint{
- [2]uint{3, 5},
- [2]uint{4, 3},
- }
+ var sample []string
if len(os.Args) > 1 {
- var sample [2]uint
- _, err := fmt.Sscanf(os.Args[1], "%d/%d", &sample[0], &sample[1])
- if err != nil {
- log.Fatal(err)
+ sample = os.Args[1:]
+ } else {
+ sample = []string{
+ "redivider",
+ "deific",
+ "rotors",
+ "challenge",
+ "champion",
+ "christmas",
}
- samples = [][2]uint{sample}
- }
- for _, v := range samples {
- p := parent(v)
- gp := parent(p)
- fmt.Printf("Input: member = '%d/%d'\nOutput: parent ='%d/%d' and grandparent = '%d/%d'\n", v[0], v[1], p[0], p[1], gp[0], gp[1])
- }
-}
-
-func parent(s [2]uint) [2]uint {
- if s[0] > s[1] {
- return [2]uint{s[0] - s[1], s[1]}
- } else if s[0] < s[1] {
- return [2]uint{s[0], s[1] - s[0]}
}
- return [2]uint{1, 1}
+ fmt.Println(sample)
}