🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of epequeno

epequeno's solution

to Raindrops in the Elm Track

Published at Apr 19 2021 · 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".

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!

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 is possible to submit an incomplete solution so you can see how others have completed the exercise.

Tests.elm

module Tests exposing (tests)

import Expect
import Raindrops exposing (raindrops)
import Test exposing (..)


tests : Test
tests =
    describe "Raindrops"
        [ test "1" <|
            \() -> Expect.equal "1" (raindrops 1)
        , skip <|
            test "3" <|
                \() -> Expect.equal "Pling" (raindrops 3)
        , skip <|
            test "5" <|
                \() -> Expect.equal "Plang" (raindrops 5)
        , skip <|
            test "7" <|
                \() -> Expect.equal "Plong" (raindrops 7)
        , skip <|
            test "6" <|
                \() -> Expect.equal "Pling" (raindrops 6)
        , skip <|
            test "9" <|
                \() -> Expect.equal "Pling" (raindrops 9)
        , skip <|
            test "10" <|
                \() -> Expect.equal "Plang" (raindrops 10)
        , skip <|
            test "14" <|
                \() -> Expect.equal "Plong" (raindrops 14)
        , skip <|
            test "15" <|
                \() -> Expect.equal "PlingPlang" (raindrops 15)
        , skip <|
            test "21" <|
                \() -> Expect.equal "PlingPlong" (raindrops 21)
        , skip <|
            test "25" <|
                \() -> Expect.equal "Plang" (raindrops 25)
        , skip <|
            test "35" <|
                \() -> Expect.equal "PlangPlong" (raindrops 35)
        , skip <|
            test "49" <|
                \() -> Expect.equal "Plong" (raindrops 49)
        , skip <|
            test "52" <|
                \() -> Expect.equal "52" (raindrops 52)
        , skip <|
            test "105" <|
                \() -> Expect.equal "PlingPlangPlong" (raindrops 105)
        ]
module Raindrops exposing (raindrops)


raindrops : Int -> String
raindrops number =
    if not (List.any (isDivisible number) [ 3, 5, 7 ]) then
        String.fromInt number

    else
        List.map (makeString number) [ 3, 5, 7 ] |> String.join ""


makeString : Int -> Int -> String
makeString n divisor =
    if isDivisible n divisor then
        case divisor of
            3 ->
                "Pling"

            5 ->
                "Plang"

            7 ->
                "Plong"

            _ ->
                ""

    else
        ""


isDivisible : Int -> Int -> Bool
isDivisible x y =
    modBy y x == 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?