aboutsummaryrefslogtreecommitdiff
path: root/challenge-160
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2022-04-11 14:36:00 +0700
committerMichael Manring <michael@manring>2022-04-11 14:36:00 +0700
commit9a511d6f71f8647b8320ecec5ed54a7b3e7c0488 (patch)
tree1f388e20ed144ea83024dcbdb2765e98070058ff /challenge-160
parent635f81890b1d36fd472afbe94df2c7cb0a289d6e (diff)
downloadperlweeklychallenge-club-9a511d6f71f8647b8320ecec5ed54a7b3e7c0488.tar.gz
perlweeklychallenge-club-9a511d6f71f8647b8320ecec5ed54a7b3e7c0488.tar.bz2
perlweeklychallenge-club-9a511d6f71f8647b8320ecec5ed54a7b3e7c0488.zip
pwc160 solution in go
Diffstat (limited to 'challenge-160')
-rw-r--r--challenge-160/pokgopun/go/ch-1.go61
-rw-r--r--challenge-160/pokgopun/go/ch-2.go51
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-160/pokgopun/go/ch-1.go b/challenge-160/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..f4a62114de
--- /dev/null
+++ b/challenge-160/pokgopun/go/ch-1.go
@@ -0,0 +1,61 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "regexp"
+ "strconv"
+ "strings"
+)
+
+func main() {
+ m := map[int]string{
+ 1: "one",
+ 2: "two",
+ 3: "three",
+ 4: "four",
+ 5: "five",
+ 6: "six",
+ 7: "seven",
+ 8: "eight",
+ 9: "nine",
+ }
+ //fmt.Println(m)
+ re := regexp.MustCompile(`\S+$`)
+ var num []int
+ if len(os.Args) > 1 {
+ num = argInts()
+ } else {
+ num = []int{5, 7, 6}
+ }
+ for _, n := range num {
+ fmt.Println("Input: $n =", n)
+ str := m[n]
+ for {
+ r := re.FindString(str)
+ if r == "four" {
+ str += " is magic"
+ break
+ } else {
+ nxt := m[len(r)]
+ str += " is " + nxt + ", " + nxt
+ r = str
+
+ }
+ }
+ fmt.Printf("Output: %v.\n\n", strings.Title(str[:1])+str[1:])
+ }
+}
+func argInts() (s []int) {
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, i)
+ }
+ }
+ return s
+}
diff --git a/challenge-160/pokgopun/go/ch-2.go b/challenge-160/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..e8a953980c
--- /dev/null
+++ b/challenge-160/pokgopun/go/ch-2.go
@@ -0,0 +1,51 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ var num [][]int
+ if len(os.Args) > 3 {
+ num = [][]int{argInts()}
+ } else {
+ num = [][]int{
+ []int{1, 3, 5, 7, 9},
+ []int{1, 2, 3, 4, 5},
+ []int{2, 4, 2},
+ }
+ }
+ for _, n := range num {
+ fmt.Println("Input: @n =", n)
+ o := -1
+ for i := 1; i < len(n)-1; i++ {
+ if sum(n[:i]) == sum(n[i+1:]) {
+ o = i
+ break
+ }
+ }
+ fmt.Printf("Output: %v\n\n", o)
+ }
+}
+func sum(s []int) (r int) {
+ for _, v := range s {
+ r += v
+ }
+ return r
+}
+
+func argInts() (s []int) {
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, i)
+ }
+ }
+ return s
+}