Good morning, everyone.
Let's warm up for the start of the week by playing with strings.
s'yadoT egnellahc semoc from user xDranik on sraWedoC!
...
For further actions, you may consider blocking this person and/or reporting abuse
JavaScript
Using reduce to generate a string from the array, but then I have to use trim as I end up with a space at either end :-/
Live demo on CodePen.
Nice one!
If you replace the
trim()
with asubstring(1)
then it'll work with strings that have spaces in front and behind since you're reliably adding a single space to the front, none to the back.See the warning here re the
split('')
call. Seems the recommended way now is [...string].JS quickie
Perl one-liner:
A bit more cleaned up, taking user input:
See runnable demo on PerlBanjo
Note that
($_)
is not needed forlength
andreverse
.Yeah, true, that's just my personal preference 🙂
And one that modifies a string inplace using C
Python one-liner:
for x in input().split(): print(x[::1]*(len(x)<5) or x[::-1], end=' ')
65 chars at most :)
Or,
print(*map(lambda x: x[::1]*(len(x)<5)or x[::-1], input().split()))
Also 65 at most :)
Here is my Rust solution and test cases!
The Rust std lib made this one pretty simple!
Haskell, with user input
This one was fun! I decided to approach it from a slightly different angle by building a general-purpose
when
function that could be used to decide when to map to a value (vs return the original value) based on a condition... just for kicks :-)Here's my go:
Gist (w/ some tests): gist.github.com/kerrishotts/ac0f30...
Rust, sort of in place
The rest of the story below the fold:
A quick one in C#
PHP 5.3.0 to 7.3.x:
PHP >= 7.4:
Elixir:
Ruby
Ruby
Tests
Results
My solution in js
Haskell:
JS/ES
Python 3 : :P
s='yadoT egnellahc semoc from user xDranik on sraWedoC!'
print(s[::-1])
function spinWords(string){
return string.split(' ').map(function(string){
return (string.length > 4) ? string.split('').reverse().join('') : string
}).join(' ');
}