summaryrefslogtreecommitdiff
path: root/go/raindrops
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-25 03:13:39 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-25 03:13:39 +0200
commit509c5063d66e8bbef4ec1def1c99c318be51aceb (patch)
treeafc811c4781a4e317043e2a0237499defc168044 /go/raindrops
Initial import
Diffstat (limited to 'go/raindrops')
-rw-r--r--go/raindrops/README.md37
-rw-r--r--go/raindrops/cases_test.go25
-rw-r--r--go/raindrops/raindrops.go21
-rw-r--r--go/raindrops/raindrops_test.go25
4 files changed, 108 insertions, 0 deletions
diff --git a/go/raindrops/README.md b/go/raindrops/README.md
new file mode 100644
index 0000000..7cd1285
--- /dev/null
+++ b/go/raindrops/README.md
@@ -0,0 +1,37 @@
+# Raindrops
+
+Write a program that converts a number to a string, the contents of which depends on the number's factors.
+
+- If the number contains 3 as a factor, output 'Pling'.
+- If the number contains 5 as a factor, output 'Plang'.
+- If the number contains 7 as a factor, output 'Plong'.
+- If the number does not contain 3, 5, or 7 as a factor,
+ just pass the number's digits straight through.
+
+## Examples
+
+- 28's prime-factorization is 2, 2, 7.
+ - In raindrop-speak, this would be a simple "Plong".
+- 1755 prime-factorization is 3, 3, 3, 5, 13.
+ - In raindrop-speak, this would be a "PlingPlang".
+- The prime factors of 34 are 2 and 17.
+ - Raindrop-speak doesn't know what to make of that,
+ so it just goes with the straightforward "34".
+
+To run the tests simply run the command `go test` in the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `-bench`
+flag:
+
+ go test -bench .
+
+For more detailed info about the Go track see the [help
+page](http://exercism.io/languages/go).
+
+## Source
+
+A variation on a famous interview question intended to weed out potential candidates. [http://jumpstartlab.com](http://jumpstartlab.com)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/raindrops/cases_test.go b/go/raindrops/cases_test.go
new file mode 100644
index 0000000..e551032
--- /dev/null
+++ b/go/raindrops/cases_test.go
@@ -0,0 +1,25 @@
+package raindrops
+
+// Source: exercism/x-common
+// Commit: 3b07e53 Merge pull request #117 from mikeyjcat/add-raindrops-json
+
+var tests = []struct {
+ input int
+ expected string
+}{
+ {1, "1"},
+ {3, "Pling"},
+ {5, "Plang"},
+ {7, "Plong"},
+ {6, "Pling"},
+ {9, "Pling"},
+ {10, "Plang"},
+ {14, "Plong"},
+ {15, "PlingPlang"},
+ {21, "PlingPlong"},
+ {25, "Plang"},
+ {35, "PlangPlong"},
+ {49, "Plong"},
+ {52, "52"},
+ {105, "PlingPlangPlong"},
+}
diff --git a/go/raindrops/raindrops.go b/go/raindrops/raindrops.go
new file mode 100644
index 0000000..d692900
--- /dev/null
+++ b/go/raindrops/raindrops.go
@@ -0,0 +1,21 @@
+package raindrops
+
+import "fmt"
+
+const testVersion = 2
+
+func Convert(n int) (s string) {
+ if n%3 == 0 {
+ s += "Pling"
+ }
+ if n%5 == 0 {
+ s += "Plang"
+ }
+ if n%7 == 0 {
+ s += "Plong"
+ }
+ if s == "" {
+ s = fmt.Sprint(n)
+ }
+ return
+}
diff --git a/go/raindrops/raindrops_test.go b/go/raindrops/raindrops_test.go
new file mode 100644
index 0000000..befa699
--- /dev/null
+++ b/go/raindrops/raindrops_test.go
@@ -0,0 +1,25 @@
+package raindrops
+
+import "testing"
+
+const targetTestVersion = 2
+
+func TestConvert(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
+ }
+ for _, test := range tests {
+ if actual := Convert(test.input); actual != test.expected {
+ t.Errorf("Convert(%d) = %q, expected %q.",
+ test.input, actual, test.expected)
+ }
+ }
+}
+
+func BenchmarkConvert(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, test := range tests {
+ Convert(test.input)
+ }
+ }
+}