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 return20 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!
Top comments (34)
JavaScript
My take at the challenge in JavaScript.
Source-Code
Test it yourself
Available online here.
love how readable this is
Thank you sir!
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
alphabetic_position.h:
Edit: the function name now conforms to the specification, as well as with the "returns the string" requirement (by returning a copy of dst).
that's crazy
JavaScript
And as an extra, the decoder:
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:
Which will be decoded into:
Link to live demo.
In C++
Edited: there was a bug :D
Python one liner to the rescue 🙂
print(*[ord(x.lower())-96 for x in input() if x.isalpha()])
Rust:
this has shown me how similar rust syntax can be to JavaScript syntax wow
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.You don't need your
filter isAlpha
andisAlpha
functions, sincetoNumber
already returnsNone
when the character isn't a letter, which chops off a nice bit of the solution!You can also use
findIndex
fromData.List
instead of find-with-zip (though that solution is cool! 😋solved in rust made with tests first :)
Haskell
Some function composition sorcery in Haskell.
Explanation
The
.
operator composes functions, so they will be applied from right to left.filter isLetter
will remove all characters that are not letters from the string.map (show . (flip (-)) 64 . ord . toUpper)
Transforms each character to its position in the alphabet.toUpper
transforms the character to uppercase, so that we can substract 64 from it's code to know the position.ord
maps a character to its ASCII code.(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.show
maps any type deriving fromShow
(Int
in this case) toString
.unwords
joins a list of strings using space as a separator.