DEV Community

Discussion on: Daily Challenge #80 - Longest Vowel Change

Collapse
 
aminnairi profile image
Amin

Elm

module Vowel exposing (getLongestVowelSize)


getFirstVowelSubstring : String -> String
getFirstVowelSubstring string =
    case string of
        "" ->
            ""

        _ ->
            let
                first : String
                first =
                    String.left 1 string

                rest : String
                rest =
                    String.dropLeft 1 string
            in
            if List.member first [ "a", "e", "i", "o", "u", "y" ] then
                first ++ getFirstVowelSubstring rest

            else
                ""


getLongestVowel : String -> String -> String
getLongestVowel longest string =
    case string of
        "" ->
            longest

        _ ->
            let
                substring : String
                substring =
                    getFirstVowelSubstring string

                rest : String
                rest =
                    String.dropLeft 1 string
            in
            if String.length longest > String.length substring then
                getLongestVowel longest rest

            else
                getLongestVowel substring rest


getLongestVowelSize : String -> Int
getLongestVowelSize =
    getLongestVowel "" >> String.length

Tests

module VowelTest exposing (suite)

import Expect exposing (equal)
import Test exposing (Test, describe, test)
import Vowel exposing (getLongestVowelSize)


suite : Test
suite =
    describe "Vowel"
        [ test "Should return 0 for a string with no vowels" <|
            \_ -> equal 0 <| getLongestVowelSize "bcdfgh"
        , test "Should return 1 for a string with a one-vowel substring" <|
            \_ -> equal 1 <| getLongestVowelSize "abcdefghij"
        , test "Should return 2 for a string with a two-vowel substring" <|
            \_ -> equal 2 <| getLongestVowelSize "codewarriors"
        ]