Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.
For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. The input will be a lowercase string with no spaces.
Tests
capitalize("dev")
capitalize("method")
capitalize("hello")
Good luck!
This challenge comes from kenkamau on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (18)
Scala
A good old fold and I use the
Nexttype to know which side to capitalize next.Interesting JS solution....I haven't seen ternary operators used so haphazardly before, I'm not comfortable with them still.
SQL:
Break the string down into a table with one character per row, use the
row_numberwindow function to capitalize it each way, then reassemble the final product; the latter two steps can't be combined because window functions can't be nested in aggregate functions.I'm a bit late to the party, but here's my Elixir solution:
Kinda silly solution using 2 mutually recursive functions (Haskell)
Ooh, clever! I like it!
One more Python solution:
This is a hallmark of proper functional programming. Elm looks intimidating than more traditional grammars but it's essentially strongly-typed function composition. I love it, and the outer function reads like english.
Solution in Haskell using folding:
The
skipCasefolding function takes care of upcasing every 2. letter. Thefolderis a makes it easier to map the function over both capitalization variations, which then can be applied to the input string.Here is some shit I wrote using shellscript
Ruby
Some comments may only be visible to logged-in visitors. Sign in to view all comments.