DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
jesseleite profile image
Jesse Leite

In PHP using Laravel's Collection pipeline...

function alphabetPositions($string)
{
    return collect(str_split($string))
        ->map(function ($letter) {
            return collect(range('a', 'z'))->flip()->get(strtolower($letter));
        })
        ->filter()
        ->map(function ($key) {
            return $key + 1;
        })
        ->implode(' ');
}

echo alphabetPositions("The sunset sets at twelve o' clock.");

Will be cleaner when PHP gets shorthand arrow functions, which I believe are coming in 7.4 😍 ...

function alphabetPositions($string)
{
    return collect(str_split($string))
        ->map(fn($letter) => collect(range('a', 'z'))->flip()->get(strtolower($letter)))
        ->filter()
        ->map(fn($key) => $key + 1)
        ->implode(' ');
}

echo alphabetPositions("The sunset sets at twelve o' clock.");