summary refs log tree commit diff stats
path: root/go/raindrops/raindrops.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/raindrops/raindrops.go')
-rw-r--r--go/raindrops/raindrops.go39
1 files changed, 39 insertions, 0 deletions
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
+}