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 all odd indexed characters lowercased.
This indexing should be zero-based with the index in position zero being considered even. The input string will consist of only alphabetical characters and spaces. Spaces should only be present when there are multiple words.
Example:
to_weird_case('String'); # => returns 'StRiNg'
to_weird_case('Weird string case') # => returns 'WeIrD StRiNg CaSe'
Happy coding~!
This challenge comes from user xDranik. 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 (32)
At work, we just call this serial killer case :P
Here goes a simple oneliner:
And the result:
I believe
caseshould be printed asCaSeas first letter (index 0) should be always capitalized.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 0of 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... 🤔
Indeed! Didn't catch that! Here is a little fix for that:
which output is:
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:
I've implemented based off of La blatte's answer.
prints,
Ah I tried
map_with_indexbut 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.
In Js ( Many Optimised solutions are already given, just tried different way)
Ruby, not optimal 🙃
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.
Solution in Dart. Simple and not the shortest one, I guess. But it works =)
Link to the playground: dartpad.dartlang.org/f01143ca792ed...