From 471b056ad154010f4c8a1f5bc830b20a3390c665 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 6 Jun 2022 11:54:42 +0700 Subject: pwc145 solution in go --- challenge-145/pokgopun/README | 1 + challenge-145/pokgopun/go/ch-1.go | 53 ++++++++++++++++++++++++++ challenge-145/pokgopun/go/ch-2.go | 80 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 challenge-145/pokgopun/README create mode 100644 challenge-145/pokgopun/go/ch-1.go create mode 100644 challenge-145/pokgopun/go/ch-2.go (limited to 'challenge-145/pokgopun') 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() +} -- cgit From 0709228ea1c882a9d1c578af1ca52a2ca695539c Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Wed, 8 Jun 2022 15:31:07 +0700 Subject: pwc145 solution in go - ch-2 to handle the case where a letter happens more than twice in a word --- challenge-145/pokgopun/go/ch-2.go | 66 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 31 deletions(-) (limited to 'challenge-145/pokgopun') diff --git a/challenge-145/pokgopun/go/ch-2.go b/challenge-145/pokgopun/go/ch-2.go index 6ec499781f..9f75a558ec 100644 --- a/challenge-145/pokgopun/go/ch-2.go +++ b/challenge-145/pokgopun/go/ch-2.go @@ -1,12 +1,11 @@ // 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. +// All examples do not have a case that a letter happens more than twice in given words but we will handle the case as well +// Here additional examples for the case: banana, redeemable, deterministic, heterogeneity, initiation, monopolous, honolulu package main import ( "fmt" "os" - "regexp" "sort" "strings" ) @@ -23,6 +22,13 @@ func main() { "challenge", "champion", "christmas", + "banana", + "redeemable", + "deterministic", + "heterogeneity", + "initiation", + "monopolous", + "honolulu", } } for _, v := range sample { @@ -34,47 +40,45 @@ func main() { type pldt struct { word string seen map[string]bool + cpos map[byte][]int + vals []string } 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)) + p.cpos = make(map[byte][]int) + for i, v := range []byte(p.word) { + p.cpos[v] = append(p.cpos[v], i) } - 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.vals = append(p.vals, string(c)) + for i, pos1 := range p.cpos[c] { + for _, pos2 := range p.cpos[c][i+1:] { + o := p.word[pos1 : pos2+1] + if p.seen[o] { + continue + } + r := []byte(o) + sort.SliceStable(r, func(i, j int) bool { + return true + }) + if o == string(r) { + p.vals = append(p.vals, o) + p.seen[o] = true + } } - p.seen[v] = true - b.WriteString(v + " ") } - /**/ } - b.WriteByte('\n') + return p +} + +func (p pldt) String() string { + var b strings.Builder + b.WriteString("Input: '" + p.word + "'\nOutput: " + strings.Join(p.vals, " ") + "\n") return b.String() } -- cgit