Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Raindrops in the Go Track

Published at Jun 02 2020 · 0 comments
Instructions
Test suite
Solution

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:

  • 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.

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

cases_test.go

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"},
}

raindrops_test.go

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
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?