From abe5b1817a10ba5fbb9be518ec1cb839fa834f64 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Sun, 6 Aug 2023 18:07:10 +1000 Subject: pwc228 solution in go --- challenge-228/pokgopun/go/ch-1.go | 61 +++++++++++++++++++++++++++++++++ challenge-228/pokgopun/go/ch-2.go | 71 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 challenge-228/pokgopun/go/ch-1.go create mode 100644 challenge-228/pokgopun/go/ch-2.go diff --git a/challenge-228/pokgopun/go/ch-1.go b/challenge-228/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..d6e5868e85 --- /dev/null +++ b/challenge-228/pokgopun/go/ch-1.go @@ -0,0 +1,61 @@ +// https://theweeklychallenge.org/blog/perl-weekly-challenge-228/ +/* +Task 1: Unique Sum +Submitted by: Mohammad S Anwar +You are given an array of integers. + +Write a script to find out the sum of unique elements in the given array. + +Example 1 +Input: @int = (2, 1, 3, 2) +Output: 4 + +In the given array we have 2 unique elements (1, 3). +Example 2 +Input: @int = (1, 1, 1, 1) +Output: 0 + +In the given array no unique element found. +Example 3 +Input: @int = (2, 1, 3, 4) +Output: 10 + +In the given array every element is unique. +*/ + +package main + +import "fmt" + +func main() { + var su SumUniq + for _, data := range []struct { + input []int + output int + }{ + {[]int{2, 1, 3, 2}, 4}, + {[]int{1, 1, 1, 1}, 0}, + {[]int{2, 1, 3, 4}, 10}, + } { + fmt.Println(su.Cal(data.input) == data.output) + } +} + +type SumUniq struct { + m map[int]int + k, v int + val int +} + +func (su SumUniq) Cal(s []int) int { + su.m = make(map[int]int, len(s)) + for _, su.v = range s { + su.m[su.v]++ + } + for su.k, su.v = range su.m { + if su.v == 1 { + su.val += su.k + } + } + return su.val +} diff --git a/challenge-228/pokgopun/go/ch-2.go b/challenge-228/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9ba2668ba4 --- /dev/null +++ b/challenge-228/pokgopun/go/ch-2.go @@ -0,0 +1,71 @@ +// https://theweeklychallenge.org/blog/perl-weekly-challenge-228/ +/* +Task 2: Empty Array +Submitted by: Mohammad S Anwar +You are given an array of integers in which all elements are unique. + +Write a script to perform the following operations until the array is empty and return the total count of operations. + + +If the first element is the smallest then remove it otherwise move it to the end. + +Example 1 +Input: @int = (3, 4, 2) +Ouput: 5 + +Operation 1: move 3 to the end: (4, 2, 3) +Operation 2: move 4 to the end: (2, 3, 4) +Operation 3: remove element 2: (3, 4) +Operation 4: remove element 3: (4) +Operation 5: remove element 4: () +Example 2 +Input: @int = (1, 2, 3) +Ouput: 3 + +Operation 1: remove element 1: (2, 3) +Operation 2: remove element 2: (3) +Operation 3: remove element 3: () +*/ + +package main + +import "fmt" + +func main() { + for _, data := range []struct { + input []int + output int + }{ + {[]int{3, 4, 2}, 5}, + {[]int{1, 2, 3}, 3}, + } { + fmt.Println(CountEmptyOp(data.input).Count() == data.output) + } +} + +type CountEmptyOp []int + +func (ceo CountEmptyOp) min() int { + if len(ceo) == 0 { + return 0 + } + min := ceo[0] + for _, v := range ceo[1:] { + if v < min { + min = v + } + } + return min +} + +func (ceo CountEmptyOp) Count() int { + count := 0 + for len(ceo) > 0 { + count += 1 + if ceo[0] != ceo.min() { + ceo = append(ceo, ceo[0]) + } + ceo = ceo[1:] + } + return count +} -- cgit