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.
The rules of raindrops
are that if a given number:
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.
Look for a stub file having the name raindrops.go and place your solution code in that file.
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.
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.
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
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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"},
}
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)
}
}
}
package raindrops
import "strconv"
// Convert returns a string generated based on the raindrops rules below.
// 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.
func Convert(n int) string {
if !isDivisibleByPlingPlangPlong(n) {
return strconv.Itoa(n)
}
var result string
if isPling(n) {
result += "Pling"
}
if isPlang(n) {
result += "Plang"
}
if isPlong(n) {
result += "Plong"
}
return result
}
func isDivisibleByPlingPlangPlong(n int) bool {
return isPling(n) || isPlang(n) || isPlong(n)
}
func isPling(n int) bool {
return n%3 == 0
}
func isPlang(n int) bool {
return n%5 == 0
}
func isPlong(n int) bool {
return n%7 == 0
}
A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments