DEV Community

Discussion on: Daily Challenge #47 - Alphabets

Collapse
 
choroba profile image
E. Choroba • Edited

Perl solution:

#!/usr/bin/perl
use warnings;
use strict;

sub alphabet_position {
    join ' ', map ord() - 96, grep /[a-z]/, split //, lc shift
}

use Test::More tests => 1;

is alphabet_position("The sunset sets at twelve o' clock."),
    '20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11';

Reading from the right: shift gets the argument, lc lower-cases it, split using an empty regex splits it into characters, grep removes all non-letters, ord returns the ASCII ordinal number of each letter, 97 corresponds to a; map replaces the characters by the numbers, join connects the numbers back to a string.

See join, map, ord, grep, split, lc, shift.