aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Firkins <michael@firkins>2023-09-19 15:54:20 +1000
committerMichael Firkins <michael@firkins>2023-09-19 15:59:31 +1000
commit6da71e0cb35dca7e75deb5d2fbfc3eb0a84f72f0 (patch)
tree35997f3f6de1815bdd58d7f729b65cfc77a856f0
parent368f5829f6692f35ea4e0baa698d6512b7eb274b (diff)
downloadperlweeklychallenge-club-6da71e0cb35dca7e75deb5d2fbfc3eb0a84f72f0.tar.gz
perlweeklychallenge-club-6da71e0cb35dca7e75deb5d2fbfc3eb0a84f72f0.tar.bz2
perlweeklychallenge-club-6da71e0cb35dca7e75deb5d2fbfc3eb0a84f72f0.zip
pwc235 solution in go
-rw-r--r--challenge-235/pokgopun/go/ch-1.go80
-rw-r--r--challenge-235/pokgopun/go/ch-2.go70
2 files changed, 150 insertions, 0 deletions
diff --git a/challenge-235/pokgopun/go/ch-1.go b/challenge-235/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..0eb71efb8d
--- /dev/null
+++ b/challenge-235/pokgopun/go/ch-1.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-235/
+/*#
+
+Task 1: Remove One
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to find out if removing ONLY one integer makes it
+ strictly increasing order.
+
+Example 1
+
+Input: @ints = (0, 2, 9, 4, 6)
+Output: true
+
+Removing ONLY 9 in the given array makes it strictly increasing order.
+
+Example 2
+
+Input: @ints = (5, 1, 3, 2)
+Output: false
+
+Example 3
+
+Input: @ints = (2, 2, 3)
+Output: true
+
+Task 2: Duplicate Zeros
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import "fmt"
+
+func main() {
+ for _, data := range []struct {
+ input []int
+ output bool
+ }{
+ {[]int{0, 2, 9, 4, 6}, true},
+ {[]int{5, 1, 3, 2}, false},
+ {[]int{2, 2, 3}, true},
+ } {
+ fmt.Println(isRmoSorted(data.input) == data.output)
+ }
+}
+
+func isRmoSorted(s []int) bool {
+ l := len(s)
+ var (
+ first, isSorted bool
+ v int
+ )
+ for i := 0; i < l; i++ {
+ isSorted = true
+ first = true
+ for j := 0; j < l; j++ {
+ if j == i {
+ continue
+ }
+ if first == true {
+ first = false
+ } else {
+ if s[v] > s[j] {
+ isSorted = false
+ break
+ }
+ }
+ v = j
+ }
+ if isSorted == true {
+ return true
+ }
+ }
+ return false
+}
diff --git a/challenge-235/pokgopun/go/ch-2.go b/challenge-235/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..5da191a3c7
--- /dev/null
+++ b/challenge-235/pokgopun/go/ch-2.go
@@ -0,0 +1,70 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-235/
+/*#
+
+Task 2: Duplicate Zeros
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to duplicate each occurrence of ZERO in the given array
+ and shift the remaining to the right but make sure the size of array
+ remain the same.
+
+Example 1
+
+Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0)
+Ouput: (1, 0, 0, 2, 3, 0, 0, 4)
+
+Example 2
+
+Input: @ints = (1, 2, 3)
+Ouput: (1, 2, 3)
+
+Example 3
+
+Input: @ints = (0, 3, 0, 4, 5)
+Ouput: (0, 0, 3, 0, 0)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 24th September
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "slices"
+)
+
+func main() {
+ for _, data := range []struct {
+ input, output []int
+ }{
+ {[]int{1, 0, 2, 3, 0, 4, 5, 0}, []int{1, 0, 0, 2, 3, 0, 0, 4}},
+ {[]int{1, 2, 3}, []int{1, 2, 3}},
+ {[]int{0, 3, 0, 4, 5}, []int{0, 0, 3, 0, 0}},
+ } {
+ fmt.Println(slices.Equal(dupZero(data.input), data.output))
+ }
+}
+
+func dupZero(s []int) []int {
+ l := len(s)
+ i := 0
+ for i < l {
+ if s[i] == 0 {
+ copy(s[i+2:], s[i+1:])
+ s[i+1] = 0
+ i++
+ }
+ i++
+ }
+ return s
+}