DEV Community

Discussion on: Daily Challenge #197 - Population Growth

Collapse
 
aminnairi profile image
Amin • Edited

Elm

I was too lazy, just put floats everywhere.

module Main exposing (populationGrowth)


populationGrowth : Float -> Float -> Float -> Float -> Int
populationGrowth start percent augmentation expectation =
    let
        population : Float
        population =
            start * ( 1 + ( percent / 100 ) ) + augmentation

    in
        if population < expectation then
            1 + populationGrowth population percent augmentation expectation

        else
            1

Tests

module MainTest exposing (suite)

import Expect exposing ( Expectation )
import Test exposing ( Test )
import Main exposing ( populationGrowth )


suite : Test
suite =
    Test.describe "Population Growth"
        [ Test.test "It should return 3 with a starting population of 1000, a percentage of 2% and an increase of 50 to reach 1200 people" <| \_ ->
            Expect.equal 3 <| populationGrowth 1000 2 50 1200
        , Test.test "It should return 15 with a starting population of 1500, a percentage of 5% and an increase of 100 to reach 5000 people" <| \_ ->
            Expect.equal 15 <| populationGrowth 1500 5 100 5000
        , Test.test "It should return 10 with a starting population of 1500000, a percentage of 2.5% and an increase of 10000 to reach 2000000 people" <| \_ ->
            Expect.equal 10 <| populationGrowth 1500000 2.5 10000 2000000
        , Test.test "It should return 94 with a starting population of 1500000, a percentage of 0.25% and an increase of 1000 to reach 2000000 people" <| \_ ->
            Expect.equal 94 <| populationGrowth 1500000 0.25 1000 2000000
        ]
$ docker run --rm --interactive --tty --volume "$(pwd)":/home/elm/app aminnairi/elm-test tests/**/*.elm
TEST RUN PASSED

Duration: 278 ms
Passed:   4
Failed:   0