aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-04-30 13:59:14 +1000
committerMichael Manring <michael@manring>2024-04-30 14:02:51 +1000
commit5cb72e307c9bd890b5a917dc8cfaa4fbf05497dc (patch)
tree214ec576d28ebc40eb79bdf822385a0510235e37
parent4254fe8fc9106aae385c5d0bc923e4c216c5dc6a (diff)
downloadperlweeklychallenge-club-5cb72e307c9bd890b5a917dc8cfaa4fbf05497dc.tar.gz
perlweeklychallenge-club-5cb72e307c9bd890b5a917dc8cfaa4fbf05497dc.tar.bz2
perlweeklychallenge-club-5cb72e307c9bd890b5a917dc8cfaa4fbf05497dc.zip
pwc267 solution in go
-rw-r--r--challenge-267/pokgopun/go/ch-1.go74
-rw-r--r--challenge-267/pokgopun/go/ch-2.go88
2 files changed, 162 insertions, 0 deletions
diff --git a/challenge-267/pokgopun/go/ch-1.go b/challenge-267/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..d9fe7d7027
--- /dev/null
+++ b/challenge-267/pokgopun/go/ch-1.go
@@ -0,0 +1,74 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
+/*#
+
+Task 1: Product Sign
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of @ints.
+
+ Write a script to find the sign of product of all integers in the given
+ array. The sign is 1 if the product is positive, -1 if the product is
+ negative and 0 if product is zero.
+
+Example 1
+
+Input: @ints = (-1, -2, -3, -4, 3, 2, 1)
+Output: 1
+
+The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0
+
+Example 2
+
+Input: @ints = (1, 2, 0, -2, -1)
+Output: 0
+
+The product 1 x 2 x 0 x -2 x -1 => 0
+
+Example 3
+
+Input: @ints = (-1, -1, 1, -1, 2)
+Output: -1
+
+The product -1 x -1 x 1 x -1 x 2 => -2 < 0
+
+Task 2: Line Counts
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) productSign() int {
+ res := 1
+ for _, v := range is {
+ if v == 0 {
+ return 0
+ } else if v < 0 {
+ res *= -1
+ }
+ }
+ return res
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{-1, -2, -3, -4, 3, 2, 1}, 1},
+ {ints{1, 2, 0, -2, -1}, 0},
+ {ints{-1, -1, 1, -1, 2}, -1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.productSign(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-267/pokgopun/go/ch-2.go b/challenge-267/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..054ff6b7f4
--- /dev/null
+++ b/challenge-267/pokgopun/go/ch-2.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
+/*#
+
+Task 2: Line Counts
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, and a 26-items array @widths containing
+ the width of each character from a to z.
+
+ Write a script to find out the number of lines and the width of the
+ last line needed to display the given string, assuming you can only fit
+ 100 width units on a line.
+
+Example 1
+
+Input: $str = "abcdefghijklmnopqrstuvwxyz"
+ @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
+,10,10,10,10,10)
+Output: (3, 60)
+
+Line 1: abcdefghij (100 pixels)
+Line 2: klmnopqrst (100 pixels)
+Line 3: uvwxyz (60 pixels)
+
+Example 2
+
+Input: $str = "bbbcccdddaaa"
+ @widths = (4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
+10,10,10,10,10)
+Output: (2, 4)
+
+Line 1: bbbcccdddaa (98 pixels)
+Line 2: a (4 pixels)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 5th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+const lim int = 100
+
+type Answer struct {
+ Lc, L int
+}
+
+type text string
+
+func (txt text) lc(widths []int) Answer {
+ var l, lc, w int
+ lc = 1
+ var b byte
+ for _, b = range []byte(txt) {
+ w = widths[b-97]
+ l += w
+ if l > lim {
+ lc++
+ l = w
+ }
+ }
+ return Answer{lc, l}
+}
+
+func main() {
+ for _, data := range []struct {
+ str text
+ widths []int
+ ans Answer
+ }{
+ {"abcdefghijklmnopqrstuvwxyz", []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, Answer{3, 60}},
+ {"bbbcccdddaaa", []int{4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, Answer{2, 4}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.str.lc(data.widths), data.ans)) // blank if ok,, otherwise show the difference
+ }
+}