Avatar of suic86

suic86's solution

to Raindrops in the Scala Track

Published at Oct 13 2019 · 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".

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

Please see the learning and installation pages if you need any help.

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.

RaindropsTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class RaindropsTest extends FunSuite with Matchers {
  
  test("the sound for 1 is 1") {
    Raindrops.convert(1) should be ("1")
  }

  test("the sound for 3 is Pling") {
    pending
    Raindrops.convert(3) should be ("Pling")
  }

  test("the sound for 5 is Plang") {
    pending
    Raindrops.convert(5) should be ("Plang")
  }

  test("the sound for 7 is Plong") {
    pending
    Raindrops.convert(7) should be ("Plong")
  }

  test("the sound for 6 is Pling as it has a factor 3") {
    pending
    Raindrops.convert(6) should be ("Pling")
  }

  test("2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base") {
    pending
    Raindrops.convert(8) should be ("8")
  }

  test("the sound for 9 is Pling as it has a factor 3") {
    pending
    Raindrops.convert(9) should be ("Pling")
  }

  test("the sound for 10 is Plang as it has a factor 5") {
    pending
    Raindrops.convert(10) should be ("Plang")
  }

  test("the sound for 14 is Plong as it has a factor of 7") {
    pending
    Raindrops.convert(14) should be ("Plong")
  }

  test("the sound for 15 is PlingPlang as it has factors 3 and 5") {
    pending
    Raindrops.convert(15) should be ("PlingPlang")
  }

  test("the sound for 21 is PlingPlong as it has factors 3 and 7") {
    pending
    Raindrops.convert(21) should be ("PlingPlong")
  }

  test("the sound for 25 is Plang as it has a factor 5") {
    pending
    Raindrops.convert(25) should be ("Plang")
  }

  test("the sound for 27 is Pling as it has a factor 3") {
    pending
    Raindrops.convert(27) should be ("Pling")
  }

  test("the sound for 35 is PlangPlong as it has factors 5 and 7") {
    pending
    Raindrops.convert(35) should be ("PlangPlong")
  }

  test("the sound for 49 is Plong as it has a factor 7") {
    pending
    Raindrops.convert(49) should be ("Plong")
  }

  test("the sound for 52 is 52") {
    pending
    Raindrops.convert(52) should be ("52")
  }

  test("the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7") {
    pending
    Raindrops.convert(105) should be ("PlingPlangPlong")
  }

  test("the sound for 3125 is Plang as it has a factor 5") {
    pending
    Raindrops.convert(3125) should be ("Plang")
  }
}
object Raindrops {
  def convert(n: Int): String = {
    var result = ""
    if (n % 3 == 0)
      result += "Pling"
    if (n % 5 == 0)
      result += "Plang"
    if (n % 7 == 0)
      result += "Plong"
    if (result == "") n.toString else result
  }
}

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?