aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2022-07-17 11:16:07 +0700
committerMichael Manring <michael@manring>2022-07-17 11:16:07 +0700
commit65477bd7fedde139fa99e0552c82fbda9c1cf5e2 (patch)
tree4fca86165925655125dc2128db5ddeb84c33df47
parent753d19e9546dfd295e36874df9fb823c0e87a45c (diff)
downloadperlweeklychallenge-club-65477bd7fedde139fa99e0552c82fbda9c1cf5e2.tar.gz
perlweeklychallenge-club-65477bd7fedde139fa99e0552c82fbda9c1cf5e2.tar.bz2
perlweeklychallenge-club-65477bd7fedde139fa99e0552c82fbda9c1cf5e2.zip
pwc140 solution in go
-rw-r--r--challenge-140/pokgopun/README1
-rw-r--r--challenge-140/pokgopun/go/ch-1.go101
-rw-r--r--challenge-140/pokgopun/go/ch-2.go106
3 files changed, 208 insertions, 0 deletions
diff --git a/challenge-140/pokgopun/README b/challenge-140/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-140/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-140/pokgopun/go/ch-1.go b/challenge-140/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..9e1e8fd94e
--- /dev/null
+++ b/challenge-140/pokgopun/go/ch-1.go
@@ -0,0 +1,101 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-140/
+TASK #1 › Add Binary
+Submitted by: Mohammad S Anwar
+You are given two decimal-coded binary numbers, $a and $b.
+
+Write a script to simulate the addition of the given binary numbers.
+
+The script should simulate something like $a + $b. (operator overloading)
+
+Example 1
+Input: $a = 11; $b = 1;
+Output: 100
+Example 2
+Input: $a = 101; $b = 1;
+Output: 110
+Example 3
+Input: $a = 100; $b = 11;
+Output: 111
+*/
+package main
+
+import (
+ "bufio"
+ "errors"
+ "os"
+ "sort"
+ "strings"
+)
+
+func main() {
+ w := bufio.NewWriter(os.Stdout)
+ sample := make([][2]string, 1)
+ if len(os.Args) > 2 {
+ sample[0] = [2]string{os.Args[1], os.Args[2]}
+ } else {
+ sample = [][2]string{
+ [2]string{"11", "1"},
+ [2]string{"101", "1"},
+ [2]string{"100", "11"},
+ }
+ }
+ var a, b string
+ for _, v := range sample {
+ a, b = v[0], v[1]
+ w.WriteString("Input: a = " + a + "; b = " + b + "\nOutput: ")
+ res, err := Add(a, b)
+ if err != nil {
+ w.WriteString(err.Error())
+ } else {
+ w.WriteString(res)
+ }
+ w.WriteString("\n\n")
+ }
+ w.Flush()
+}
+func Add(a, b string) (r string, err error) {
+ for _, v := range a + b {
+ if v != '1' && v != '0' {
+ return "", errors.New("invalid binary string")
+ }
+ }
+ for _, v := range []*string{&a, &b} {
+ *v = Reverse(strings.TrimLeft(*v, "0"))
+ }
+ max := MaxLen(a, b)
+ var count1 int
+ for i := 0; i < max+count1; i++ {
+ for _, str := range []string{a, b} {
+ if i < len(str) {
+ if str[i] == '1' {
+ count1++
+ }
+ }
+ }
+ if count1%2 == 0 {
+ r += "0"
+ } else {
+ r += "1"
+ }
+ count1 /= 2
+ }
+ return Reverse(r), nil
+}
+func Reverse(str string) string {
+ s := []byte(str)
+ sort.SliceStable(s, func(i, j int) bool {
+ return true
+ })
+ return string(s)
+}
+func MaxLen(s ...string) (max int) {
+ l := len(s[0])
+ max = l
+ for _, v := range s[1:] {
+ l := len(v)
+ if max < l {
+ max = l
+ }
+ }
+ return max
+}
diff --git a/challenge-140/pokgopun/go/ch-2.go b/challenge-140/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..bd691d106c
--- /dev/null
+++ b/challenge-140/pokgopun/go/ch-2.go
@@ -0,0 +1,106 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-140/
+TASK #2 › Multiplication Table
+Submitted by: Mohammad S Anwar
+You are given 3 positive integers, $i, $j and $k.
+
+Write a script to print the $kth element in the sorted multiplication table of $i and $j.
+
+Example 1
+Input: $i = 2; $j = 3; $k = 4
+Output: 3
+
+Since the multiplication of 2 x 3 is as below:
+
+ 1 2 3
+ 2 4 6
+
+The sorted multiplication table:
+
+ 1 2 2 3 4 6
+
+Now the 4th element in the table is "3".
+Example 2
+Input: $i = 3; $j = 3; $k = 6
+Output: 4
+
+Since the multiplication of 3 x 3 is as below:
+
+ 1 2 3
+ 2 4 6
+ 3 6 9
+
+The sorted multiplication table:
+
+ 1 2 2 3 3 4 6 6 9
+
+Now the 6th element in the table is "4".
+*/
+package main
+
+import (
+ "errors"
+ "fmt"
+ "log"
+ "os"
+ "sort"
+ "strings"
+)
+
+func main() {
+ s := make([][3]int, 1)
+ i, j, k := &s[0][0], &s[0][1], &s[0][2]
+ _, err := fmt.Sscanf(strings.Join(os.Args[1:], " "), "%d %d %d", i, j, k)
+ if err != nil {
+ s = [][3]int{
+ [3]int{2, 3, 4},
+ [3]int{3, 3, 6},
+ }
+ }
+ for _, v := range s {
+ mt, err := NewMulTab(v[0], v[1], v[2])
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(mt)
+ }
+}
+
+type mulTab struct {
+ i, j, k, kth int
+ desc string
+}
+
+func NewMulTab(i, j, k int) (mulTab, error) {
+ if i < 1 || j < 1 || k > i*j {
+ return mulTab{}, errors.New("i, j must be greater than 1 while k must not be greater than i*j")
+ }
+ s := []int{}
+ var (
+ v int
+ desc string
+ )
+ desc += fmt.Sprintf(`
+Since the multiplication of %d x %d is as below:
+
+`, i, j)
+ for x := 1; x <= i; x++ {
+ for y := 1; y <= j; y++ {
+ v = x * y
+ desc += fmt.Sprint(" ", v)
+ s = append(s, v)
+ }
+ desc += "\n"
+ }
+ sort.Ints(s)
+ desc += fmt.Sprintf(`
+The sorted multiplication table:
+
+%v
+
+Now element#%d in the table is "%d".
+`, s, k, s[k-1])
+ return mulTab{i, j, k, s[k-1], desc}, nil
+}
+func (mt mulTab) String() string {
+ return fmt.Sprintf("Input: i = %d, j = %d, k = %d\nOutput: %d\n", mt.i, mt.j, mt.k, mt.kth) + mt.desc
+}