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
CSS
Just add the class
removeFirstAndLastLetter
to 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
-1
to 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...
I wanted to take my chances with these challenges, but I decided to start from the beginning, so here's my (super) late entry, done in Java :
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 :/
in C# as Extension-Method
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".
Ruby Language Version
With specs
output
Ruby solution
Clojure:
ReasonML
PHP:
Elixir
Off the top of the head in Python 🐍🐍🐍🐍
Solution in Go:
Running example in Go Playground
Swift - 5
I see a lot of solutions removing characters without checking to see if they're "letters." Here's my JavaScript solution.
Rust
Swift
In Python:
Trivial in Ruby:
VB5 / VB6
Visual Basic .NET
C#
C# using LINQ (because yes)
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.
I am so many months late in the game so starting with the first one.
And also unit tested the same:
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.
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
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...
C++ that takes user input
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");
}
}
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.
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"
Go:
Go
JS
Codepen
Haskell
JS
Too late to the wagon, but here.
JS:
Haskell:
As a bonus, it works on any type of list
Javascript
Here The Journey Begins.. 🚀
Python
Rust
Python 3
function stringPeeler(str) {
return str.length > 2 ? str.substr(1, str.length-2) : 'Invalid String'
}
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
Ruby
R
Natively works on matrices/vectors of strings, which is cool.
Seat of your pants GNU C
(untested, also a bad idea in multiple ways)
Perl?
Or maybe just
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)
Python:
code:
example:
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])
Here is my simple solution :).
javascript
(function (str) {
if (str.length <= 2) {
return null;
}
console.log(str.slice(1, str.length - 1));
})('cut me from start and end')
JavaScript
github.com/manavm1990/code-challen...
Just to clarify. Can any given string contain whitespace character? What is the expected output of " \r\n"?
this is very basic but i'm just a beginner :
a = "challenge of the day"
if (len(a)) > 2:
print( a[ 1 : len(a)-1])
else :
print(a)
I am new to coding world, so my code might not be that efficient but i am happy with the fact that i have completed it.
Javascript :
s1 = s.length > 2 ? s.slice(1,s.length-1):null
public static String removeFirstAndLastLetter(String str) {
if(str.length()>2) {
return str.substring(1, str.length()-1);
}
return null;
}
if (str.Length <= 2) return;
var t = str.Substring(1, str.Length - 2);
Console.Write(t);