DEV Community

Daily Challenge #210 - Separate Capitalization

dev.to staff on March 21, 2020

Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered e...
Collapse
 
avalander profile image
Avalander • • Edited

Scala

A good old fold and I use the Next type to know which side to capitalize next.

sealed trait Next
case object Left extends Next
case object Right extends Next

def capitalize (str: String): (String, String) = {
  val (l, r, _) = str.foldLeft(("", "", Left: Next)) {
    case ((l, r, n), c) => n match {
      case Left  => (l :+ c.toUpper, r :+ c.toLower, Right)
      case Right => (l :+ c.toLower, r :+ c.toUpper, Left)
    }
  }
  (l, r)
}

println(capitalize("abcdef")) // ("AbCdEf", "aBcDeF")
Collapse
 
fluffynuts profile image
Davyd McColl •
function capitalize(str) {
  return str.split("").reduce((acc, cur, i) => {
    const 
      odd = i % 2,
      upper = cur.toUpperCase();
    acc[0] += !odd ? upper : cur;
    acc[1] += odd ? upper : cur;
    return acc;
  }, ["", ""]);
}
Collapse
 
not_jffrydsr profile image
@nobody •

Interesting JS solution....I haven't seen ternary operators used so haphazardly before, I'm not comfortable with them still.

Collapse
 
dmfay profile image
Dian Fay •

SQL:

[local] dian#dian= WITH seq AS (
  SELECT regexp_split_to_table('string', '') AS c
), capitalized AS (
  SELECT
    CASE
      WHEN row_number() OVER () % 2 = 0 THEN upper(c)
      ELSE lower(c)
    END AS even,
    CASE
      WHEN row_number() OVER () % 2 = 1 THEN upper(c)
      ELSE lower(c)
    END AS odd
  FROM seq
)
SELECT
  string_agg(even, '') AS even,
  string_agg(odd, '') AS odd
FROM capitalized;
  even  │  odd   
────────┼────────
 sTrInG │ StRiNg
(1 row)

Time: 0.613 ms

Break the string down into a table with one character per row, use the row_number window 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.

Collapse
 
savagepixie profile image
SavagePixie • • Edited

I'm a bit late to the party, but here's my Elixir solution:

defmodule Letters do
  import Integer, only: [ is_even: 1, is_odd: 1 ]

  def capitalise(str) do
    str
      |> String.split("", trim: true)
      |> Enum.with_index
      |> Enum.map(&_mapper/1)
      |> Enum.unzip
      |> _join
  end

  defp _mapper({ letter, idx })
    when is_even(idx) do
      { String.capitalize(letter), String.downcase(letter) }
    end
  defp _mapper({ letter, idx })
    when is_odd(idx) do
      { String.downcase(letter), String.capitalize(letter) }
    end
  defp _join({ a, b }), do: { Enum.join(a), Enum.join(b) }
end
Collapse
 
craigmc08 profile image
Craig McIlwrath •

Kinda silly solution using 2 mutually recursive functions (Haskell)

import Data.Char (toUpper, toLower) 

skip :: String -> String
skip [] = [] 
skip (c:cs) = c : capitalize' cs

capitalize' :: String -> String
capitalize' [] = [] 
capitalize' (c:cs) = toUpper c : skip cs

capitalize :: String -> (String, String) 
capitalize cs = (capitalize' cs, skip cs) 
Collapse
 
avalander profile image
Avalander •

Ooh, clever! I like it!

Collapse
 
ksyula profile image
Ksenia • • Edited

One more Python solution:

def capitalize(s: str) -> list:
    cap_s_even, cap_s_odd = "", ""
    for i in range(0, len(s)):
        if i % 2 == 0:
            cap_s_even+=s[i].upper()
            cap_s_odd+=s[i]
        else:
            cap_s_even+=s[i]
            cap_s_odd+=s[i].upper()
    return [cap_s_even, cap_s_odd]
Collapse
 
not_jffrydsr profile image
@nobody •

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.

Collapse
 
cipharius profile image
Valts Liepiņš •

Solution in Haskell using folding:

import Data.Char (toUpper)

capitalize :: String -> [String]
capitalize str = folder skipCase <$> [("",0),("",1)] <*> pure str
  where
    folder fn init = fst . foldr fn init
    skipCase :: Char -> (String, Int) -> (String, Int)
    skipCase c (cs, i) | i `mod` 2 == 1 = ((toUpper c):cs, i+1)
                       | otherwise      = (c:cs, i+1)

The skipCase folding function takes care of upcasing every 2. letter. The folder is a makes it easier to map the function over both capitalization variations, which then can be applied to the input string.

Collapse
 
lordapple profile image
LordApple •
#!/bin/sh

ARR=$(seq 0 $(( ${#1} - 1 )))
LOWER=$(echo "$1" | tr '[:upper:]' '[:lower:]')

for x in $ARR; do 
  (( $x % 2 == 0 )) && ARR_EVEN+=$(echo "${LOWER:$x:1}" | tr '[:lower:]' '[:upper:]') || ARR_EVEN+=$(echo "${LOWER:$x:1}")
  (( $x % 2 )) && ARR_ODD+=$(echo "${LOWER:$x:1}" | tr '[:lower:]' '[:upper:]') || ARR_ODD+=$(echo "${LOWER:$x:1}")
done

echo ${ARR_EVEN[@]} ${ARR_ODD[@]}

Here is some shit I wrote using shellscript

Collapse
 
nilbert profile image
Nilbert •

Ruby

method = ->(s) { s.each_char.with_index.map { |c,i| [c.downcase,c.upcase].rotate(i[0])}.transpose.map(&:join)}

method.call 'dev'
=> ["dEv", "DeV"]

method.call 'method'
=> ["mEtHoD", "MeThOd"]

method.call 'hello'
=> ["hElLo", "HeLlO"]
Collapse
 
jhermann profile image
Jürgen Hermann • • Edited

Any Python version 2.7+.

>>> MiXeD = lambda s: ''.join(x.upper() if i&1==m else x for m in (0,1) for i, x in enumerate(s+' ')).split()
>>> MiXeD('abcdef')
['AbCdEf', 'aBcDeF']

😳 😱

Collapse
 
muhimen123 profile image
Muhimen •

This is the magic of python. Short. I just love it. 💗

Collapse
 
rafaacioly profile image
Rafael Acioly •

If you return like this return _change_even_casing(True), _change_even_casing(False) python will understand that it's already a tuple