summary refs log tree commit diff stats
path: root/go/raindrops/raindrops.go
blob: 1ddc5a7c019900e1a32ee3342abf631686adfcc3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
}