Avatar of paulfioravanti

paulfioravanti's solution

to Raindrops in the Elm Track

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

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 Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

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 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 =
    let
        conversions =
            [ ( 3, "Pling" )
            , ( 5, "Plang" )
            , ( 7, "Plong" )
            ]

        conversion =
            conversions
                |> List.foldl (addRaindrop number) ""
    in
    case conversion of
        "" ->
            String.fromInt number

        _ ->
            conversion



-- PRIVATE


addRaindrop : Int -> ( Int, String ) -> String -> String
addRaindrop number ( factor, raindrop ) acc =
    case modBy factor number of
        0 ->
            acc ++ raindrop

        _ ->
            acc

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?