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

rootulp's solution

to Raindrops in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a number to a string, the contents of which depend on the number's factors.

  • If the number has 3 as a factor, output 'Pling'.
  • If the number has 5 as a factor, output 'Plang'.
  • If the number has 7 as a factor, output 'Plong'.
  • If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

Examples

  • 28's factors are 1, 2, 4, 7, 14, 28.
    • In raindrop-speak, this would be a simple "Plong".
  • 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
    • In raindrop-speak, this would be a "PlingPlang".
  • 34 has four factors: 1, 2, 17, and 34.
    • In raindrop-speak, this would be "34".

Elm Installation

Refer to the Exercism help page for Elm installation and learning resources.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

$ elm-package install --yes

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (..)

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)
        ]

elm-package.json

{
    "version": "3.0.0",
    "summary": "Exercism problems in Elm.",
    "repository": "https://github.com/exercism/elm.git",
    "license": "BSD3",
    "source-directories": [
        ".",
        ".."
    ],
    "exposed-modules": [],
    "dependencies": {
        "elm-lang/core": "5.0.0 <= v < 6.0.0",
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0"
    },
    "elm-version": "0.18.0 <= v < 0.19.0"
}
module Raindrops exposing (..)

raindrops: Int -> String
raindrops number =
    if isFactor 3 number || isFactor 5 number || isFactor 7 number then
        plingPlangPlong number
    else
        toString number

plingPlangPlong: Int -> String
plingPlangPlong number =
    String.join "" [
        if isFactor 3 number then "Pling" else "",
        if isFactor 5 number then "Plang" else "",
        if isFactor 7 number then "Plong" else ""
    ]

isFactor: Int -> Int -> Bool
isFactor potentialFactor number =
    number % potentialFactor == 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?