diff options
| author | Michael Manring <michael@manring> | 2022-06-06 11:54:42 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-06-08 14:41:06 +0700 |
| commit | 471b056ad154010f4c8a1f5bc830b20a3390c665 (patch) | |
| tree | 86a9028db59c396b9b4072bb98680382a749de3f /challenge-145 | |
| parent | 4b569aef972ae1d1b9b6811ca336cd77a03b4998 (diff) | |
| download | perlweeklychallenge-club-471b056ad154010f4c8a1f5bc830b20a3390c665.tar.gz perlweeklychallenge-club-471b056ad154010f4c8a1f5bc830b20a3390c665.tar.bz2 perlweeklychallenge-club-471b056ad154010f4c8a1f5bc830b20a3390c665.zip | |
pwc145 solution in go
Diffstat (limited to 'challenge-145')
| -rw-r--r-- | challenge-145/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-145/pokgopun/go/ch-1.go | 53 | ||||
| -rw-r--r-- | challenge-145/pokgopun/go/ch-2.go | 80 |
3 files changed, 134 insertions, 0 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() +} |
