aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2022-07-21 12:41:53 +0700
committerMichael Manring <michael@manring>2022-07-21 12:41:53 +0700
commit70b7d2c73c58841e449ca9fa6f1c4cb14da052e0 (patch)
treed66435c27f30937b0e7384f17a5f1ca8fc44d406
parentb563ddfc31ef87a31dec5bf04ebbcf305d2e5f0a (diff)
downloadperlweeklychallenge-club-70b7d2c73c58841e449ca9fa6f1c4cb14da052e0.tar.gz
perlweeklychallenge-club-70b7d2c73c58841e449ca9fa6f1c4cb14da052e0.tar.bz2
perlweeklychallenge-club-70b7d2c73c58841e449ca9fa6f1c4cb14da052e0.zip
pwc047 solution in go
-rw-r--r--challenge-047/pokgopun/README1
-rw-r--r--challenge-047/pokgopun/go/ch-1.go71
-rw-r--r--challenge-047/pokgopun/go/ch-2.go29
3 files changed, 101 insertions, 0 deletions
diff --git a/challenge-047/pokgopun/README b/challenge-047/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-047/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-047/pokgopun/go/ch-1.go b/challenge-047/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..b7f2853139
--- /dev/null
+++ b/challenge-047/pokgopun/go/ch-1.go
@@ -0,0 +1,71 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-047/
+TASK #1
+Roman Calculator
+Write a script that accepts two roman numbers and operation. It should then perform the operation on the give roman numbers and print the result.
+
+For example,
+
+perl ch-1.pl V + VI
+It should print
+
+XI
+*/
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strings"
+
+ "github.com/pokgopun/go/roman"
+)
+
+func main() {
+ guide := `
+Usage:
+ %cmd% %RomanString1% %op% %RomanString2%
+
+ supported ops are + and -
+ result less than 1 or greater than 3999 will get error as it cannot be displayed in standard roman form
+
+ MMXXII + DXLIII
+ MMDLXV
+
+ MMDLXV - DXLIII
+ MMXXII`
+ var str1, op, str2 string
+ _, err := fmt.Sscanf(strings.Join(os.Args[1:], " "), "%s %s %s", &str1, &op, &str2)
+ if err != nil {
+ fmt.Println(guide)
+ os.Exit(0)
+ }
+ roman := roman.NewRoman()
+ r1, err1 := roman.ToDec(str1)
+ r2, err2 := roman.ToDec(str2)
+ if err1 != nil {
+ fmt.Println("RomanString1:", err1)
+ }
+ if err2 != nil {
+ fmt.Println("RomanString2:", err2)
+ }
+ if err1 != nil || err2 != nil {
+ os.Exit(1)
+ }
+ switch op {
+ case "+":
+ r, err := roman.FromDec(r1 + r2)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(r)
+ case "-":
+ r, err := roman.FromDec(r1 - r2)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(r)
+ default:
+ log.Fatal("invalid ops: supported ops are +,-,*")
+ }
+}
diff --git a/challenge-047/pokgopun/go/ch-2.go b/challenge-047/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..5479cad0d4
--- /dev/null
+++ b/challenge-047/pokgopun/go/ch-2.go
@@ -0,0 +1,29 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-047/
+TASK #2
+Gapful Number
+Write a script to print first 20 Gapful Numbers greater than or equal to 100. Please check out the page for more information about Gapful Numbers.
+*/
+package main
+
+import (
+ "bufio"
+ "os"
+ "strconv"
+
+ "github.com/pokgopun/go/gapful"
+)
+
+func main() {
+ n := 20
+ w := bufio.NewWriter(os.Stdout)
+ for i := 100; i < 500; i++ {
+ if gapful.IsGapful(i) {
+ w.WriteString(strconv.Itoa(i) + "\n")
+ n--
+ if n == 0 {
+ break
+ }
+ }
+ }
+ w.Flush()
+}