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:
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.
19 yo student *and* a full-time developer. Building with Rails currently, exploring new frontiers. I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
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 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! 😋
functionalphabet_position($text){$alphabet=range('a','z');$strippedText=str_split(strtolower(preg_replace("/[^a-zA-Z]/","",$text)));$result="";foreach($strippedTextas$letter){$result.=array_search($letter,$alphabet)+1." ";}return$result;}echoalphabet_position("The sunset sets at twelve o' clock.");
letposition=(str)=>{constupper=str.trim().toUpperCase().split('');letarr=[];upper.map(l=>(/^[a-z]+$/i.test(l))&&arr.push(l.charCodeAt(0)-64).toString())returnarr.join("");}position("The sunset sets at twelve o' clock.");
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 from Show (Int in this case) to String.
unwords joins a list of strings using space as a separator.
The Canadian @statamic Gentleman™. Vim nut. Bluffing musician. Go Saints. Go Jays. Go Habs. Husband & proud father. Love because he first loved us. 1 John 4:19.
functionalphabetPositions($string){returncollect(str_split($string))->map(function($letter){returncollect(range('a','z'))->flip()->get(strtolower($letter));})->filter()->map(function($key){return$key+1;})->implode(' ');}echoalphabetPositions("The sunset sets at twelve o' clock.");
Will be cleaner when PHP gets shorthand arrow functions, which I believe are coming in 7.4 😍 ...
functionalphabetPositions($string){returncollect(str_split($string))->map(fn($letter)=>collect(range('a','z'))->flip()->get(strtolower($letter)))->filter()->map(fn($key)=>$key+1)->implode(' ');}echoalphabetPositions("The sunset sets at twelve o' clock.");
Coding since 11yo, that makes it over 30 years now ~~~ Have a PhD in Comp Sci ~~~ Love to go on bike tours ~~~ I try to stay as generalist as I can in this crazy wide place coding is at now.
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.
JavaScript
My take at the challenge in JavaScript.
Source-Code
Test it yourself
Available online here.
love how readable this is
Thank you sir!
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.
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
Rust:
this has shown me how similar rust syntax can be to JavaScript syntax wow
Python one liner to the rescue 🙂
print(*[ord(x.lower())-96 for x in input() if x.isalpha()])In C++
Edited: there was a bug :D
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 isAlphaandisAlphafunctions, sincetoNumberalready returnsNonewhen the character isn't a letter, which chops off a nice bit of the solution!You can also use
findIndexfromData.Listinstead of find-with-zip (though that solution is cool! 😋a bit late but here's the answer anyway... in PHP
Lua, just as a series of string operations:
Lua, written with a loop and so a bit less wasteful:
JavaScript
Python
Haskell
Some function composition sorcery in Haskell.
Explanation
The
.operator composes functions, so they will be applied from right to left.filter isLetterwill 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.toUppertransforms the character to uppercase, so that we can substract 64 from it's code to know the position.ordmaps 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.showmaps any type deriving fromShow(Intin this case) toString.unwordsjoins a list of strings using space as a separator.In PHP using Laravel's Collection pipeline...
Will be cleaner when PHP gets shorthand arrow functions, which I believe are coming in 7.4 😍 ...
A JS one-liner
Output:
Here is the simple solution with PHP:
solved in rust made with tests first :)
Perl solution:
Reading from the right:
shiftgets the argument,lclower-cases it,splitusing an empty regex splits it into characters,grepremoves all non-letters,ordreturns the ASCII ordinal number of each letter, 97 corresponds toa;mapreplaces the characters by the numbers,joinconnects the numbers back to a string.See join, map, ord, grep, split, lc, shift.
ruby <3
"One-liner" (kind of) Ruby:
My take on this challenge, with Javascript
so simple love it
One line Javascript
A tiny python solutiuon:
C#
[gist.github.com/devparkk/d46fd8763...]
Python :