dev.to staff

Posted on

Daily Challenge #47 - Alphabets

In today's challenge, you are asked to replace every letter with its position in the alphabet for a given string where 'a' = 1, 'b'= 2, etc.

For example:

`alphabet_position("The sunset sets at twelve o' clock.")` should return `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` as a string.

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

This challenge comes from MysteriousMagenta on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Amin • Edited

JavaScript

My take at the challenge in JavaScript.

Source-Code

``````"use strict";

function lettersOnly(letterOrElse) {
return letterOrElse.toUpperCase() !== letterOrElse.toLowerCase();
}

function toAlphabetPosition(letter) {
return letter.toLowerCase().charCodeAt(0) - 'a'.charCodeAt(0) + 1;
}

function alphabet_position(input) {
return Array
.from(input)
.filter(lettersOnly)
.map(toAlphabetPosition)
.join(" ");
}

const result = alphabet_position("The sunset sets at twelve o' clock.");
const expectations = "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";

assert(result === expectations); // undefined (meaning OK)
``````

Test it yourself

Available online here.

Jarod Smith

Amin

Thank you sir!

Jarod Smith
``````const alphaPosition = str => [...str]
.filter(letter => letter.toLowerCase().charCodeAt(0) - 96 > 0)
.map(letter => letter.toLowerCase().charCodeAt(0) - 96)
.reduce((s, pos) => s += `\${pos} `, '')
.trim();
``````

classic map filter reduce problem loved it.

x86_64 assembly (System V ABI, GNU assembler), as usual. Not really correct, since there will be an extra `' '` at the end which I was too lazy to remove, but it'll do.

alphabetic_position.S

``````    .global alphabetic_position

.text
alphabetic_position:
#using these registers to avoid sprintf breaking them
push %rbx
push %rbp

mov %rdi, %rbx
mov %rdi, %r12
mov %rsi, %rbp

xor %eax, %eax
xor %edx, %edx
loop:
mov (%rbp), %dl

cmp \$65, %dl # 'A'
jl skip

cmp \$91, %dl # 'Z' + 1
jl print

cmp \$97, %dl # 'a'
jl skip

cmp \$123, %dl # 'z' + 1
ja skip

printl:
sub \$32, %dl # 'a' -> 'A'
print:
sub \$64, %dl # 'A' -> 1

push %rdx

mov %rbx, %rdi
mov \$format, %rsi
call sprintf

pop %rdx
skip:
inc %rbp
cmp \$0, %dl
jne loop

mov %r12, %rax

pop %rbp
pop %rbx
ret

.section .rodata
format:
.asciz "%d "
``````

alphabetic_position.h:

``````char *alphabetic_position(char *dst, const char *src);
``````

Edit: the function name now conforms to the specification, as well as with the "returns the string" requirement (by returning a copy of dst).

Héctor Pascual

that's crazy

Tiash • Edited

In C++

``````#include <string>
#include <iostream>

void alphabet_position(std::string s) {
for (int i = 0; i < s.size(); i++)
{
int pos = (int)((char)s[i] - 'a');
pos = pos < 0 ? pos + ('a' - 'A') : pos;
if (pos >= 0 && pos <= 'z' - 'a') {
std::cout << pos+1 << " ";
}
}
}

int main (int argc, char *argv[])
{
alphabet_position("<The quick brown fox jumps over the lazy dog!>");
//print 20 8 5 17 21 9 3 11 2 18 15 23 14 6 15 24 10 21 13 16 19 15 22 5 18 20 8 5 12 1 26 25 4 15 7
return 0;
}
``````

Edited: there was a bug :D

Python one liner to the rescue 🙂

`print(*[ord(x.lower())-96 for x in input() if x.isalpha()])`

Oleksii Filonenko • Edited

Rust:

``````pub fn alphabet_position(text: &str) -> String {
text.to_lowercase()
.chars()
.filter(|c| c.is_alphabetic())
.map(|char| (char as usize - 96).to_string())
.collect::<Vec<String>>()
.join(" ")
}

#[test]
fn test_alphabet_position() {
assert_eq!(
alphabet_position("The sunset sets at twelve o' clock."),
String::from("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")
);
}
``````

Jarod Smith

this has shown me how similar rust syntax can be to JavaScript syntax wow

Alvaro Montoro • Edited

JavaScript

``````const code = s => [...s].reduce((a, v) => v.match(/[a-z]{1}/i)
? a+(v.toLowerCase().charCodeAt(0)-96)+' '
: a
, '')
.trim();
``````

And as an extra, the decoder:

``````const decode = s => String.fromCharCode(...s.split(' ').map(val=>parseInt(val) + 96));
``````

Although the decoding process is not perfect, because all the spaces and symbols are lost during the coding process. For example, the sentence "The sunset sets at twelve o' clock" will be coded into:

"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"

Which will be decoded into:

"thesunsetsetsattwelveoclock"

Craig McIlwrath
``````import Data.Maybe (catMaybes)
import Data.List (find)
import Data.Functor (fmap)
import Data.Char (toLower)

alpha = ['a'..'z']

isAlpha :: Char -> Bool
isAlpha = (`elem` alpha)

(>.<) :: (a -> b -> d) -> (c -> b) -> (a -> c -> d)
a >.< b = flip \$ flip a . b

toNumber :: Char -> Maybe Int
toNumber = fmap snd . (flip find) alphaNum . ((==) >.< fst)
where alphaNum = zip alpha [1..]

encodeMsg :: String -> String
encodeMsg = unwords . map show . catMaybes . map toNumber . filter isAlpha . map toLower
``````

I wanted to try to write this function completely using point-free style. It led to me having to write that `>.<` operator, which you can see from the type definition exactly what it does. It was a good mental exercise in types for me, a Haskell beginner.

Curtis Fenner

You don't need your `filter isAlpha` and `isAlpha` functions, since `toNumber` already returns `None` when the character isn't a letter, which chops off a nice bit of the solution!

You can also use `findIndex` from `Data.List` instead of find-with-zip (though that solution is cool! 😋

``````toNumber = (fmap (+1)) . (flip findIndex alpha) . (==)
``````

Mat-R-Such

Python

``````import string
def alphabet_position(text):
text = [i for i in text.replace(' ','').lower() if i.isalpha() ]
position = [str(string.ascii_lowercase.find(i)+1) for i in text ]
return ' '.join(position)
``````

peter279k

Here is the simple solution with PHP:

``````function alphabet_position(string \$s): string {
\$s = strtoupper(\$s);
\$alphabetNums = range(65, 90);
\$alphabets = [];

foreach (\$alphabetNums as \$chr) {
\$aphabets[] = chr(\$chr);
}

\$result = "";

\$index = 0;
for (; \$index < strlen(\$s); \$index++) {
if (in_array(\$s[\$index], \$aphabets) === false) {
continue;
}

\$result .= (string)(ord(\$s[\$index]) - 64) . " ";
}

return substr(\$result, 0, -1);
}
``````

Avalander

Some function composition sorcery in Haskell.

``````import Data.Char (isLetter, toUpper, ord)

alphabet_position :: String -> String
alphabet_position = unwords . map (show . (flip (-)) 64 . ord . toUpper) . filter isLetter
``````

Explanation

The `.` operator composes functions, so they will be applied from right to left.

1. `filter isLetter` will remove all characters that are not letters from the string.
2. `map (show . (flip (-)) 64 . ord . toUpper)` Transforms each character to its position in the alphabet.
1. `toUpper` transforms the character to uppercase, so that we can substract 64 from it's code to know the position.
2. `ord` maps a character to its ASCII code.
3. `(flip (-) 64)` subtracts 64 from the character code. Since the code for `'A'` is 65, this will give us the position in the alphabet starting at index 1. The way it works is it partially applies the second argument of the subtract operator to 64, i.e., this is equivalent to `(\x -> x - 64)` but fancier.
4. `show` maps any type deriving from `Show` (`Int` in this case) to `String`.
3. `unwords` joins a list of strings using space as a separator.

Ilyes Chouia • Edited

a bit late but here's the answer anyway... in PHP

``````function alphabet_position(\$text){
\$alphabet = range('a', 'z');
\$strippedText = str_split(strtolower(preg_replace("/[^a-zA-Z]/", "", \$text)));
\$result = "";
foreach(\$strippedText as \$letter){
\$result .= array_search(\$letter, \$alphabet)+1 . " ";
}
return \$result;
}
echo alphabet_position("The sunset sets at twelve o' clock.");
``````

Curtis Fenner • Edited

Lua, just as a series of string operations:

``````local function solution(text)
return text:lower()
:gsub("%A+", "")
:gsub("%a", function(n) return " " .. 1 + n:byte() - string.byte("a") end)
:sub(2)
end
``````

Lua, written with a loop and so a bit less wasteful:

``````local function solution(text)
local ns = {}
for a in text:gmatch("%a") do
table.insert(ns, 1 + a:lower():byte() - string.byte("a"))
end
return table.concat(ns, " ")
end
``````

Yasser Beyer • Edited

JavaScript

``````let position = (str) => {
const upper = str.trim().toUpperCase().split('');
let arr = [];
upper.map(l => (/^[a-z]+\$/i.test(l)) && arr.push(l.charCodeAt(0)-64).toString())
return arr.join(" ");
}

position("The sunset sets at twelve o' clock.");
``````

willsmart • Edited

A JS one-liner

``````alphaPosition = s => [...s.toLowerCase().replace(/[^a-z]/g, '')].map(c => c.charCodeAt(0) + 1 - 'a'.charCodeAt(0)).join(' ');
``````

Output:

``````> alphaPosition("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"
``````