diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-31 13:27:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-31 13:27:51 +0100 |
| commit | f7e16a452c3534d99d6061d67c63e06d5ce4cd23 (patch) | |
| tree | 87949d8be3dec516282bcd84614c7de6c0d81452 /challenge-106 | |
| parent | a50ec8bd9f3ea83aa7cbb8a42e60970d68c1a4b6 (diff) | |
| parent | 100676d6d3968544f5a9bdfaedb6c687e11aefb1 (diff) | |
| download | perlweeklychallenge-club-f7e16a452c3534d99d6061d67c63e06d5ce4cd23.tar.gz perlweeklychallenge-club-f7e16a452c3534d99d6061d67c63e06d5ce4cd23.tar.bz2 perlweeklychallenge-club-f7e16a452c3534d99d6061d67c63e06d5ce4cd23.zip | |
Merge pull request #3811 from gramosg/ch106
Challenge 106 (Go)
Diffstat (limited to 'challenge-106')
| -rw-r--r-- | challenge-106/guillermo-ramos/go/ch-1.go | 15 | ||||
| -rw-r--r-- | challenge-106/guillermo-ramos/go/ch-2.go | 59 | ||||
| -rw-r--r-- | challenge-106/guillermo-ramos/go/ch106_test.go | 42 | ||||
| -rw-r--r-- | challenge-106/guillermo-ramos/go/go.mod | 3 | ||||
| -rwxr-xr-x | challenge-106/guillermo-ramos/go/run_tests.sh | 3 |
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 |
