aboutsummaryrefslogtreecommitdiff
path: root/challenge-106
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-106')
-rw-r--r--challenge-106/guillermo-ramos/go/ch-1.go15
-rw-r--r--challenge-106/guillermo-ramos/go/ch-2.go59
-rw-r--r--challenge-106/guillermo-ramos/go/ch106_test.go42
-rw-r--r--challenge-106/guillermo-ramos/go/go.mod3
-rwxr-xr-xchallenge-106/guillermo-ramos/go/run_tests.sh3
5 files changed, 122 insertions, 0 deletions
diff --git a/challenge-106/guillermo-ramos/go/ch-1.go b/challenge-106/guillermo-ramos/go/ch-1.go
new file mode 100644
index 0000000000..df60fc25a3
--- /dev/null
+++ b/challenge-106/guillermo-ramos/go/ch-1.go
@@ -0,0 +1,15 @@
+package ch106
+
+import "sort"
+
+func MaximumGap(numbers sort.IntSlice) int {
+ sort.Sort(numbers)
+ var maxGap int
+ for i := 0; i < len(numbers)-1; i++ {
+ diff := numbers[i+1] - numbers[i]
+ if diff > maxGap {
+ maxGap = diff
+ }
+ }
+ return maxGap
+}
diff --git a/challenge-106/guillermo-ramos/go/ch-2.go b/challenge-106/guillermo-ramos/go/ch-2.go
new file mode 100644
index 0000000000..14a16a677d
--- /dev/null
+++ b/challenge-106/guillermo-ramos/go/ch-2.go
@@ -0,0 +1,59 @@
+package ch106
+
+import (
+ "fmt"
+ "strconv"
+)
+
+type PerioDec struct {
+ digits []int
+ periodicIdx int
+}
+
+func (d PerioDec) String() (s string) {
+ for i, v := range d.digits {
+ if i == 1 {
+ s = fmt.Sprint(s, ".")
+ }
+ if i == d.periodicIdx {
+ s = fmt.Sprint(s, "(")
+ }
+ s = fmt.Sprint(s, strconv.Itoa(v))
+ }
+ if d.periodicIdx != -1 {
+ s = fmt.Sprint(s, ")")
+ }
+ return
+}
+
+func DecimalString(n, d int) string {
+ dec := PerioDec{periodicIdx: -1}
+ numeratorIdxs := make(map[int]int)
+
+ maxDecimals := 20
+ for i := 0; i <= maxDecimals; i++ {
+ if idx, ok := numeratorIdxs[n]; ok && idx > 0 {
+ dec.periodicIdx = idx
+ break
+ }
+
+ q := 0
+ r := n
+ for r >= d {
+ r = r - d
+ q++
+ }
+
+ numeratorIdxs[n] = i
+ dec.digits = append(dec.digits, q)
+
+ n = r * 10
+
+ if r == 0 {
+ break
+ }
+
+ }
+
+ return dec.String()
+}
diff --git a/challenge-106/guillermo-ramos/go/ch106_test.go b/challenge-106/guillermo-ramos/go/ch106_test.go
new file mode 100644
index 0000000000..ca80e137b7
--- /dev/null
+++ b/challenge-106/guillermo-ramos/go/ch106_test.go
@@ -0,0 +1,42 @@
+package ch106
+
+import "testing"
+
+type MaxGapCase struct {
+ input []int
+ wants int
+}
+
+func TestMaximumGap(t *testing.T) {
+ expected := []MaxGapCase{
+ {[]int{2, 9, 3, 5}, 4},
+ {[]int{1, 3, 8, 2, 0}, 5},
+ {[]int{5}, 0},
+ }
+ for _, c := range expected {
+ res := MaximumGap(c.input)
+ if res != c.wants {
+ t.Errorf("MaximumGap(%v) returned '%d' but expected '%d'",
+ c.input, res, c.wants)
+ }
+ }
+}
+
+type DecStringInput struct {
+ n, d int
+}
+
+func TestDecimalString(t *testing.T) {
+ expected := map[DecStringInput]string{
+ {n: 1, d: 3}: "0.(3)",
+ {n: 1, d: 2}: "0.5",
+ {n: 5, d: 66}: "0.0(75)",
+ }
+ for input, wants := range expected {
+ res := DecimalString(input.n, input.d)
+ if res != wants {
+ t.Errorf("DecimalString(%d, %d) returned '%s' but expected '%s'",
+ input.n, input.d, res, wants)
+ }
+ }
+}
diff --git a/challenge-106/guillermo-ramos/go/go.mod b/challenge-106/guillermo-ramos/go/go.mod
new file mode 100644
index 0000000000..dd59d04ad4
--- /dev/null
+++ b/challenge-106/guillermo-ramos/go/go.mod
@@ -0,0 +1,3 @@
+module example.com/ch106
+
+go 1.16
diff --git a/challenge-106/guillermo-ramos/go/run_tests.sh b/challenge-106/guillermo-ramos/go/run_tests.sh
new file mode 100755
index 0000000000..20e7774e09
--- /dev/null
+++ b/challenge-106/guillermo-ramos/go/run_tests.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+go test -v