diff options
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 | 84 |
3 files changed, 138 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..9f75a558ec --- /dev/null +++ b/challenge-145/pokgopun/go/ch-2.go @@ -0,0 +1,84 @@ +// 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 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" + "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", + "banana", + "redeemable", + "deterministic", + "heterogeneity", + "initiation", + "monopolous", + "honolulu", + } + } + for _, v := range sample { + p := newPldt(v) + fmt.Println(p) + } +} + +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) + p.cpos = make(map[byte][]int) + for i, v := range []byte(p.word) { + p.cpos[v] = append(p.cpos[v], i) + } + for _, c := range []byte(p.word) { + if p.seen[string(c)] { + continue + } + p.seen[string(c)] = true + 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 + } + } + } + } + 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() +} |
