aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-002/pokgopun/README1
-rw-r--r--challenge-002/pokgopun/go/ch-1.go36
-rw-r--r--challenge-002/pokgopun/go/ch-2.go50
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-002/pokgopun/README b/challenge-002/pokgopun/README
new file mode 100644
index 0000000000..33dfd303a4
--- /dev/null
+++ b/challenge-002/pokgopun/README
@@ -0,0 +1 @@
+Solution by PokGoPun
diff --git a/challenge-002/pokgopun/go/ch-1.go b/challenge-002/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..080fb3cec9
--- /dev/null
+++ b/challenge-002/pokgopun/go/ch-1.go
@@ -0,0 +1,36 @@
+// Write a script or one-liner to remove leading zeros from positive numbers.
+// Assume number can be float and zero
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+ "strings"
+)
+
+func main() {
+ if len(os.Args) > 1 {
+ w := bufio.NewWriter(os.Stdout)
+ var (
+ f float64
+ err error
+ )
+ for _, v := range os.Args[1:] {
+ //fmt.Println(strings.TrimLeft(v, "0"))
+ f, err = strconv.ParseFloat(v, 64)
+ if err != nil || f < 0 {
+ log.Fatal("Not a positive number")
+ }
+ if f != 0 {
+ v = strings.TrimLeft(v, "0")
+ }
+ w.WriteString(v + "\n")
+ }
+ w.Flush()
+ } else {
+ fmt.Println("Enter number to have its leading zeros removed")
+ }
+}
diff --git a/challenge-002/pokgopun/go/ch-2.go b/challenge-002/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..cc68633dda
--- /dev/null
+++ b/challenge-002/pokgopun/go/ch-2.go
@@ -0,0 +1,50 @@
+// Write a script that can convert integers to and from a base35 representation, using the characters 0-9 and A-Y.
+package main
+
+import (
+ "errors"
+ "fmt"
+ "log"
+ "math/big"
+ "os"
+ "strconv"
+)
+
+func main() {
+ guide := `Example:
+To convert from base10to35, go run ch-2.go 10to35 1653676305073
+pokgopun
+To convert from base35to10, go run ch-2.go 35to10 PokGoPun
+1653676305073`
+ if len(os.Args) < 2 {
+ fmt.Println(guide)
+ } else {
+ switch os.Args[1] {
+ case "10to35":
+ n, err := strconv.Atoi(os.Args[2])
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(base10To35(n))
+ case "35to10":
+ n, err := base35To10(os.Args[2])
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(n)
+ default:
+ fmt.Println(guide)
+ }
+ }
+}
+func base10To35(n int) string {
+ return big.NewInt(int64(n)).Text(35)
+}
+func base35To10(str string) (int, error) {
+ i := new(big.Int)
+ j, ok := i.SetString(str, 35)
+ if ok {
+ return int(j.Int64()), nil
+ }
+ return 0, errors.New("conversion failed")
+}