Hey, everyone. We've decided to host a daily challenge series. We'll have a variety of challenges, ranging in difficulty and complexity. If you choose to accept the task, your goal is to write the most efficient function you can for your language. Bonus points for any features you add!
I’ll start you off with a simple, straightforward challenge. Our first one comes from user @SteadyX on CodeWars.
Your goal is to create a function that removes the first and last letters of a string. Strings with two characters or less are considered invalid. You can choose to have your function return null or simply ignore.
The fundamentals are important, it only gets more difficult from here.
Happy coding!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (113)
Ruby
I think you can get away with
string[1..-2]there, Ben.JavaScript
Python
C++
C
Let people do something 😂
HAHAHA damn. Let me finish my code LOL.
Python
def remove(yo): if yo <= 2: print("Not enough characters in string") else: return yo[1:-1]PHP
I like that substr trick with the -1, didn't think of that!
How about this one liner?
Use mb_string to support multibyte chars, such as Chinese
Also typehinted the argument, you never know...
This one is even shorter, possible only if the arg is typehinted tho
When the string is shorter than 2, substr returns false;
when the length is 2, substr returns "".
In both cases it's false, the the return is null.
Edit: many typos, I'm on mobile :/
Ruby
Basic
Extra
Rust
View it in the Rust Playground here: play.rust-lang.org/?version=stable...
Perl?
Or maybe just
Rust
Ruby
Some comments may only be visible to logged-in visitors. Sign in to view all comments.