The goal of this challenge is to write a function that accepts a string and returns the same string with all even indexed characters uppercased and...
For further actions, you may consider blocking this person and/or reporting abuse
Here goes a simple oneliner:
And the result:
I believe
case
should be printed asCaSe
as first letter (index 0) should be always capitalized.Indeed! Didn't catch that! Here is a little fix for that:
which output is:
Index 0 refers to the first letter of the entire string; we don't need to consider each word separately, just the string as a whole
Sorry about that.
I meant it as
index 0
of each word.the example shows the result should be
Ah, you're right, that's interesting. I hadn't noticed that discrepancy with the capital 'C' in 'CaSe'; that makes this a more interesting challenge!
The original post should probably call that out a little more clearly, because it raises additional questions: Should punctuation count as a word separator? e.g., should 'word-other' become 'WoRd-oThEr' or 'WoRd-OtHeR'? Or should we only worry about letters and spaces? What characters are we considering?
@thepracticaldev , any help here?
For some reason, CodeWars isn't loading for me so won't be able to check the edge cases... 🤔
Python solution:
EDIT
@dance2die pointed out in this comment that the multi-word example in the post seems to suggest that each word should be considered and cased separately, rather than the whole string. In that case, this becomes a bit more interesting.
Here's a simple modification to account only for spaces separating words:
But this could get a lot more complex if we need to account for things like punctuation and other whitespace characters.
As one last example, here's one that handles alternative whitespace characters using regular expressions:
In Js ( Many Optimised solutions are already given, just tried different way)
Ruby, not optimal 🙃
JavaScript
And as an extension, I used that function to create a Sponge Bob Mocking meme generator! (which is the first thing that I thought when I saw the challenge)
A simple solution in Javascript:
At work, we just call this serial killer case :P
Python one liner
My solution in js
I've implemented based off of La blatte's answer.
prints,
Elixir:
Here is the simple solution with PHP:
Here's mine -- I'll admit that it took a little to recognize that the index was supposed to be word-based, not for the entire string. But that's what test cases are for! :-)
Gist: gist.github.com/kerrishotts/e3a213...
Ruby solution. Doesn’t use modulo.
Works with the strange edge case. Also sorry typed on tablet.
Ah I tried
map_with_index
but that's not a method. Did not know there was a.map.with_index
!I'm glad to hear that. Enumerable has so many methods, which is useful but I take a little bit time to remember them.
Solution in Dart. Simple and not the shortest one, I guess. But it works =)
Link to the playground: dartpad.dartlang.org/f01143ca792ed...
PHP 💻
Haskell:
I used applicative functors here mostly for fun. I think it looks nicer to define
weirdifier
asHeres my ruby example
result
Hey! We loved this challenge and actually got three developers to live code a solution in 15 minutes (with commentary): youtu.be/Z1fQ7J5fZhY
We also release similar videos every week on other Javascript, HTML/CSS, Python and Ruby challenges, so don't forget to like and subscribe if you're interested! ♥️
Python with a function :
One liner with list comprehension :
Here is a little Haskell one-liner
A JS single line solution that works with multi-line texts:
any CSharp Solution???