aboutsummaryrefslogtreecommitdiff
path: root/challenge-276
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-07-01 22:45:18 +1000
committerMichael Manring <michael@manring>2024-07-01 22:45:18 +1000
commitc72dac8f5f7406025c8b090db54172c900569e7e (patch)
tree7d9b12d385756c681ff620d06a738e61afe1e05f /challenge-276
parentdb5ffff46ce7a5f46f50dce151f8de19b070d5bd (diff)
downloadperlweeklychallenge-club-c72dac8f5f7406025c8b090db54172c900569e7e.tar.gz
perlweeklychallenge-club-c72dac8f5f7406025c8b090db54172c900569e7e.tar.bz2
perlweeklychallenge-club-c72dac8f5f7406025c8b090db54172c900569e7e.zip
pwc276 solution in go
Diffstat (limited to 'challenge-276')
-rw-r--r--challenge-276/pokgopun/go/ch-1.go83
-rw-r--r--challenge-276/pokgopun/go/ch-2.go105
2 files changed, 188 insertions, 0 deletions
diff --git a/challenge-276/pokgopun/go/ch-1.go b/challenge-276/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..ba9885236e
--- /dev/null
+++ b/challenge-276/pokgopun/go/ch-1.go
@@ -0,0 +1,83 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-276/
+/*#
+
+Task 1: Complete Day
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers, @hours.
+
+ Write a script to return the number of pairs that forms a complete day.
+
+ A complete day is defined as a time duration that is an exact
+ multiple of 24 hours.
+
+Example 1
+
+Input: @hours = (12, 12, 30, 24, 24)
+Output: 2
+
+Pair 1: (12, 12)
+Pair 2: (24, 24)
+
+Example 2
+
+Input: @hours = (72, 48, 24, 5)
+Output: 3
+
+Pair 1: (72, 48)
+Pair 2: (72, 24)
+Pair 3: (48, 24)
+
+Example 3
+
+Input: @hours = (12, 18, 24)
+Output: 0
+
+Task 2: Maximum Frequency
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type hour int
+
+func (hr hour) isCompleteDay() bool {
+ return hr%24 == 0
+}
+
+type hours []hour
+
+func (hrs hours) countCompleteDayPair() int {
+ c := 0
+ l := len(hrs)
+ for i := 0; i < l-1; i++ {
+ for j := i + 1; j < l; j++ {
+ if (hrs[i] + hrs[j]).isCompleteDay() {
+ c++
+ }
+ }
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input hours
+ output int
+ }{
+ {hours{12, 12, 30, 24, 24}, 2},
+ {hours{72, 48, 24, 5}, 3},
+ {hours{12, 18, 24}, 0},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.countCompleteDayPair(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-276/pokgopun/go/ch-2.go b/challenge-276/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..38f9f25037
--- /dev/null
+++ b/challenge-276/pokgopun/go/ch-2.go
@@ -0,0 +1,105 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-276/
+/*#
+
+Task 2: Maximum Frequency
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers, @ints.
+
+ Write a script to return the total number of elements in the given
+ array which have the highest frequency.
+
+Example 1
+
+Input: @ints = (1, 2, 2, 4, 1, 5)
+Ouput: 4
+
+The maximum frequency is 2.
+The elements 1 and 2 has the maximum frequency.
+
+Example 2
+
+Input: @ints = (1, 2, 3, 4, 5)
+Ouput: 5
+
+The maximum frequency is 1.
+The elements 1, 2, 3, 4 and 5 has the maximum frequency.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 7th July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type intCount struct {
+ int, count int
+}
+
+type ints []int
+
+func (is ints) uniqInts() ints {
+ seen := make(map[int]bool)
+ var s ints
+ for _, v := range is {
+ if seen[v] {
+ continue
+ }
+ seen[v] = true
+ s = append(s, v)
+ }
+ return s
+}
+
+func (is ints) intCounts() []intCount {
+ var s []intCount
+ for _, v := range is.uniqInts() {
+ c := 0
+ for _, e := range is {
+ if e == v {
+ c++
+ }
+ }
+ s = append(s, intCount{v, c})
+ }
+ return s
+}
+
+func (is ints) maxFreq() int {
+ intCounts := is.intCounts()
+ mx := intCounts[0].count
+ for _, v := range intCounts[1:] {
+ mx = max(mx, v.count)
+ }
+ c := 0
+ for _, v := range intCounts {
+ if v.count == mx {
+ c += mx
+ }
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{1, 2, 2, 4, 1, 5}, 4},
+ {ints{1, 2, 3, 4, 5}, 5},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.maxFreq(), data.output)) // blank if ok, otherwise show the difference
+ }
+}