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 cho...
For further actions, you may consider blocking this person and/or reporting abuse
Displaying a subset of the total comments. Please sign in to view all comments on this post.
CSS
Just add the class
removeFirstAndLastLetterto a tag, and see the first and last letter "removed" from the text 😋And as an extra, it can be stylized and animated, so you can see the letters fade out:
Ha! This one is great.
Thanks :)
JavaScript
Python
C++
C
Let people do something 😂
HAHAHA damn. Let me finish my code LOL.
Let's just go wild
Why not?
This is indeed wild
In JavaScript it could be 24 bytes:
I am surprised no one wrote test code. Sometimes in interviews with challenge this simple and you have access to run ruby they are expecting to see test code. Check out my ruby test code tutorials buds.
I notice lots of people are raising an error instead of ignoring or returning null so they have failed the challenge's instructions.
Ruby
I think you can get away with
string[1..-2]there, Ben.BASH
I was surprised to find that
-1to work as well:I tried to not use any str function.
Rust
View it in the Rust Playground here: play.rust-lang.org/?version=stable...
in C# as Extension-Method
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
solution in SQL (postgres)
In C#, I would use in built string function Trim()
In my opinion we don't need Substring() here.
Just call :
With 2 length validation:
But what if the input would be "aaajldflbbb"?
When using Trim(char[]), all appearances of the characters given to the method at the beginning and at the end, will be removed, leaving "jldfl".
ReasonML
Clojure:
Ruby Language Version
With specs
output
Ruby solution
I've only just stumbled over the Daily Challenge. Oh well, here's my C# Linq solution (different from the other one) and returns an empty string if length <= 2.
Swift
APL (using Dyalog APL)
as a direct function:
Testing it
Try it online!
I'll start doing this challenges in Dart :D
Factor
and some tests
In F#. Not the most straightforward way to solve this, but I wanted to use the language Array splicing.
And it turns out that you don't even need to explicitly convert it to an array. F# will let you do splicing on a string. So a much better solution is:
My solution using Python and list comprehension :)
Powershell
JAVA
Python
PHP:
I see a lot of solutions removing characters without checking to see if they're "letters." Here's my JavaScript solution.
Off the top of the head in Python 🐍🐍🐍🐍
Trivial in Ruby:
VB5 / VB6
Visual Basic .NET
C#
C# using LINQ (because yes)
In Python:
Rust
Solution in Go:
Running example in Go Playground
I liked today's challenge so I'm going back to do past ones
Returns
My "enterprise" solution
Hi!
A bit late to the party, but here's a Groovy one:
My approach in groovy...
Not sure if "ignore <2 chars" means "don't consider" or "ignore the trimming"
Don't consider =>
Ignore =>
I don't like returning null.
Swift - 5
I am so many months late in the game so starting with the first one.
And also unit tested the same:
Elixir
Im new to rust, and tried to get in some detailed error-handling.
Also added the functionality to not peel text that is too long as it does not make sense to me rn.
Maybe it would be better to take an String instead of &str as an argument, but idk.
Go
Purescript
flremove val =
let
value = toCharArray val
value1 = Array.length value
in
if (length val > 2)
then do
show $ fromCharArray $ Array.slice 1 (value1 -1) value
else ""
main = render =<< withConsole do
log $ flremove "string"
C++ that takes user input
Go:
java function using string builder
public String removefirstWordLastWord(String mystring )
{
///Delete last and first character
StringBuilder chr=new StringBuilder(mystring);
String strresult="";
if (mystring.length()>=2)
{
chr.deleteCharAt(0);
chr.deleteCharAt(chr.length()-1);
strresult=chr.toString();
}else{
strresult=("Invalid String");
}
}
Yesterday I started to discover golang, so i try do make these daily challenges in go.
My simple go solution
Handwrote my answer to practice:
const firstLast = (str) => {
const letters = [...str];
if (letters.length <=2){
return null;
}
let trimmed = letters.shift().pop();
return trimmed.join('');
}
Now to see if it works...
Rust
Here is my simple solution :).
const strig_slicer = (some_string) => some_string.length >= 2 : some_string.slice(1,-1) : null
Here in JS
I am new to the coding world so my code may not be that much efficient, but i am happy with the fact that i wrote it.
import sys
def trim_characters(input_string):
return_string=""
#print(return_string)
if (len(input_string))>2:
for i in range(0, len(input_string)):
if i!=0 and i!=len(input_string)-1:
return_string=return_string + input_string[i]
print("Output string : "+ return_string)
else:
print("Minnimum 3 characters are allowed")
trim_characters(sys.argv[1])
if (str.Length <= 2) return;var t = str.Substring(1, str.Length - 2);
Console.Write(t);
Javascript
Haskell
Erlang
Ruby
Or
My solution in js
The SmEXy python.
Java:
someString.length()<=2?someString:someString.substring(1,someString.length()-1)
I think Javascript is the most efficient line liner, I have also added trim :) to take care of extra white spaces
Javascript
I'm late to the party, catching up one by one!
Haskell:
Ruby
Python
def remove(yo): if yo <= 2: print("Not enough characters in string") else: return yo[1:-1]Ruby