aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-10-03 21:58:53 +1000
committerMyoungjin JEON <jeongoon@gmail.com>2020-10-03 21:58:53 +1000
commit2f882cfb91c2af0320e74cd678f4a102c85f0d0f (patch)
treee041172a1dc74701f898b886e281ff8495ed77c5
parent5a4324d11f6e1005922965c46931f459f01c91d5 (diff)
downloadperlweeklychallenge-club-2f882cfb91c2af0320e74cd678f4a102c85f0d0f.tar.gz
perlweeklychallenge-club-2f882cfb91c2af0320e74cd678f4a102c85f0d0f.tar.bz2
perlweeklychallenge-club-2f882cfb91c2af0320e74cd678f4a102c85f0d0f.zip
[ch-080/jeongoon] Go solution added.
-rw-r--r--challenge-080/jeongoon/go/ch-1.go74
-rw-r--r--challenge-080/jeongoon/go/ch-2.go48
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-080/jeongoon/go/ch-1.go b/challenge-080/jeongoon/go/ch-1.go
new file mode 100644
index 0000000000..d78b813325
--- /dev/null
+++ b/challenge-080/jeongoon/go/ch-1.go
@@ -0,0 +1,74 @@
+package main
+
+/* Ref:
+https://tour.golang.org/flowcontrol/1
+https://www.davidkaya.com/sets-in-golang/
+https://golangdocs.com/converting-string-to-integer-in-golang
+*/
+
+/* test with:
+go run ch-1.go -2 3 5 2 1
+# > 4
+*/
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+)
+
+var exists = struct{}{}
+
+type intSet struct {
+ m map[int]struct{}
+}
+
+func NewIntSet() *intSet {
+ s := &intSet{}
+ s.m = make(map[int]struct{})
+ return s
+}
+
+func (s *intSet) Add(value int) {
+ s.m[value] = exists
+}
+
+func (s *intSet) Remove(value int) {
+ delete(s.m, value)
+}
+
+func (s *intSet) Contains(value int) bool {
+ _, c := s.m[value]
+ return c
+}
+
+func main() {
+ set := NewIntSet()
+ max := 0
+ args := os.Args[1:] // ignore programme name
+
+ for _, it := range args {
+ int_val, err := strconv.Atoi( it )
+ if err != nil {
+ fmt.Fprint( os.Stderr, err )
+ fmt.Fprintln( os.Stderr, ": skipped" )
+ }
+ if int_val < 0 {
+ fmt.Fprintln( os.Stderr,
+ "negative values are unnecessary: skipped" )
+ } else {
+ set.Add( int_val )
+ if max < int_val {
+ max = int_val
+ }
+ }
+ }
+ max++;
+
+ for maybe_missing := 1; maybe_missing <= max; maybe_missing++ {
+ if ! set.Contains( maybe_missing ) {
+ fmt.Println( maybe_missing )
+ break
+ }
+ }
+}
diff --git a/challenge-080/jeongoon/go/ch-2.go b/challenge-080/jeongoon/go/ch-2.go
new file mode 100644
index 0000000000..3a78e3fe1c
--- /dev/null
+++ b/challenge-080/jeongoon/go/ch-2.go
@@ -0,0 +1,48 @@
+package main
+
+/* Ref:
+https://golangdocs.com/slices-in-golang
+*/
+
+/* test with:
+go run ch-2.go 1 5 4 2
+# > 6
+*/
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+)
+
+func main() {
+ args := os.Args[1:] // ignore programme name
+ rank := []int{}
+
+ for _, it := range args {
+ int_val, err := strconv.Atoi( it )
+ if err != nil {
+ fmt.Fprint( os.Stderr, err )
+ fmt.Fprintln( os.Stderr, ": skipped" )
+ continue
+ }
+ rank = append( rank, int_val )
+ }
+ rank_len := len(rank)
+ if rank_len == 0 {
+ fmt.Println("0 as no useuful data")
+ os.Exit(1)
+ }
+
+ left := rank[0:rank_len -1]
+ right := rank[1:]
+
+ candies := rank_len
+
+ for i := 0; i < rank_len -2; i++ {
+ if left[i] != right[i] {
+ candies++
+ }
+ }
+ fmt.Println(candies)
+}