aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-07-08 15:31:03 +1000
committerMichael Manring <michael@manring>2024-07-08 15:31:03 +1000
commitcd47e4b155a5c72586de6d9e4e39fc5301becb9a (patch)
tree06844c0cf31651f8b745d0204785ac59135e995d
parent062951f4582d6d397b1a924a5277e81c369407af (diff)
downloadperlweeklychallenge-club-cd47e4b155a5c72586de6d9e4e39fc5301becb9a.tar.gz
perlweeklychallenge-club-cd47e4b155a5c72586de6d9e4e39fc5301becb9a.tar.bz2
perlweeklychallenge-club-cd47e4b155a5c72586de6d9e4e39fc5301becb9a.zip
pwc277 solution in go
-rw-r--r--challenge-277/pokgopun/go/ch-1.go97
-rw-r--r--challenge-277/pokgopun/go/ch-2.go89
2 files changed, 186 insertions, 0 deletions
diff --git a/challenge-277/pokgopun/go/ch-1.go b/challenge-277/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..a8b499055d
--- /dev/null
+++ b/challenge-277/pokgopun/go/ch-1.go
@@ -0,0 +1,97 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/
+/*#
+
+Task 1: Count Common
+
+Submitted by: [53]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two array of strings, @words1 and @words2.
+
+ Write a script to return the count of words that appears in both arrays
+ exactly once.
+
+Example 1
+
+Input: @words1 = ("Perl", "is", "my", "friend")
+ @words2 = ("Perl", "and", "Raku", "are", "friend")
+Output: 2
+
+The words "Perl" and "friend" appear once in each array.
+
+Example 2
+
+Input: @words1 = ("Perl", "and", "Python", "are", "very", "similar")
+ @words2 = ("Python", "is", "top", "in", "guest", "languages")
+Output: 1
+
+Example 3
+
+Input: @words1 = ("Perl", "is", "imperative", "Lisp", "is", "functional")
+ @words2 = ("Crystal", "is", "similar", "to", "Ruby")
+Output: 0
+
+Task 2: Strong Pair
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type words []string
+
+func (wd words) removeMultiple() words {
+ m := make(map[string]int)
+ for _, v := range wd {
+ m[v]++
+ }
+ var r words
+ for k, v := range m {
+ if v > 1 {
+ continue
+ }
+ r = append(r, k)
+ }
+ return r
+}
+
+func (wd words) remainMultiple() words {
+ m := make(map[string]int)
+ for _, v := range wd {
+ m[v]++
+ }
+ var r words
+ for k, v := range m {
+ if v > 1 {
+ r = append(r, k)
+ }
+ }
+ return r
+}
+
+func countCommon(words1, words2 words) int {
+ return len(
+ append(
+ words1.removeMultiple(), words2.removeMultiple()...,
+ ).remainMultiple(),
+ )
+}
+
+func main() {
+ for _, data := range []struct {
+ words1, words2 words
+ count int
+ }{
+ {words{"Perl", "is", "my", "friend"}, words{"Perl", "and", "Raku", "are", "friend"}, 2},
+ {words{"Perl", "and", "Python", "are", "very", "similar"}, words{"Python", "is", "top", "in", "guest", "languages"}, 1},
+ {words{"Perl", "is", "imperative", "Lisp", "is", "functional"}, words{"Crystal", "is", "similar", "to", "Ruby"}, 0},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(countCommon(data.words1, data.words2), data.count)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-277/pokgopun/go/ch-2.go b/challenge-277/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..277965f895
--- /dev/null
+++ b/challenge-277/pokgopun/go/ch-2.go
@@ -0,0 +1,89 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-277/
+/*#
+
+Task 2: Strong Pair
+
+Submitted by: [54]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @ints.
+
+ Write a script to return the count of all strong pairs in the given
+ array.
+
+ A pair of integers x and y is called strong pair if it satisfies: 0
+ < |x - y| < min(x, y).
+
+Example 1
+
+Input: @ints = (1, 2, 3, 4, 5)
+Ouput: 4
+
+Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5)
+
+Example 2
+
+Input: @ints = (5, 7, 1, 7)
+Ouput: 1
+
+Strong Pairs: (5, 7)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 14th July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) removeDuplicate() ints {
+ m := make(map[int]int)
+ for _, v := range is {
+ m[v]++
+ }
+ var r ints
+ for k := range m {
+ r = append(r, k)
+ }
+ return r
+}
+
+func strongPair(is ints) int {
+ is = is.removeDuplicate()
+ c := 0
+ l := len(is)
+ for i := 0; i < l-1; i++ {
+ for j := i + 1; j < l; j++ {
+ if is[i] == is[j] {
+ continue
+ }
+ if max(is[i], is[j]) < 2*min(is[i], is[j]) {
+ c++
+ }
+ }
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ ints ints
+ count int
+ }{
+ {ints{1, 2, 3, 4, 5}, 4},
+ {ints{5, 7, 1, 7}, 1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(strongPair(data.ints), data.count)) // blank if ok, otherwise show the difference
+ }
+}