summary refs log tree commit diff stats
path: root/go/raindrops
diff options
context:
space:
mode:
Diffstat (limited to 'go/raindrops')
-rw-r--r--go/raindrops/README.md56
-rw-r--r--go/raindrops/cases_test.go29
-rw-r--r--go/raindrops/go.mod3
-rw-r--r--go/raindrops/raindrops.go39
-rw-r--r--go/raindrops/raindrops_test.go20
5 files changed, 147 insertions, 0 deletions
diff --git a/go/raindrops/README.md b/go/raindrops/README.md
new file mode 100644
index 0000000..71db66d
--- /dev/null
+++ b/go/raindrops/README.md
@@ -0,0 +1,56 @@
+# Raindrops
+
+Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation).
+
+The rules of `raindrops` are that if a given number:
+
+- has 3 as a factor, add 'Pling' to the result.
+- has 5 as a factor, add 'Plang' to the result.
+- has 7 as a factor, add 'Plong' to the result.
+- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number.
+
+## Examples
+
+- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
+- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
+- 34 is not factored by 3, 5, or 7, so the result would be "34".
+
+## Simple Stub
+
+The raindrops.go "stub file" contains only one line with the correct
+package name and nothing more.  This will be the usual pattern for future
+exercises.  You will need to figure out the function signature(s).
+
+One way to figure out the function signature(s) is to look
+at the corresponding \*\_test.go file. It will show the package level
+functions(s) that the test will use to verify the solution.
+
+
+## Coding the solution
+
+Look for a stub file having the name raindrops.go
+and place your solution code in that file.
+
+## Running the tests
+
+To run the tests run the command `go test` from within the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
+flags:
+
+    go test -v --bench . --benchmem
+
+Keep in mind that each reviewer will run benchmarks on a different machine, with
+different specs, so the results from these benchmark tests may vary.
+
+## Further information
+
+For more detailed information about the Go track, including how to get help if
+you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources).
+
+## Source
+
+A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. [https://en.wikipedia.org/wiki/Fizz_buzz](https://en.wikipedia.org/wiki/Fizz_buzz)
+
+## Submitting Incomplete Solutions
+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..ea6becd
--- /dev/null
+++ b/go/raindrops/cases_test.go
@@ -0,0 +1,29 @@
+package raindrops
+
+// Source: exercism/problem-specifications
+// Commit: 99de15d raindrops: apply "input" policy
+// Problem Specifications Version: 1.1.0
+
+var tests = []struct {
+	input    int
+	expected string
+}{
+	{1, "1"},
+	{3, "Pling"},
+	{5, "Plang"},
+	{7, "Plong"},
+	{6, "Pling"},
+	{8, "8"},
+	{9, "Pling"},
+	{10, "Plang"},
+	{14, "Plong"},
+	{15, "PlingPlang"},
+	{21, "PlingPlong"},
+	{25, "Plang"},
+	{27, "Pling"},
+	{35, "PlangPlong"},
+	{49, "Plong"},
+	{52, "52"},
+	{105, "PlingPlangPlong"},
+	{3125, "Plang"},
+}
diff --git a/go/raindrops/go.mod b/go/raindrops/go.mod
new file mode 100644
index 0000000..9454edc
--- /dev/null
+++ b/go/raindrops/go.mod
@@ -0,0 +1,3 @@
+module raindrops
+
+go 1.13
diff --git a/go/raindrops/raindrops.go b/go/raindrops/raindrops.go
new file mode 100644
index 0000000..1ddc5a7
--- /dev/null
+++ b/go/raindrops/raindrops.go
@@ -0,0 +1,39 @@
+// Package raindrops implements Convert.
+package raindrops
+
+import "strconv"
+
+type raindrop struct {
+	div int
+	res string
+}
+
+// Convert returns a string given an integer.
+//
+// - adds "Pling" to the result if the number is divisible by 3.
+//
+// - adds "Plang" to the result if the number is divisible by 5.
+//
+// - adds "Plong" to the result if the number is divisible by 7.
+//
+// - if it's not divisible by 3, 5 or 7 then the digits of given
+// integer is returned.
+func Convert(num int) string {
+	var res string
+	var drops = [3]raindrop{
+		{3, "Pling"},
+		{5, "Plang"},
+		{7, "Plong"},
+	}
+
+	for _, drop := range drops {
+		if num%drop.div == 0 {
+			res += drop.res
+		}
+	}
+	if len(res) == 0 {
+		res = strconv.Itoa(num)
+	}
+
+	return res
+}
diff --git a/go/raindrops/raindrops_test.go b/go/raindrops/raindrops_test.go
new file mode 100644
index 0000000..14f0983
--- /dev/null
+++ b/go/raindrops/raindrops_test.go
@@ -0,0 +1,20 @@
+package raindrops
+
+import "testing"
+
+func TestConvert(t *testing.T) {
+	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)
+		}
+	}
+}