Today's challenge requires you to write a function which removes all question marks from a given string.
For example:
hello?
would behello
This challenge comes from aikedaa here on DEV.
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Discussion
I'll do:
TrustmeI'manEngineer
Hey there! Nice to see some bash users here.
You can even use bash without sed using this little trick:
Try it online.
See Manipulating String.
Are you aware of the fish shell? I guess that you're pro+ in the bash, so I can't know about that. If not, check it out. You'll like that! It's a fully auto comopletion shell that is perfect for the home users. For scripts you'll still have to use bash though (ot tcsh, zsh whatsoever....) :)
If not you, hope so somebody will find it useful.
I've never had the courage to deep dive into the Fish shell honestly but your comment gave me some strength. I'll fire up a Docker container to test it out. Added to my ToDo list! Thanks.
hooray!
thanks that's nice!
sed
is overkill. Trytr
:Yap, he is right :)
I'm not yet a Senior developer.
ruby <3
Ha, you probably confused some people there, especially with the character literal.
I love weird Ruby syntax😆
TIPS:
??
is known as character literal notationdocs.ruby-lang.org/en/trunk/syntax...
I know, that's why I wrote "especially with the character literal" ;-)
Other literals many people don't know are rational numbers (
4/3r
) and complex numbers (3+2i
).I see. Also
%
literal is my favorite string literal :)JavaScript
Here is my take to the challenge:
Source-Code
Available online.
Side-note
By the name of the title in my notifications, I really though that the challenge would be to write a challenge and submit the best to the Dev.to team as they were running out of ideas. Haha!
Please submit challenge ideas! Simply email yo+challenge@dev.to with any proposals and we'll give you credit when we post it :)
Tomorrows challenge:
Oh, sounds sexy. I love it! Haha.
x86_64 assembly (System V ABI, GNU assembler), as usual:
bsure.S:
bsure.h:
Javascript:
Called as:
Important to note, this will also remove the first escape characters (
\
) and output newlines in template literals as\n
Rust
Playground
... 😐
I mean... that's not even a function to write. It's a function to call.
Elixir:
Ruby:
JavaScript:
Bash:
Okay, I'll bite and imagine that Carmen Sandiego has stolen all the regular expressions!
And to use it:
NOOOO!!!
You need to create an instance of the
OpinionAboutJavaFactory
! You can just have a staticgetOpinionAboutJava
method! Now your code is not SOLID!ENTERPRISE
Ruby
(javascript)
Uses a simple regular expressions with the global (
g
) modifier to find all the?
(this needs to be escaped in a regular expression, so\?
) and replace them with the empty string''
.PHP :
Hey there! Awesome to see some PHP users. PHP IS NOT DEAD! Haha.
But you are halfway done buddy!
okay okay let's do a wrapper function then :D
That function name tho. haha!
Perl solution:
The
tr
operator works like thetr
shell util./d
means non-replaced characters are deleted,/r
returns the value instead of modifying the bound variable.Even though this is a simple one - it's really interesting to see all of the different ways you can do it!
But I agree with Josh - it's built into most languages :)
Bash somebody?
This one works for me. Since Bash doesn't support the global flag for regular expressions I had to iterate over the input string...
Try
tr
instead. 😁"the global flag in regular expression"
You've got regex for search in bash and regex for replace.
For replace, you've got sed. Are you familiar with sed? People usually skip it when learning bash for themselves on their own.
I intentionally avoided using
sed
this time, wanted to implement it with "pure" Bash. Usingsed
the implementation, of course, becomes much shorter.Haskell:
Similar in Elm (but we have to use wrapping parenthesis for binary operators).
My solution in js
Python:
PHP:
Well, since we're doing cmd things...
(bash on mac)
`
Python
As simple as it sounds, in python :