loading...

Discussion on: Daily Challenge #82 - English Beggars

Collapse
aminnairi profile image
Amin

Elm

module EnglishBeggars exposing (englishBeggars)


currentBeggar distribution turn donation =
    donation -- (0, 1)
        |> Tuple.first -- 0
        |> modBy distribution -- modBy 0 = 0, modBy 1 = 1, modBy 2 = 0
        |> (==) turn -- 0 == 0 = True, 1 == 0 = False, 0 == 0 = True


distribute donations distribution turn =
    donations -- [1, 2, 3, 4, 5]
        |> List.indexedMap Tuple.pair -- [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
        |> List.filter (currentBeggar distribution turn) -- [(0, 1), (2, 3), (4, 5)]
        |> List.map Tuple.second -- [1, 3, 5]
        |> List.sum -- 9


englishBeggars : List Int -> Int -> List Int
englishBeggars donations distribution =
    List.range 0 (distribution - 1) -- [0, 1]
        |> List.map (distribute donations distribution) -- [9, 6]

Tests

module EnglishBeggarsTest exposing (suite)

import EnglishBeggars exposing (englishBeggars)
import Expect exposing (equal)
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "English beggars"
        [ test "Should return [9, 6] for a donation of [1, 2, 3, 4, 5] between 2 beggars" <|
            \_ -> englishBeggars [ 1, 2, 3, 4, 5 ] 2 |> equal [ 9, 6 ]
        , test "Should return [5, 7, 3] for a donation of [1, 2, 3, 4, 5] between 3 beggars" <|
            \_ -> englishBeggars [ 1, 2, 3, 4, 5 ] 3 |> equal [ 5, 7, 3 ]
        , test "Should return [1, 2, 3, 0] for a donation of [1, 2, 3] between 4 beggars" <|
            \_ -> englishBeggars [ 1, 2, 3 ] 4 |> equal [ 1, 2, 3, 0 ]
        ]