aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-05-13 22:36:32 +1000
committerPok <pok@goyangi>2025-05-13 22:36:32 +1000
commit7c3ee687c1e483f37869f932aebd08a0472ac7bb (patch)
tree3caad0dfcdad659730beab6c609c95917169c8ec
parent32ef6ceed6fcb350e87bfa2455a7baf6b2ab5445 (diff)
downloadperlweeklychallenge-club-7c3ee687c1e483f37869f932aebd08a0472ac7bb.tar.gz
perlweeklychallenge-club-7c3ee687c1e483f37869f932aebd08a0472ac7bb.tar.bz2
perlweeklychallenge-club-7c3ee687c1e483f37869f932aebd08a0472ac7bb.zip
pwc321 solution in go
-rw-r--r--challenge-321/pokgopun/go/ch-1.go91
-rw-r--r--challenge-321/pokgopun/go/ch-2.go95
2 files changed, 186 insertions, 0 deletions
diff --git a/challenge-321/pokgopun/go/ch-1.go b/challenge-321/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..baffd55378
--- /dev/null
+++ b/challenge-321/pokgopun/go/ch-1.go
@@ -0,0 +1,91 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/
+/*#
+
+Task 1: Distinct Average
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of numbers with even length.
+
+ Write a script to return the count of distinct average. The average is
+ calculate by removing the minimum and the maximum, then average of the
+ two.
+
+Example 1
+
+Input: @nums = (1, 2, 4, 3, 5, 6)
+Output: 1
+
+Step 1: Min = 1, Max = 6, Avg = 3.5
+Step 2: Min = 2, Max = 5, Avg = 3.5
+Step 3: Min = 3, Max = 4, Avg = 3.5
+
+The count of distinct average is 1.
+
+Example 2
+
+Input: @nums = (0, 2, 4, 8, 3, 5)
+Output: 2
+
+Step 1: Min = 0, Max = 8, Avg = 4
+Step 2: Min = 2, Max = 5, Avg = 3.5
+Step 3: Min = 3, Max = 4, Avg = 3.5
+
+The count of distinct average is 2.
+
+Example 3
+
+Input: @nums = (7, 3, 1, 0, 5, 9)
+Output: 2
+
+Step 1: Min = 0, Max = 9, Avg = 4.5
+Step 2: Min = 1, Max = 7, Avg = 4
+Step 3: Min = 3, Max = 5, Avg = 4
+
+The count of distinct average is 2.
+
+Task 2: Backspace Compare
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type qr struct {
+ q, r int
+}
+
+type input []int
+
+func (in input) process() int {
+ l := len(in)
+ slices.Sort(in)
+ m := make(map[qr]bool)
+ for i := range l / 2 {
+ sm := (in[i] + in[l-1-i])
+ m[qr{sm / 2, sm % 2}] = true
+ }
+ //fmt.Println(in, m)
+ return len(m)
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output int
+ }{
+ {input{1, 2, 4, 3, 5, 6}, 1},
+ {input{0, 2, 4, 8, 3, 5}, 2},
+ {input{7, 3, 1, 0, 5, 9}, 2},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-321/pokgopun/go/ch-2.go b/challenge-321/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..9ea092effd
--- /dev/null
+++ b/challenge-321/pokgopun/go/ch-2.go
@@ -0,0 +1,95 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/
+/*#
+
+Task 2: Backspace Compare
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two strings containing zero or more #.
+
+ Write a script to return true if the two given strings are same by
+ treating # as backspace.
+
+Example 1
+
+Input: $str1 = "ab#c"
+ $str2 = "ad#c"
+Output: true
+
+For first string, we remove "b" as it is followed by "#".
+For second string, we remove "d" as it is followed by "#".
+In the end both strings became the same.
+
+Example 2
+
+Input: $str1 = "ab##"
+ $str2 = "a#b#"
+Output: true
+
+Example 3
+
+Input: $str1 = "a#b"
+ $str2 = "c"
+Output: false
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 18th May 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type input struct {
+ str1, str2 string
+}
+
+func (in input) process() bool {
+ //fmt.Printf("%v, '%s', '%s'\n", in, ab(in.str1), ab(in.str2))
+ return ab(in.str1) == ab(in.str2)
+}
+
+func ab(str string) string {
+ l := len(str)
+ bs := []byte(str)
+ i := 0
+ for i < l {
+ if bs[i] == byte('#') {
+ if i > 0 {
+ slices.Delete(bs, i-1, i+1)
+ l -= 2
+ i--
+ } else {
+ slices.Delete(bs, i, i+1)
+ l--
+ }
+ } else {
+ i++
+ }
+ }
+ return string(bs)
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output bool
+ }{
+ {input{"ab#c", "ad#c"}, true},
+ {input{"ab##", "a#b#"}, true},
+ {input{"a#b", "c"}, false},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}